Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
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	[thread overview]
Message-ID: <200705060439.41842.jon@ffconsultancy.com> (raw)
In-Reply-To: <1ffe809c0705052033p6079116fq2a30f5dd942789aa@mail.gmail.com>

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


  reply	other threads:[~2007-05-06  3:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-04 23:46 Justin Corn
2007-05-05  0:59 ` [Caml-list] " Gabriel Kerneis
2007-05-06  3:33   ` Pablo Polvorin
2007-05-06  3:39     ` Jon Harrop [this message]
2007-05-06  7:54       ` Alain Frisch
2007-05-06  8:05         ` skaller
2007-05-05  7:10 ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200705060439.41842.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox