Version française
Home     About     Download     Resources     Contact us    
Browse thread
Extracting common information
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Extracting common information
On Thu, 2007-05-24 at 13:04 +0200, Niko Matsakis wrote:
> Hello,

> > type type_tree = Primitive of string | Pointer of type_tree
> > and  expr_tree = Unary of string * expr_tree |
> >                  Binary of string * expr_tree * expr_tree

> This seemed like it should work reasonably well, but then I realized  
> I would want to thread along some annotation to store the types, line  
> numbers, and things like that.  Since these annotations will change  
> between compiler passes, I thought I would use a variant type like so:
> 
> > type 'ud type_tree = Primitive of 'ud * string |
> >                      Pointer of 'ud * 'ud type_tree
> > and 'ud expr_tree = Unary of 'ud * string * 'ud expr_tree |
> >                     Binary of 'ud * string * 'ud expr_tree * 'ud  
> > expr_tree
> > etc
> 
> but then I ran into the problem that I sometimes want to be able to  
> extract the user data without reference to the type of tree involved,  
> and I can see no easy way to do that beyond:
> 
> > let ud_type type =
> >   match type with
> >     Primitive(ud,_) ud |
> >     Pointer(ud,_) ud
> > etc
> 
> Obviously, that is unappealing.

That doesn't matter: what you have done is the best way, except
the type for source references should be concrete, not a parameter.

Although you could factor this by separating the source
location from the term .. that's a bad idea because you will
be tempted to throw out the source location when analysing
the terms .. which prevents you emitting good errors.

Putting the source location directly in the terms FORCES
you to calculate a reasonable location for every term you
synthesise by a rewriting rule (via the type system), 
which is a good thing.

There is another way though .. just add a term like:

	`Source of src_loc * tree

which says 'my child was produced from this source location'.
I don't recommend this though, precisely because it can be used
wherever you like .. or not used at all.


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net