* [Caml-list] First class modules sub-typing
@ 2014-10-21 7:49 Thomas Braibant
2014-10-21 8:12 ` Jeremy Yallop
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Braibant @ 2014-10-21 7:49 UTC (permalink / raw)
To: OCaML Mailing List
Hi list,
I am slightly puzzled by a type-error related to first class modules.
I am basically defining two types (see full example below)
```
type t = (module A)
type 'a s = (module A with type I.t = 'a)
```
and I expect a value of type `'a s` can be coerced to a value of type
t. However, this is obviously not the case because I get the following
error message, and I wonder why.
```
(* Error: This expression has type a s = (module A with type I.t = a) *)
(* but an expression was expected of type t = (module A) *)
```
Is there a common pattern to deal with this situation? Any pointers appreciated.
Best,
Thomas
(* Full example *)
module type IN = sig
type t
val of_string : string -> t
val to_string : t -> string
end
module type A = sig
module I : IN
val v : string
end
type t = (module A)
type 'a s = (module A with type I.t = 'a)
let test1 : t -> string = fun (module T) -> T.v
(* let test2 : 'a s -> string = fun (module T) -> T.v
The type of this packed module contains variables:
'a s
*)
let test3 (type a) : a s -> string = fun (module T) -> T.v
let test4 (type a) : a s -> string = fun t -> test1 t
(* Error: This expression has type a s = (module A with type I.t = a) *)
(* but an expression was expected of type t = (module A) *)
module A1 : A = struct
module I : IN = struct
type t = int
let of_string = int_of_string
let to_string = string_of_int
end
let v = "A1"
end
let _ = test1 (module A1) (* OK *)
let _ = test3 (module A1) (* OK *)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] First class modules sub-typing
2014-10-21 7:49 [Caml-list] First class modules sub-typing Thomas Braibant
@ 2014-10-21 8:12 ` Jeremy Yallop
2014-10-21 8:14 ` Jeremy Yallop
0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Yallop @ 2014-10-21 8:12 UTC (permalink / raw)
To: Thomas Braibant; +Cc: OCaML Mailing List
On 21 October 2014 08:49, Thomas Braibant <thomas.braibant@gmail.com> wrote:
> I am slightly puzzled by a type-error related to first class modules.
> I am basically defining two types (see full example below)
>
> ```
> type t = (module A)
> type 'a s = (module A with type I.t = 'a)
> ```
>
> and I expect a value of type `'a s` can be coerced to a value of type
> t. However, this is obviously not the case because I get the following
> error message, and I wonder why.
Conversions between first class module types need explicit coercions.
It's sufficient to change your test4 function as follows:
> let test4 (type a) : a s -> string = fun t -> test1 t
let test4 (type a) : a s -> string = fun t -> test1 (t :> (module A))
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] First class modules sub-typing
2014-10-21 8:12 ` Jeremy Yallop
@ 2014-10-21 8:14 ` Jeremy Yallop
0 siblings, 0 replies; 3+ messages in thread
From: Jeremy Yallop @ 2014-10-21 8:14 UTC (permalink / raw)
To: Thomas Braibant; +Cc: OCaML Mailing List
On 21 October 2014 09:12, Jeremy Yallop <yallop@gmail.com> wrote:
> On 21 October 2014 08:49, Thomas Braibant <thomas.braibant@gmail.com> wrote:
>> I am slightly puzzled by a type-error related to first class modules.
>> I am basically defining two types (see full example below)
>>
>> ```
>> type t = (module A)
>> type 'a s = (module A with type I.t = 'a)
>> ```
>>
>> and I expect a value of type `'a s` can be coerced to a value of type
>> t. However, this is obviously not the case because I get the following
>> error message, and I wonder why.
>
> Conversions between first class module types need explicit coercions.
> It's sufficient to change your test4 function as follows:
>
>> let test4 (type a) : a s -> string = fun t -> test1 t
>
> let test4 (type a) : a s -> string = fun t -> test1 (t :> (module A))
or you can, of course, use the alias 't' to coerce:
let test4 (type a) : a s -> string = fun t -> test1 (t :> t)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-10-21 8:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-21 7:49 [Caml-list] First class modules sub-typing Thomas Braibant
2014-10-21 8:12 ` Jeremy Yallop
2014-10-21 8:14 ` Jeremy Yallop
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox