caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Clarification needed: use of "as" in patterns (with GADTs)
@ 2012-10-19 14:05 Sebastien Furic
  2012-10-20  7:27 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Sebastien Furic @ 2012-10-19 14:05 UTC (permalink / raw)
  To: caml-list

  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.

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

end of thread, other threads:[~2012-10-29 11:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-19 14:05 [Caml-list] Clarification needed: use of "as" in patterns (with GADTs) Sebastien Furic
2012-10-20  7:27 ` Jacques Garrigue
2012-10-29 11:19   ` Sebastien Furic

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