caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] an issue with coercing private types
@ 2013-05-08 23:34 Milan Stanojević
  2013-05-09  0:57 ` Markus Mottl
  2013-05-09  3:53 ` Jacques Garrigue
  0 siblings, 2 replies; 4+ messages in thread
From: Milan Stanojević @ 2013-05-08 23:34 UTC (permalink / raw)
  To: Caml List

I have this module

module Foo : sig
  type 'a t = private int
end = struct
  type 'a t = int
end

and then I want to coerce Foo.t into int
let to_int (a : 'a Foo.t) = (a :> int)

this fails to compile with
Error: This expression cannot be coerced to type int; it has type 'a Foo.t
       but is here used with type int

but this compiles fine
let to_int a = (a : 'a Foo.t :> int)

and gives me the right type.

What is wrong with the first version of [to_int]? Is this a bug in the
compiler? I'm using ocaml 4.0.1

I also tried this with Foo.t that doesn't have phantom type variable
and in that case both versions of [to_int] work.

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

* Re: [Caml-list] an issue with coercing private types
  2013-05-08 23:34 [Caml-list] an issue with coercing private types Milan Stanojević
@ 2013-05-09  0:57 ` Markus Mottl
  2013-05-09  2:26   ` Milan Stanojević
  2013-05-09  3:53 ` Jacques Garrigue
  1 sibling, 1 reply; 4+ messages in thread
From: Markus Mottl @ 2013-05-09  0:57 UTC (permalink / raw)
  To: Milan Stanojević; +Cc: Caml List

On Wed, May 8, 2013 at 7:34 PM, Milan Stanojević <milanst@gmail.com> wrote:
> but this compiles fine
> let to_int a = (a : 'a Foo.t :> int)

This seems like the preferred solution and should always work, because
it enforces that "a" has a coercible type (rather than e.g. a mere
type variable) by the time type inference reaches the coercion
operator.  Otherwise it may depend on the order in which the type of
sub-terms is inferred whether coercion will work.

> and gives me the right type.
>
> What is wrong with the first version of [to_int]? Is this a bug in the
> compiler? I'm using ocaml 4.0.1
>
> I also tried this with Foo.t that doesn't have phantom type variable
> and in that case both versions of [to_int] work.

I'm actually surprised that removing the phantom type variable makes a
difference in that respect.  I guess this just shows how unpredictable
the order of type inference can be, hence the need for proper type
annotations with coercions.

Regards,
Markus

--
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com

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

* Re: [Caml-list] an issue with coercing private types
  2013-05-09  0:57 ` Markus Mottl
@ 2013-05-09  2:26   ` Milan Stanojević
  0 siblings, 0 replies; 4+ messages in thread
From: Milan Stanojević @ 2013-05-09  2:26 UTC (permalink / raw)
  To: Markus Mottl; +Cc: Caml List

Right, it seems (a : t1 :> t2) is actually a different operator that,
as you said, should I always work.
I was pointed to
http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#toc53
which explains this is some detail (and I should have searched harder
it in the first place)

Thanks everyone!

On Wed, May 8, 2013 at 8:57 PM, Markus Mottl <markus.mottl@gmail.com> wrote:
> On Wed, May 8, 2013 at 7:34 PM, Milan Stanojević <milanst@gmail.com> wrote:
>> but this compiles fine
>> let to_int a = (a : 'a Foo.t :> int)
>
> This seems like the preferred solution and should always work, because
> it enforces that "a" has a coercible type (rather than e.g. a mere
> type variable) by the time type inference reaches the coercion
> operator.  Otherwise it may depend on the order in which the type of
> sub-terms is inferred whether coercion will work.
>
>> and gives me the right type.
>>
>> What is wrong with the first version of [to_int]? Is this a bug in the
>> compiler? I'm using ocaml 4.0.1
>>
>> I also tried this with Foo.t that doesn't have phantom type variable
>> and in that case both versions of [to_int] work.
>
> I'm actually surprised that removing the phantom type variable makes a
> difference in that respect.  I guess this just shows how unpredictable
> the order of type inference can be, hence the need for proper type
> annotations with coercions.
>
> Regards,
> Markus
>
> --
> Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com

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

* Re: [Caml-list] an issue with coercing private types
  2013-05-08 23:34 [Caml-list] an issue with coercing private types Milan Stanojević
  2013-05-09  0:57 ` Markus Mottl
@ 2013-05-09  3:53 ` Jacques Garrigue
  1 sibling, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2013-05-09  3:53 UTC (permalink / raw)
  To: Milan Stanojević; +Cc: Caml List

On 2013/05/09, at 8:34, Milan Stanojević <milanst@gmail.com> wrote:

> I have this module
> 
> module Foo : sig
>  type 'a t = private int
> end = struct
>  type 'a t = int
> end
> 
> and then I want to coerce Foo.t into int
> let to_int (a : 'a Foo.t) = (a :> int)
> 
> this fails to compile with
> Error: This expression cannot be coerced to type int; it has type 'a Foo.t
>       but is here used with type int
> 
> but this compiles fine
> let to_int a = (a : 'a Foo.t :> int)
> 
> 	and gives me the right type.


The reason one works and not the other is explained here:
http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc76
Namely, when using single coercions from private type abbreviations,
the type of the expression should not contain type variables.

An interesting consequence is that the following is fine:

let to_int (type a) (a : a Foo.t) = (a :> int);;

	Jacques Garrigue

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

end of thread, other threads:[~2013-05-09  3:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-08 23:34 [Caml-list] an issue with coercing private types Milan Stanojević
2013-05-09  0:57 ` Markus Mottl
2013-05-09  2:26   ` Milan Stanojević
2013-05-09  3:53 ` Jacques Garrigue

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