Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 
default.

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
http://www.ffconsultancy.com/products/fsharp_journal/?e