caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Le Normand <rathereasy@gmail.com>
To: Yaron Minsky <yminsky@janestreet.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] explaining polymorphic variants
Date: Sun, 28 Oct 2012 12:46:22 -0700	[thread overview]
Message-ID: <CAK0y-35yry6QJmNAwrA2i0=6rbjcLnnmnHoyybi=RJRZ8QYe0w@mail.gmail.com> (raw)
In-Reply-To: <CACLX4jT1bd-F9oXmQB9ha0fFyHzKFDgp5ehV4YPO2RYFEG-2kg@mail.gmail.com>

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

Well, for one, the patterns are typed independently of the type of the
guard. For two, this wouldn't work for nested patterns.
Do we want to make an exception for the case when there are only simple
patterns and when the type of the guard has been fully determined? I don't
know if it's worth the trouble, especially when it can usually be handled
with the # operator.
On Oct 28, 2012 6:50 AM, "Yaron Minsky" <yminsky@janestreet.com> wrote:

> 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
>
> --
> 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: 2695 bytes --]

      reply	other threads:[~2012-10-28 19:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-28 13:49 Yaron Minsky
2012-10-28 19:46 ` Jacques Le Normand [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='CAK0y-35yry6QJmNAwrA2i0=6rbjcLnnmnHoyybi=RJRZ8QYe0w@mail.gmail.com' \
    --to=rathereasy@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=yminsky@janestreet.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).