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: Tue, 24 Jan 2012 10:21:43 +0900 [thread overview]
Message-ID: <2FA31185-B4C7-48D3-8C3C-771756CB26AA@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAKR7PS95d1vL3smxojbMoGj=tXgm8Xu52PUKmfodDyFSRH+QzA@mail.gmail.com>
On 2012/01/24, at 9:53, Milan Stanojević wrote:
> Hi, we're trying to understand the type inference with polymorphic
> variants in match statements. This is a simplification of an actual
> case that happened in practice.
>
> 1)
> let f i a =
> match i, a with
> | true, `A -> `B
> | false, x -> x
>
> fails with
> File "foo.ml", line 4, characters 16-17:
> Error: This expression has type [< `A ]
> but an expression was expected of type [> `B ]
> The first variant type does not allow tag(s) `B
>
> 2) changing false to _
> let f i a =
> match i, a with
> | true, `A -> `B
> | _, x -> x
>
> this succeeds with
> val f : bool -> ([> `A | `B ] as 'a) -> 'a
>
> 3) changing x in (1) to _ , and using a on the right side
> let f i a =
> match i, a with
> | true, `A -> `B
> | false, _ -> a
>
> this fails in the same way as (1)
>
> 4) finally adding another case to match statement
> let f i a =
> match i, a with
> | true, `A -> `B
> | false, x -> x
> | true, x -> x
>
> this succeeds with the same type as (2)
>
>
> So it seems there is some interaction between type inference and
> exhaustivnest of the match statements.
>
> Can someone shed some light on what is going on here?
Indeed. The basic idea is to close variant types when leaving them
open would make the pattern matching non-exhaustive.
Here, if we assume that a has type [`A | `B], then the pattern-matching
becomes non-exhaustive, so the type inferred is just [`A]
(i.e. the list of all constructors appearing inside the patterns at this position).
Actually, the theory is a bit more complicated, and the full details are
in the following paper, but you should just expect the above behavior
in practice.
Typing deep pattern-matching in presence of polymorphic variants.
http://www.math.nagoya-u.ac.jp/~garrigue/papers/index.html
Note that there is also another way to make (1) type, without adding
new cases
let f i a =
match i, a with
| true, `A -> `B
| false, (`A as x) -> x;;
val f : bool -> [< `A ] -> [> `A | `B ] = <fun>
Here we have removed the connection between a and the output,
allowing `A to be combine with `B without changing the type of a.
Hope this helps.
Jacques Garrigue
next prev parent reply other threads:[~2012-01-24 1:21 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 [this message]
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
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=2FA31185-B4C7-48D3-8C3C-771756CB26AA@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).