caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: "Milan Stanojević" <milanst@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] polymorphic variants in match statements
Date: Sat, 11 Feb 2012 10:14:05 +0900	[thread overview]
Message-ID: <D2A911AD-1F1F-4A56-B1DD-5AEC2CACBBD1@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAKR7PS_FT3NCq5YDSfFP7P80s=xSJNr=Ehz0dZ7vsiOEGE4iCA@mail.gmail.com>

On 2012/02/11, at 7:20, Milan Stanojević wrote:

> A small follow-up question
> 
> Polymorphic variant types are not allowed to have non-regular
> recursion, i.e. the following type definition gives an error
> 
> type 'a t = [ `A of 'a | `B of ('a * 'a) t ]
> 
> Error: In the definition of t, type ('a * 'a) t should be 'a t
> 
> I guess the reason why this is not allowed has something to do with
> our previous discussion, but I'm struggling to connect the dots.
> 
> Can you please explain this restriction?

Actually, this is not directly related.
The problem here is with type inference of recursive types.
To do that, we need to do type inference on (possibly infinite) recursive trees.
This is easy for regular trees (which can be represent as a directed graph),
as the usual algorithm for first-order unification works.
So what ocaml does is building such a regular tree on the fly, by creating a
back edge to a previous ['a t] node when it meets another ['b t] node below it.

So for instance, with definition
  type 'a t = [ `A of 'a | `B of 'a t ]

the type [int t] would expand to ([`A of int | `B of 'r] as 'r)

However, for this to work, any occurrence of t inside its own definition
should have identical parameters. Hence the enforced restriction.

	Jacques Garrigue

      reply	other threads:[~2012-02-11  1:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-24  0:53 Milan Stanojević
2012-01-24  1:21 ` Jacques Garrigue
2012-01-24 17:42   ` Milan Stanojević
2012-01-25  1:21     ` Jacques Garrigue
2012-02-10 22:20     ` Milan Stanojević
2012-02-11  1:14       ` Jacques Garrigue [this message]

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=D2A911AD-1F1F-4A56-B1DD-5AEC2CACBBD1@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=milanst@gmail.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).