English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
beginner question: DAGs w/ recursive types an encapsulation of a Map
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-06 (03:45)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] beginner question: DAGs w/ recursive types an encapsulation of a Map
On Sunday 06 May 2007 04:33, Pablo Polvorin wrote:
> Sorry, may be a stupid question, but i dont get it.
> What is the difference between the first and second solution that you
> propose, that make the second less safe?.
> As far as i understand, both are recursive type declarations. I do read
> some post related to this topic.. but fail to fully understand it.
> what i missing here?

OCaml has a -rectypes option that relaxes type checking and allows more types 
though. This relaxation is usually undesirable because it results in 
obfuscated error messages for some type errors, so -rectypes is off by 

However, you have struck upon a case where it could be useful.

Rather than defining your tree type as:

# type tree = Node of int * (char * tree) list ;;
type tree = Node of int * (char * tree) list

with -rectypes you can define it as:

# type tree' =  int  * (char * tree') list ;;
type tree' = int * (char * tree') list

This is not "unsafe" in the usual sense of the word but it means that the 
compiler will be less friendly on all of the rest of your code. Also, I have 
had the OCaml compilers crash in the past if you try to mix code compiled 
with -rectypes with code without.

Another solution is to use polymorphic variants:

# let rec tree = `Node(3, ['a', tree]);;
val tree : [> `Node of int * (char * 'a) list ] as 'a = ...

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal