caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Transforming of ASTs and polymorphic variants
@ 2007-04-25  4:19 Joel Reymont
  2007-04-25  4:56 ` Joel Reymont
  2007-04-25  5:54 ` [Caml-list] " skaller
  0 siblings, 2 replies; 5+ messages in thread
From: Joel Reymont @ 2007-04-25  4:19 UTC (permalink / raw)
  To: Caml List

My apologies if this is a beginners question...

I have two ASTs in different modules that look very much alike in  
that some constructors have the same name and type name. When I  
transform that ASTs I end up "re-applying" the similarly named  
constructor and arguments. This is best explained with an example.

a.ml:

type typ =
   | Integer
   | Double
   | String
   | Boolean

b.ml:

type ty =
   | Integer
   | Double
   | String
   | Boolean
   | Void

A transformation function would be

let conv_type = function
   | A.Integer -> B.Integer
   | A.String -> B.String
   | A.Boolean -> B.Boolean

I don't want to share types between ASTs since, like above, some of  
them may be supersets of others. How can I improve this with  
polymorphic variants?

	Thanks, Joel

--
http://wagerlabs.com/






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Transforming of ASTs and polymorphic variants
  2007-04-25  4:19 Transforming of ASTs and polymorphic variants Joel Reymont
@ 2007-04-25  4:56 ` Joel Reymont
  2007-04-25  5:54 ` [Caml-list] " skaller
  1 sibling, 0 replies; 5+ messages in thread
From: Joel Reymont @ 2007-04-25  4:56 UTC (permalink / raw)
  To: Caml List

I also tend to write "macros" to lessen the pain of typing up the  
ASTs in my unit tests.

I may have in both a.ml and b.ml (contrived example)

type expr = VarIdent of string

I then end up with duplicate let id x = VarIdent x in both a.ml and  
b.ml since A.expr and B.expr are of a different type. It would be  
great if I could write id once and have it work for both A.expr and  
B.expr.

	Thanks, Joel

--
http://wagerlabs.com/






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Transforming of ASTs and polymorphic variants
  2007-04-25  4:19 Transforming of ASTs and polymorphic variants Joel Reymont
  2007-04-25  4:56 ` Joel Reymont
@ 2007-04-25  5:54 ` skaller
  2007-04-25  5:59   ` Joel Reymont
  1 sibling, 1 reply; 5+ messages in thread
From: skaller @ 2007-04-25  5:54 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On Wed, 2007-04-25 at 05:19 +0100, Joel Reymont wrote:
> My apologies if this is a beginners question...
> 
> I have two ASTs in different modules that look very much alike in  
> that some constructors have the same name and type name. When I  
> transform that ASTs I end up "re-applying" the similarly named  
> constructor and arguments. This is best explained with an example.
a.ml:

type typ = [
   | `Integer
   | `Double
   | `String
   | `Boolean
]

b.ml:

type ty = [
   | `Integer
   | `Double
   | `String
   | `Boolean
   | `Void
]

A transformation function would be

let conv_type (x:A.typ): B.ty = match x with
   | x -> (x : A.typ :> B.ty)

A simpler implementation:

let conv_type x = (x : A.typ :> B.ty)

will also work in this case.



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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Transforming of ASTs and polymorphic variants
  2007-04-25  5:54 ` [Caml-list] " skaller
@ 2007-04-25  5:59   ` Joel Reymont
  2007-04-25  6:28     ` Martin Jambon
  0 siblings, 1 reply; 5+ messages in thread
From: Joel Reymont @ 2007-04-25  5:59 UTC (permalink / raw)
  To: skaller; +Cc: Caml List

John,

> let conv_type x = (x : A.typ :> B.ty)
>
> will also work in this case.

Thanks for the tip!

Can you explain the following expression?

(x : A.typ :> B.ty)

Where can I find more on :>?

	Thanks, Joel

--
http://wagerlabs.com/






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Transforming of ASTs and polymorphic variants
  2007-04-25  5:59   ` Joel Reymont
@ 2007-04-25  6:28     ` Martin Jambon
  0 siblings, 0 replies; 5+ messages in thread
From: Martin Jambon @ 2007-04-25  6:28 UTC (permalink / raw)
  To: Joel Reymont; +Cc: skaller, Caml List

On Wed, 25 Apr 2007, Joel Reymont wrote:

> John,
>
>> let conv_type x = (x : A.typ :> B.ty)
>> 
>> will also work in this case.
>
> Thanks for the tip!
>
> Can you explain the following expression?
>
> (x : A.typ :> B.ty)

Basically you would use :> to convert the type of a value to a type which 
is more general. With polymorphic variants it allows you to "add" cases 
and with objects it allows you to "remove" methods.


Martin

--
Martin Jambon
http://martin.jambon.free.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-04-25  6:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-25  4:19 Transforming of ASTs and polymorphic variants Joel Reymont
2007-04-25  4:56 ` Joel Reymont
2007-04-25  5:54 ` [Caml-list] " skaller
2007-04-25  5:59   ` Joel Reymont
2007-04-25  6:28     ` Martin Jambon

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).