caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Managing a polymorphic environment
@ 2004-08-12 14:46 Diego Olivier Fernandez Pons
  2004-08-12 15:06 ` Jean-Baptiste Rouquier
  2004-08-12 17:05 ` brogoff
  0 siblings, 2 replies; 7+ messages in thread
From: Diego Olivier Fernandez Pons @ 2004-08-12 14:46 UTC (permalink / raw)
  To: caml-list

    Bonjour,

Here is an other type related problem :

I would like to have an environment that associates strings to values

let x = ...
Env.add x "the number of elements in the knapsack"
let c = ...
Env.add c "the total cost of the knapsack"


The idea is of course to be able to pretty-print the result

# Env.print_int x;;
the number of elements in the knapsack is 10 - : unit = ()
# Env.print_float c
the total cost of the knapsack is 100.0 - : unit = ()


Env data structure can be based on equallity only since the user is
supposed to add comments only for the most important variables. The
simplest idea is once more hashing all variables. But one would like
to avoid collisions.


Any suggestion ?


        Diego Olivier

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


^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: [Caml-list] Managing a polymorphic environment
@ 2004-08-13 14:20 Jean-Baptiste Rouquier
  0 siblings, 0 replies; 7+ messages in thread
From: Jean-Baptiste Rouquier @ 2004-08-13 14:20 UTC (permalink / raw)
  To: caml-list

>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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-08-13 14:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-12 14:46 [Caml-list] Managing a polymorphic environment Diego Olivier Fernandez Pons
2004-08-12 15:06 ` Jean-Baptiste Rouquier
2004-08-12 15:19   ` Diego Olivier Fernandez Pons
2004-08-12 17:05 ` brogoff
2004-08-13  8:06   ` Diego Olivier Fernandez Pons
2004-08-13 10:12     ` Diego Olivier Fernandez Pons
2004-08-13 14:20 Jean-Baptiste Rouquier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).