caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Baptiste Rouquier <jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr>
To: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] Managing a polymorphic environment
Date: Fri, 13 Aug 2004 16:20:16 +0200 (CEST)	[thread overview]
Message-ID: <1092406816.411cce207d09b@mouette.ens-lyon.fr> (raw)

>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


             reply	other threads:[~2004-08-13 14:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-13 14:20 Jean-Baptiste Rouquier [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-08-12 14:46 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1092406816.411cce207d09b@mouette.ens-lyon.fr \
    --to=jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).