caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Sebastien Furic <programming.languages@furic.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Clarification needed: use of "as" in patterns (with GADTs)
Date: Sat, 20 Oct 2012 16:27:02 +0900	[thread overview]
Message-ID: <565CA144-D2B0-45D5-8C2C-845610AE860A@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <50815E40.5080601@furic.org>

On 2012/10/19, at 23:05, Sebastien Furic <programming.languages@furic.org> wrote:

> Hello,
> 
> Would someone be kind enough to explain me what's going on with the following code:
> 
> type empty
> and nonempty
> type ('a, _) my_list =
>  | Nil: ('a, empty) my_list
>  | Cons: 'a * ('a, 'b) my_list -> ('a, nonempty) my_list
> 
> (* Works fine *)
> let rec max = function
>  | Cons (x, Nil) -> x
>  | Cons (x, Cons (x', xs)) when x <= x' -> max (Cons (x', xs))
>  | Cons (x, Cons (_, xs)) -> max (Cons (x, xs))
> 
> (* Fails *)
> let rec max = function
>  | Cons (x, Nil) -> x
>  | Cons (x, (Cons (x', _) as xs)) when x <= x' -> max xs
>  | Cons (x, Cons (_, xs)) -> max (Cons (x, xs));;
> 
> Characters 97-99:
>  | Cons (x, (Cons (x', _) as xs)) when x <= x' -> max xs
>                                                       ^^
> Error: This expression has type ('a, nonempty) my_list
>       but an expression was expected of type ('a, nonempty) my_list
>       This instance of nonempty is ambiguous:
>       it would escape the scope of its equation
> 
> I remember having seen similar issues in the past, involving "as" and polymorphic variants (but I can't find it in the archives). Is it the same issue? Why does Ocaml need to "break the continuity" of types in presence of "as"?
> BTW, what is the recommended way to write the code above (I want to avoid having to reconstruct the list)?

Actually, this is not the same issue: the problem here is related to ambiguity inference, which in the case of OCaml is required for soundness in presence of GADTs.
What happens here is that you Cons constructor introduces an existential variable, which is immediately forced to nonempty by matching against the nested Cons. However is inferred as using this existential variable.
When typing the recursive call the existential variable is forced to expand to nonempty, but as a result of this expansion it is marked as ambiguous.

When you get such an error message, there is an easy solution: add a type annotation on the faulty expression, using exactly the printed type:

   max (xs : ('a, nonempty) my_list)

This is enough to make this program accepted.

It could be argued that in this case there is no ambiguity, since the existential cannot be exported anyway.
I'll look into that, but you must keep in mind that we must be very careful, as soundness is at stake.

Jacques Garrigue

  reply	other threads:[~2012-10-20  7:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-19 14:05 Sebastien Furic
2012-10-20  7:27 ` Jacques Garrigue [this message]
2012-10-29 11:19   ` Sebastien Furic

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=565CA144-D2B0-45D5-8C2C-845610AE860A@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=programming.languages@furic.org \
    /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).