From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id A8C9BBC0A for ; Sun, 6 May 2007 05:45:17 +0200 (CEST) Received: from pih-relay06.plus.net (pih-relay06.plus.net [212.159.14.133]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l463jHMq023086 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Sun, 6 May 2007 05:45:17 +0200 Received: from [80.229.56.224] (helo=beast.local) by pih-relay06.plus.net with esmtp (Exim) id 1HkXgS-0001dq-K4 for caml-list@yquem.inria.fr; Sun, 06 May 2007 04:45:16 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] beginner question: DAGs w/ recursive types an encapsulation of a Map Date: Sun, 6 May 2007 04:39:41 +0100 User-Agent: KMail/1.9.5 References: <20070505025954.71b5a5b5@kerneis.info> <1ffe809c0705052033p6079116fq2a30f5dd942789aa@mail.gmail.com> In-Reply-To: <1ffe809c0705052033p6079116fq2a30f5dd942789aa@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200705060439.41842.jon@ffconsultancy.com> X-Miltered: at discorde with ID 463D4F4D.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; recursive:01 recursive:01 ocaml:01 -rectypes:01 -rectypes:01 node:01 node:01 compiler:01 ocaml:01 variants:01 val:01 fsharp:01 frog:98 compilers:01 polymorphic:01 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