From: "Török Edwin" <edwin+ml-ocaml@etorok.net>
To: Leo White <lpw25@cam.ac.uk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] strange type inference for polymorphic variants
Date: Mon, 07 Jan 2013 14:48:41 +0200 [thread overview]
Message-ID: <50EAC429.1020103@etorok.net> (raw)
In-Reply-To: <Prayer.1.3.5.1301071128570.25079@hermes-1.csi.cam.ac.uk>
On 01/07/2013 01:28 PM, Leo White wrote:
> Hi
>
> This is an interesting example of one the problems with how polymorphic variants are implemented in OCaml.
>
> However, firstly I must address your confusion over the meaning of [< `A of a | `B of a > `A ]. The "> `A" here means that the type must include an `A tag. It is a lower bound for the variant types
> that can be used. Since the upper bound of the type ("< `A of a | `B of a") already tells us what type the `A tag must have there is no need to include that information in the lower bound.
Thanks, I didn't see this explain in the manual.
>
> The problem here is with how OCaml handles matches with default cases. Given the code:
>
> match foo with
> `Bar x -> x + 1
> | _ -> 0
>
> OCaml will conclude that foo has the type [> `Bar of int]. This means that foo must have a type that includes a Bar tag, since Bar is in the lower bound of the type.
>
> Conversly, given the code:
>
> match foo with
> `Bar x -> x +1
> | `Foo -> 0
>
> OCaml will conclude that foo has the type [< `Bar of int | `Foo ]. This means that foo does not have to have a type that includes a Bar tag, since Bar is only part of the upper bound.
>
> This is why your example includes a spurious "> `A". The match gives "a" the type [> `A of a], while the use of "generic" gives "a" the type [< `A of a | `B of a], when these are unified they become
> [< `A of a | `B of a > `A].
Thanks, that type makes sense now.
Could the compiler be improved to realize that [< `A of a | `B of a > `A] and [< `A of a | `B of a] are the same type?
>
>
> Interestingly, this problem is actually due to the syntax of OCaml. The formal system on which the implementation of polymorphic variants is based (see Section 6 of "Programming with Polymorphic
> Variants" by Jacques Garrique) is capable of expressing the type that a match with a default case should have. However, the OCaml syntax has no means to express this type
Will read.
>
> In the syntax used in that paper, the example I gave above should actually have the type [0 < T | Bar: int]. In other words, "foo" can have any variant tags (there are essentially no lower or upper
> bounds), but if it has a Bar tag then that tag has an int type.
>
> I don't think that it would be difficult to use such a type within OCaml, but as I said the syntax has no means to express it.
>
> This is also the reason why a type such as [ `A of int or float ] (analagous to the [ `A of int & float ] that OCaml does support) can not be supported in OCaml.
>
> Personally, I wouldn't mind replacing the current polymorphic variant syntax with a more expressive one (and then slowly depreciating the old one). However, I imagine most people would consider this
> too large a change for too small a gain.
Best regards,
--Edwin
next prev parent reply other threads:[~2013-01-07 12:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-04 16:36 Török Edwin
2013-01-07 11:28 ` Leo White
2013-01-07 12:48 ` Török Edwin [this message]
2013-01-08 5:19 ` Jacques Garrigue
2013-01-13 17:58 ` Török Edwin
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=50EAC429.1020103@etorok.net \
--to=edwin+ml-ocaml@etorok.net \
--cc=caml-list@inria.fr \
--cc=lpw25@cam.ac.uk \
/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