Discussion:
First class modules sub-typing
Thomas Braibant
2014-10-21 07:49:30 UTC
Permalink
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 *)
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Jeremy Yallop
2014-10-21 08:12:57 UTC
Permalink
Post by Thomas Braibant
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.
Post by Thomas Braibant
let test4 (type a) : a s -> string = fun t -> test1 t
let test4 (type a) : a s -> string = fun t -> test1 (t :> (module A))
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Jeremy Yallop
2014-10-21 08:14:04 UTC
Permalink
Post by Jeremy Yallop
Post by Thomas Braibant
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.
Post by Thomas Braibant
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)
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Loading...