Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Managing a polymorphic environment
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Baptiste Rouquier <jrouquiethearchiveshouldhaveafewantispamtricks@e...>
Subject: Re: [Caml-list] Managing a polymorphic environment
>The first problem is that it doesn't work. (==) reduces to (=) for
>simple types
>
>let x = 5
>let y = 5
>let env = Env.add x "x" env
>let env = Env.add y "y" env
>
># Env.get_string x env ;;
>- : string = "y"

Because it's the same internal representation : "5" is stored in one word,
exactly the same word for all the values storing "5". int isn't boxed, there are
no pointers involved here. So (==) and (=) have no choice but being the same
operation on int.

You have to box your type if you want to distinguish between x and y. And once
they are boxed, why not adding the description in this type ? There are several
choices: trivial tuple type, property lists, objects...
Then, since you look for efficiency and thus want to work on int or float, you
need conversion functions between this boxed type (which makes (==) work as you
want) and int / float (more efficient). You need two distinct types.


For instance here you will also need a fonction 'a result -> 'a, and
complex_computation will have type int -> int -> int.

>># type 'a result = 'a * string;;
>># let new_result (a:'a) description = ((a,description) : 'a result);;
>>val new_result : 'a -> string -> 'a result = <fun>
>># let print_int ((a,d): int result) = Printf.printf "%s is %d\n%!" d a;;
>>val print_int : int result -> unit = <fun>
>># let print_float = ...
>># let x = new_result 10 "the number of elements in the knapsack";;
>>val x : int result = ...
>># print_int x;;
>>the number of elements in the knapsack is 10
>>- : unit = ()
>
>
>Here is the problem of this approach
>
># let complex_computation = fun x y -> x + y
>val complex_computation : int -> int -> int = <fun>
>
>let x = simple_computation () and
>    y = simple_computation ()
>
>let z = complex_computation x y
>
>Now I want to add a comment to x to print intermediate results and
>check the first computation is correct. In your approach, this lifts x
>: int on x : int result. But what will then be the type of
>complex_computation ?


Jean-Baptiste.

-------------------
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