caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] explaining polymorphic variants
@ 2012-10-28 13:49 Yaron Minsky
  2012-10-28 19:46 ` Jacques Le Normand
  0 siblings, 1 reply; 2+ messages in thread
From: Yaron Minsky @ 2012-10-28 13:49 UTC (permalink / raw)
  To: caml-list

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] explaining polymorphic variants
  2012-10-28 13:49 [Caml-list] explaining polymorphic variants Yaron Minsky
@ 2012-10-28 19:46 ` Jacques Le Normand
  0 siblings, 0 replies; 2+ messages in thread
From: Jacques Le Normand @ 2012-10-28 19:46 UTC (permalink / raw)
  To: Yaron Minsky; +Cc: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-10-28 19:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-28 13:49 [Caml-list] explaining polymorphic variants Yaron Minsky
2012-10-28 19:46 ` Jacques Le Normand

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).