caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Diego Olivier Fernandez Pons <Diego.FERNANDEZ_PONS@etu.upmc.fr>
To: brogoff <brogoff@speakeasy.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Managing a polymorphic environment
Date: Fri, 13 Aug 2004 10:06:26 +0200 (DST)	[thread overview]
Message-ID: <Pine.A41.4.44.0408130937130.524432-100000@ibm1> (raw)
In-Reply-To: <Pine.LNX.4.58.0408120942030.22505@shell2.speakeasy.net>

    Bonjour,

> I'm sure I'm completely misunderstanding your problem, but how about
> referring to the environment variables by by strings and attaching
> properties to them, using the property list trick that Xavier
> described here a few years ago

What I had in mind is basing the environment in the polymorphic
physical equality (==) - and therefor I said the environment was
expected to be small and hence could use an equality based set
representation

module Env = struct
  type 'a t = ('a * string) list
  let empty : 'a t = []
  let add : 'a -> string -> 'a t -> 'a t = fun x s l -> (x, s) :: l
  let rec get_string : 'a -> 'a t -> string = fun x l ->
    match l with
      | [] -> ""
      | (y, s) :: tail -> if x == y then s else get_string x tail
end

# let env = Env.empty;;
val env : 'a Env.t = []
# let x = 5;;
val x : int = 5
# let y = 3;;
val y : int = 3
# let env = Env.add x "x" env;;
val env : int Env.t = [(5, "x")]
# let env = Env.add y "y" env;;
val env : int Env.t = [(3, "y"); (5, "x")]
# Env.get_string x env;;
- : string = "x"
# Env.get_string y env;;
- : string = "y"

The problem is to make Env forget it is typed because the
polymorphic parameter 'a captures the first instanciated type
(int in my example) even if (==) is polymorphic.

I solve it below with some magic.

My code seems - at least to me - correctly typed. Why isn't 'a Env.t
'totally' polymorphic or is it polymorphic in a 'more general' sense ?
It does not seem able to raise a runtime error, does it ?

module Env = struct
  type t
  let empty : t = Obj.magic []
  let add : 'a -> string -> t -> t = fun x s l ->
    let l' : ('a * string) list = Obj.magic l in
    let l'' = (x, s) :: l' in
    let l''' : t = Obj.magic l'' in
      l'''
  let rec get_string : 'a -> t -> string = fun x l ->
    let l' : ('a * string) list = Obj.magic l in
      match l' with
	| [] -> ""
	| (y, s) :: _ when x == y -> s
	| _ :: tail ->
	    let tail' : t = Obj.magic tail in
	      get_string x tail'
end

module Env :
  sig
    type t
    val empty : t
    val add : 'a -> string -> t -> t
    val get_string : 'a -> t -> string
  end

Example :

# let env = Env.empty;;
val env : Env.t = <abstr>
# let x = 5;;
val x : int = 5
# let y = 3.0;;
val y : float = 3.
# let env = Env.add x "an int" env;;
val env : Env.t = <abstr>
# let env = Env.add y "a float" env;;
val env : Env.t = <abstr>
# Env.get_string x env;;
- : string = "an int"
# Env.get_string y env;;
- : string = "a float"


        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


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

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2004-08-13 10:12     ` Diego Olivier Fernandez Pons
2004-08-13 14:20 Jean-Baptiste Rouquier

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=Pine.A41.4.44.0408130937130.524432-100000@ibm1 \
    --to=diego.fernandez_pons@etu.upmc.fr \
    --cc=brogoff@speakeasy.net \
    --cc=caml-list@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).