Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Thomas Braibant <thomas.braibant@gmail.com>
To: OCaML Mailing List <caml-list@inria.fr>
Subject: [Caml-list] First class modules sub-typing
Date: Tue, 21 Oct 2014 09:49:30 +0200	[thread overview]
Message-ID: <CAHR=VkzsbRrPEqi3bg3Zk3oQHAthgrtdOuK9uM0J6846pUFyFQ@mail.gmail.com> (raw)

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

             reply	other threads:[~2014-10-21  7:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-21  7:49 Thomas Braibant [this message]
2014-10-21  8:12 ` Jeremy Yallop
2014-10-21  8:14   ` Jeremy Yallop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHR=VkzsbRrPEqi3bg3Zk3oQHAthgrtdOuK9uM0J6846pUFyFQ@mail.gmail.com' \
    --to=thomas.braibant@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox