caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] First class modules question
@ 2014-04-10 20:14 Neel Krishnaswami
  2014-04-10 21:16 ` Gerd Stolpmann
  2014-04-10 22:03 ` Raphael 'kena' Poss
  0 siblings, 2 replies; 3+ messages in thread
From: Neel Krishnaswami @ 2014-04-10 20:14 UTC (permalink / raw)
  To: caml-list

Hello,

I'm experimenting with Ocaml's first-class modules feature, and
have run into a problem. I have a record type

   type 'a monoid = {unit : 'a; join : 'a -> 'a -> 'a}

and a signature

   module type APPLICATIVE = sig
     type 'a t
     val map : ('a -> 'b) -> 'a t -> 'b t
     val pure : 'a -> 'a t
     val ( $ ) : ('a -> 'b) t -> 'a t -> 'b t
   end

and I would like to write a function which turns a monoid into
an applicative module, as follows:

   let monoid_to_app (type s) (m : s monoid) =
     let module I = struct
       type 'a t = s
       let map _ x = x
       let pure _ = m.unit
       let ($) x y = m.join x y
     end
     in (module I : APPLICATIVE with type 'a t = s)

Here, I want to constrain the type of APPLICATIVE to be the monoid
element type s. However, Ocaml complains with a syntax error, and
looking at the grammar for type constraints, the annotation on I
is a package-type, which only permits constraints using the
package-constraint production

   package-type ::= modtype-path
                 |  modtype-path with package-constraint
                                 {and package-constraint}

   package-constraint ::= type typeconstr =  typexpr

This does not permit a constraint of the form "type 'a t = s",
unlike the module-constraint production of the grammar.

Is this a fundamental limitation on first-class modules, or is it
just a restriction for ease of implementation?

-- 
Neel Krishnaswami
N.Krishnaswami@cs.bham.ac.uk


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

end of thread, other threads:[~2014-04-10 22:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-10 20:14 [Caml-list] First class modules question Neel Krishnaswami
2014-04-10 21:16 ` Gerd Stolpmann
2014-04-10 22:03 ` Raphael 'kena' Poss

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