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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] lisp -> ocaml
> Right, I just meant in a program known to be running under the
> toplevel, would it be possible to use the typing information that's
> already there?  Those functions aren't available right now, but I
> don't see why they couldn't be.

The "print" function cannot be a regular function because it needs
both the value to be printed and the (representation of) the type of
this value.  In other terms, just having
        val print: 'a -> unit
isn't enough, because the actual type of the argument would not be
known to the print function.


> > val new_property: unit -> 'a property
> >val put_property: atom -> 'a property -> 'a -> unit
> >val get_property: atom -> 'a property -> 'a
> >...
> 
> I'm confused by what this code is trying to do.  Maybe I don't know
> enough about lisp to know how this code maps to it, but what's an
> example code snippet you'd use this code for?

Property lists in classic Lisp are association lists attached to
atoms.  They provide a simple way to attach information to atoms.
The keys of the a-list are arbitrary Lisp values (I think) but in
practice are generally atoms or integers.  The associated data are
arbitrary Lisp values.

The natural way to handle this in Caml is to parameterize the type of
atoms by the types of the keys and data of the property list:

type ('a, 'b) atom = { name : string;
                       mutable properties : ('a * 'b) list }

but this constraints quite a lot the data that can be put in property
lists.  If you're translating old Lisp code verbatim, it may be a
problem.

The mysterious code I sent works around this limitation by allowing
data of several types to be stored in property lists, without being
reflected in the type of atoms.  Of course, to preserve type safety,
property keys need to be typed, so that when we recover a data from a
property list, its type is known.

Here is how you'd use all this.  Start with Lisp code where property A
can be associated with integers or strings, and property B can be
associated with booleans.  You'd first define three Caml properties
and assign them types:

   let prop_A_int = (new_property : int property)
   let prop_A_string = (new_property : string property)
   let prop_B = (new_property : bool property)

Then you can add properties to any atom:

  put_property atom prop_A_int 12;
  put_property atom prop_B true;
  put_property atom2 prop_A_string "Hello"

And recover the values in a type-safe way:

  get_property atom prop_A_int  (--> type int, value 12 )
  get_property atom prop_A_string ( --> type string, raises Not_found)

Silly trick, but I find this quite enjoyable.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr