caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Phantom types and variants
@ 2014-08-22  3:11 Daniel Bünzli
  2014-08-22  3:17 ` Daniel Bünzli
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Bünzli @ 2014-08-22  3:11 UTC (permalink / raw)
  To: Caml-list

Hello,  

I'm pretty sure I have used the following pattern in the past. But the `coerce` function doesn't type. It's quite strange since the type variable is a phantom type so it should not enforce anything. Any hints ? 

--------
module M : sig
  type kind = [ `A | `B ]
  type +'a t constraint 'a = [< kind ]

  val a : unit -> [> `A] t
  val b : unit -> [> `B] t
  val coerce : ([> kind] as 'a) -> 'b t -> 'a t
end = struct                                                                                         
  type kind = [ `A | `B ]                                                                            
  type +'a t =                                                                                       
    { kind : kind }                                                                                  
    constraint 'a = [< kind]                                                                         
                                                                                                     
  let a () = { kind = `A }                                                                           
  let b () = { kind = `B }                                                                           
  let coerce k v = if v.kind <> k then invalid_arg "" else v                                         
end


let (vl : [`A | `B ] M.t list) = [M.a (); M.b ()]
let (v : [`A] M.t) = M.coerce `A (List.hd vl)


--------

Fails with:

Values do not match:
val coerce : kind -> ([< kind ] as 'a) t -> 'a t
is not included in
val coerce : kind -> [< kind ] t -> kind t



Best,

Daniel 



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

end of thread, other threads:[~2014-08-23 15:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-22  3:11 [Caml-list] Phantom types and variants Daniel Bünzli
2014-08-22  3:17 ` Daniel Bünzli
2014-08-22 19:49   ` Philippe Veber
2014-08-23 15:06     ` Daniel Bünzli
2014-08-23 15:40   ` Jeremy Yallop
2014-08-23 15:48     ` Daniel Bünzli

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