Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Chris Hecker <checker@d6.com>
Cc: Miles Egan <miles@caddr.com>, caml-list@inria.fr
Subject: Re: [Caml-list] lisp -> ocaml
Date: Mon, 28 May 2001 15:32:56 +0200	[thread overview]
Message-ID: <20010528153256.C21783@pauillac.inria.fr> (raw)
In-Reply-To: <4.3.2.7.2.20010527170201.02754820@shell16.ba.best.com>; from checker@d6.com on Sun, May 27, 2001 at 05:06:12PM -0700

> 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


  reply	other threads:[~2001-05-28 13:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-22 16:33 Miles Egan
2001-05-22 16:52 ` Markus Mottl
2001-05-22 17:09 ` Brian Rogoff
2001-05-22 18:27 ` Remi VANICAT
2001-05-22 17:12   ` Miles Egan
2001-05-22 18:26     ` Chris Hecker
2001-05-27 13:01 ` Xavier Leroy
2001-05-27 14:43   ` Miles Egan
2001-05-27 21:24   ` Dave Mason
2001-05-28 17:10     ` William Chesters
2001-05-28  0:06   ` Chris Hecker
2001-05-28 13:32     ` Xavier Leroy [this message]
2001-05-28 19:02       ` Chris Hecker

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=20010528153256.C21783@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    --cc=miles@caddr.com \
    /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