caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ben Millwood <bmillwood@janestreet.com>
To: Romain Bardou <romain@cryptosense.com>
Cc: "caml users" <caml-list@inria.fr>,
	"Daniel Bünzli" <daniel.buenzli@erratique.ch>
Subject: Re: [Caml-list] Cyclic type abbreviation
Date: Mon, 16 Nov 2015 13:43:42 +0800	[thread overview]
Message-ID: <CA+MHO52JaupiCFhOxW626deYSuLF-REr89muX9ngxghnijQSsQ@mail.gmail.com> (raw)
In-Reply-To: <5645F28B.9000708@cryptosense.com>

[-- Attachment #1: Type: text/plain, Size: 3115 bytes --]

To be clear, options aren't being handled differently from any other
*pre-existing* user-defined type.

utop # type 'a my_type = One of 'a | Two of 'a * 'a;;
type 'a my_type = One of 'a | Two of 'a * 'a
utop # type node = int * tree * tree and tree = node my_type;;
Error: The type abbreviation node is cyclic

Using a real ADT instead of a type abbrevation works. David Allsopp already
gave one way you can do it, here is another which preserves the use of the
option type:

utop # type node = Node of int * tree * tree and tree = node option;;
type node = Node of int * tree * tree
and tree = node option

Note that here [tree] is still a type abbreviation, but [node] is not, so
the cycle is not a problem.

Note also that your original example works fine with the -rectypes flag.
But my general opinion is that if you need -rectypes to compile your code,
you should write different code :)

On 13 November 2015 at 22:24, Romain Bardou <romain@cryptosense.com> wrote:

> On 13/11/2015 14:46, Romain Bardou wrote:
>
>> On 13/11/2015 14:37, David Allsopp wrote:
>>
>>> Mr. Herr wrote:
>>>
>>>> On 13.11.2015 13:49, Christoph Höger wrote:
>>>>
>>>>> Dear all,
>>>>>
>>>>> why is this type cyclic?
>>>>>
>>>>> type node = int * tree * tree
>>>>>   and tree = node option
>>>>>
>>>>> I cannot introduce a manifest for the option type, as there is no
>>>>> Option module (why is that, btw?) - so I would assume option to be
>>>>> special enough to be handled like any other algebraic data type.
>>>>>
>>>> type 'a option = None | Some 'a
>>>>
>>>> no need for a module, just a simple type. Maybe you confound it with
>>>> other
>>>> languages.
>>>>
>>>> And cyclic - well, the types are referring to each other.
>>>>
>>>> Summary: what is supposedly wrong with it?
>>>>
>>>
>>> I expect that what is wrong is that you can write:
>>>
>>> type node = int * tree * tree
>>>   and tree = Some of node
>>>            | None
>>>
>>> I don't know why you can't write [and tree = node option] instead.
>>>
>>>
>>> David
>>>
>>>
>> Interestingly, this definition is accepted:
>>
>> type tree = (int * 'a * 'a) option as 'a
>>
>> Here you are helping the type-checker because you give it a "canonical"
>> representation that it can use when unifying; it no longer has to expand
>> the type, potentially infinitely. I think the main point is that the
>> type is isorecursive, but I'm not really an expert on the subject.
>>
>>
> My bad, I had actually redefined the option type as a polymorphic variant
> before:
>
> type 'a option = [ `None | `Some of 'a ]
>
> and I forgot about it when I tested the definition of tree above.
>
> So yeah you can do that with polymorphic variants even though they are
> kind of type abbreviations.
>
> Thanks to Daniel for pointing this out.
>
> --
> Romain
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 4573 bytes --]

  reply	other threads:[~2015-11-16  5:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-13 12:49 Christoph Höger
2015-11-13 13:15 ` Mr. Herr
2015-11-13 13:37   ` David Allsopp
2015-11-13 13:46     ` Romain Bardou
2015-11-13 14:24       ` Romain Bardou
2015-11-16  5:43         ` Ben Millwood [this message]
2015-11-16 16:42       ` peterfrey
2015-11-16 17:08         ` Pierrick Couderc
2015-11-13 13:46     ` Christoph Höger
2015-11-13 13:53     ` Mr. Herr

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=CA+MHO52JaupiCFhOxW626deYSuLF-REr89muX9ngxghnijQSsQ@mail.gmail.com \
    --to=bmillwood@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    --cc=romain@cryptosense.com \
    /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;
as well as URLs for NNTP newsgroup(s).