caml-list - the Caml user's mailing list
 help / color / Atom feed
* [Caml-list] phantom types and coercion
@ 2019-10-28 14:27 rixed
  2019-10-28 15:10 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: rixed @ 2019-10-28 14:27 UTC (permalink / raw)
  To: caml-list

Hello list.

I'm trying to use several parameters in a phantom type ; everything works alright, but coercion.
For instance, in the code below, how come s1 can be coerced into a string by s2 cannot ?

# module M : sig type +'a t = private string val m1 : string -> [`C1] t val m2 : string -> ([`C2] * 'a) t end = struct type 'a t = string let m1 s = s let m2 s = s end;;   module M :
  sig
    type +'a t = private string
    val m1 : string -> [ `C1 ] t
    val m2 : string -> ([ `C2 ] * 'a) t
  end
# let s1 = M.m1 "foo";;
val s1 : [ `C1 ] M.t = "foo"
# let s2 = M.m2 "bar";;
val s2 : ([ `C2 ] * 'a) M.t = "bar"
# print_string (s1 :> string);;
foo- : unit = ()
# print_string (s2 :> string);;
Error: This expression cannot be coerced to type string; it has type
         ([ `C2 ] * 'a) M.t
       but is here used with type string


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

* Re: [Caml-list] phantom types and coercion
  2019-10-28 14:27 [Caml-list] phantom types and coercion rixed
@ 2019-10-28 15:10 ` Jacques Garrigue
  2019-10-28 16:45   ` rixed
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2019-10-28 15:10 UTC (permalink / raw)
  To: caml-list

On 28/10/2019 15:27, rixed@happyleptic.org wrote:
> Hello list.
>
> I'm trying to use several parameters in a phantom type ; everything works alright, but coercion.
> For instance, in the code below, how come s1 can be coerced into a string by s2 cannot ?
This is due to the presence of a free type variable in the type of s2.
In that case, a single coercion will just generate a generic supertype, 
and attempt to unify the type of s2 with it.
Your best bet is to write a double coercion.

    (s2 : _ M.t :> string)

Jacques Garrigue
> # module M : sig type +'a t = private string val m1 : string -> [`C1] t val m2 : string -> ([`C2] * 'a) t end = struct type 'a t = string let m1 s = s let m2 s = s end;;   module M :
>    sig
>      type +'a t = private string
>      val m1 : string -> [ `C1 ] t
>      val m2 : string -> ([ `C2 ] * 'a) t
>    end
> # let s1 = M.m1 "foo";;
> val s1 : [ `C1 ] M.t = "foo"
> # let s2 = M.m2 "bar";;
> val s2 : ([ `C2 ] * 'a) M.t = "bar"
> # print_string (s1 :> string);;
> foo- : unit = ()
> # print_string (s2 :> string);;
> Error: This expression cannot be coerced to type string; it has type
>           ([ `C2 ] * 'a) M.t
>         but is here used with type string
>


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

* Re: [Caml-list] phantom types and coercion
  2019-10-28 15:10 ` Jacques Garrigue
@ 2019-10-28 16:45   ` rixed
  0 siblings, 0 replies; 3+ messages in thread
From: rixed @ 2019-10-28 16:45 UTC (permalink / raw)
  To: caml-list

> Your best bet is to write a double coercion.
> 
>     (s2 : _ M.t :> string)

Works indeed. Thank you!

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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-28 14:27 [Caml-list] phantom types and coercion rixed
2019-10-28 15:10 ` Jacques Garrigue
2019-10-28 16:45   ` rixed

caml-list - the Caml user's mailing list

Archives are clonable:
	git clone --mirror http://inbox.vuxu.org/caml-list
	git clone --mirror https://inbox.ocaml.org/caml-list

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.caml-list


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git