From: Sebastien Furic <programming.languages@furic.org>
To: caml-list@inria.fr
Subject: [Caml-list] Clarification needed: use of "as" in patterns (with GADTs)
Date: Fri, 19 Oct 2012 16:05:52 +0200 [thread overview]
Message-ID: <50815E40.5080601@furic.org> (raw)
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)?
Cheers,
Sébastien.
next reply other threads:[~2012-10-19 14:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-19 14:05 Sebastien Furic [this message]
2012-10-20 7:27 ` Jacques Garrigue
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=50815E40.5080601@furic.org \
--to=programming.languages@furic.org \
--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;
as well as URLs for NNTP newsgroup(s).