From: Yaron Minsky <yminsky@janestreet.com>
To: caml-list@inria.fr
Subject: [Caml-list] explaining polymorphic variants
Date: Sun, 28 Oct 2012 09:49:46 -0400 [thread overview]
Message-ID: <CACLX4jT1bd-F9oXmQB9ha0fFyHzKFDgp5ehV4YPO2RYFEG-2kg@mail.gmail.com> (raw)
A question that came up in the section of Real World OCaml on
polymorphic variants.
I'm wondering if someone can explain the error in the third definition
below.
# let handle_a `A = "a";;
val handle_a : [< `A ] -> string = <fun>
# let handle_ab (x: [`A | `B ]) =
match x with
|`B -> "b"
| `A as a -> handle_a a
;;
val handle_ab : [ `A | `B ] -> string = <fun>
# let handle_ab' (x: [`A | `B ]) =
match x with
| `B -> "b"
| a -> handle_a a
;;
Characters 86-87:
| a -> handle_a a
^
Error: This expression has type [ `A | `B ]
but an expression was expected of type [< `A ]
The second variant type does not allow tag(s) `B
I understand roughly what's going on here. In the definition of
handle_ab, there is a straightforward syntactic basis for narrowing
the type of the variable a. In the case of handle_ab', that syntactic
basis is no longer available, and so the type of a is not narrowed.
But the question is: why isn't it narrowed based on the other
information available? It seems in theory possible to infer the
narrower type in this case. Does anyone have a satisfying explanation
as to why?
y
next reply other threads:[~2012-10-28 13:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-28 13:49 Yaron Minsky [this message]
2012-10-28 19:46 ` Jacques Le Normand
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=CACLX4jT1bd-F9oXmQB9ha0fFyHzKFDgp5ehV4YPO2RYFEG-2kg@mail.gmail.com \
--to=yminsky@janestreet.com \
--cc=caml-list@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