Version française
Home     About     Download     Resources     Contact us    
Browse thread
[newbie] Define and use records in sum types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe Filliatre <filliatr@c...>
Subject: Re: [newbie] Define and use records in sum types

> I have problems with basic record and sum types use.
> 
> I would like to define a sum type with records as constructor
> parameters, records with the same field names. 
> 
> I have tried the follwing definition:
> 
> <ocaml>
> # type asp = A of { name : string } | B of { name : string ; ext : int };;
>                   -
> Syntax error
> </ocaml>

I don't know what you want to do exactly, but I applied to following
idea (which you can find in the ocaml sources) for AST tagged with
common informations:

type t = { name : string; info : u }
 and u = A
       | B of int

(in the case of AST, t and u are really mutually recursive, with
occurrences of t in the arguments of u's constructors)

Then you can still make pattern-matching like this
 
   match x.info with A -> ... | B n -> ...

and still access the name field with x.name.

Hops this helps,
-- 
Jean-Christophe Filliatre    
  Computer Science Laboratory   Phone (650) 859-5173
  SRI International             FAX   (650) 859-2844
  333 Ravenswood Ave.           email filliatr@csl.sri.com
  Menlo Park, CA 94025, USA     web   http://www.csl.sri.com/~filliatr