caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unpacked module parameterized type escaping
@ 2013-04-18 17:12 John Carr
  2013-04-18 20:11 ` Markus Mottl
  0 siblings, 1 reply; 2+ messages in thread
From: John Carr @ 2013-04-18 17:12 UTC (permalink / raw)
  To: caml-list


I have a function of two arguments:

1. A packed module implementing Map.S.

2. A value of the module's Map.S.t.

I can't find a way to prevent a "type constructor ... would escape its
scope" error.

I'm running into this limitation from the ocaml manual:

"The package-type syntactic class appearing in the (module package-type)
type expression and in the annotated forms represents a subset of module
types. This subset consists of named module types with optional
constraints of a limited form: only non-parametrized types can be
specified."

The type I want to constrain is parameterized: +'a Map.S.t.

Can I get this code to type check?

(* val make_module : unit -> (module Map.S with type key = int) *)
let make_module () =
  let module K = struct
    type t = int
    let compare = compare
  end
  in
    (module Map.Make(K) : Map.S with type key = int)

let is_empty m v =
  let module M = (val m : Map.S with type key = int) in
    M.is_empty v

(* This fails because (type mt) can not be unified with +'a Map.S.t.

let is_empty (type mt) m v =
  let module M = (val m : Map.S with type key = int and type t = mt) in
    M.is_empty v *)

(* This invalid syntax is approximately what I want:

let is_empty (type '+a mt) m v =
  let module M = (val m : Map.S with type key = int and type 'a t = 'a mt) in
    M.is_empty v
*)  

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

* Re: [Caml-list] Unpacked module parameterized type escaping
  2013-04-18 17:12 [Caml-list] Unpacked module parameterized type escaping John Carr
@ 2013-04-18 20:11 ` Markus Mottl
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Mottl @ 2013-04-18 20:11 UTC (permalink / raw)
  To: John Carr; +Cc: caml-list

On Thu, Apr 18, 2013 at 1:12 PM, John Carr <jfc@mit.edu> wrote:
>
> I have a function of two arguments:
>
> 1. A packed module implementing Map.S.
>
> 2. A value of the module's Map.S.t.
>
> I can't find a way to prevent a "type constructor ... would escape its
> scope" error.

I think you may just want to wrap both the map module to be used and
the value on which its functions should operate in another first-class
module, e.g.:

  module type Arg = sig
    module M : Map.S with type key = int
    val v : 'a M.t
  end

Then you can write:

  let is_empty (module Arg : Arg) = Arg.M.is_empty Arg.v

This admittedly doesn't look particularly elegant, but should solve the problem.

Regards,
Markus

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

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

end of thread, other threads:[~2013-04-18 20:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-18 17:12 [Caml-list] Unpacked module parameterized type escaping John Carr
2013-04-18 20:11 ` Markus Mottl

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