Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] partial eval question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Walid Taha <taha@c...>
Subject: Re: [Caml-list] partial eval question

Hi Ben,

Below is a MetaOCaml (www.metaocaml.org) program that does what you were
asking for.  

----- begin ben.ml

Trx.init_times (* Initialize MetaOCaml timer library *)

(* Here's Ben's original code *)

let pow n x =                                                                      
  let rec pow_iter (n1, x1, p1) =                                                 
    if (n1 = 0) then 
      p1                                                         
    else if (n1 mod 2 = 0) then 
      pow_iter(n1/2, x1*x1, p1)                                     
    else pow_iter(n1-1, x1, p1*x1)                                         
  in pow_iter(n, x, 1);;                                                          
                                                                                    
let pow2 = pow 2

(* Here's a staged version of Ben's code *)

let pow' n x =                                                                        
  let rec pow_iter (n1, x1, p1) =                                                 
    if (n1 = 0) then 
      p1                                                         
    else if (n1 mod 2 = 0) then 
      pow_iter(n1/2, .<.~x1 * .~x1>., p1)                                     
    else pow_iter(n1-1, x1, .<.~p1 * .~x1>.)                                         
  in pow_iter(n, x, .<1>.);;

let pow2 = pow' 2

(* Here's some code to get some timings *)                                      
                                          
let unstagedRunning = 
  Trx.timenew "unstaged running"(fun () -> pow 5 3)

let stage1Running =
  Trx.timenew "stage 1 running" (fun () -> .<fun x-> .~(pow' 5 .<x>.)>.)

let compiling = Trx.timenew "compiling" (fun () -> .! stage1Running)

let stage2Running = Trx.timenew "stage 2 running" (fun () -> (compiling 
3))

let baseline = Trx.timenew "baseline" (fun () -> ())

(* Print all the timings *)

let _ = Trx.print_times ()
                                           
(* Outpout of timer:
__ unstaged running ______ 2097152x avg= 7.929525E-04 msec
__ stage 1 running ________ 262144x avg= 7.248323E-03 msec
__ compiling ________________ 8192x avg= 2.320860E-01 msec
__ stage 2 running ______ 16777216x avg= 1.123075E-04 msec
__ baseline _____________ 33554432x avg= 3.921819E-05 msec
*)

--- end ben.ml

Walid.

On Mon, 27 Oct 2003, Ben Kavanagh wrote:

|
|Say I have a function such as pow defined as
|
|let pow n x = 
|    let rec pow_iter (n1, x1, p1) = 
|        if (n1 = 0) then p1 
|        else if (n1 mod 2 = 0) 
|		 then pow_iter(n1/2, x1*x1, p1) 
|             else pow_iter(n1-1, x1, p1*x1)
|    in pow_iter(n, x, 1);;
|
|and I say 
|
|let pow2 = pow 2
|
|Are there any ML implementations that would automatically perform
|partial evaluation to create pow2 instead of using closures, possibly
|unfolding the pow_iter call? Would Caml ever have this capability?
|
|Ben
|
|
|-------------------
|To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
|Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
|Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
|

-- 


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners