caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Neel Krishnaswami <krishnan@cs.bham.ac.uk>
To: caml-list@inria.fr
Subject: [Caml-list] First class modules question
Date: Thu, 10 Apr 2014 21:14:07 +0100	[thread overview]
Message-ID: <5346FB8F.3010406@cs.bham.ac.uk> (raw)

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


             reply	other threads:[~2014-04-10 20:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-10 20:14 Neel Krishnaswami [this message]
2014-04-10 21:16 ` Gerd Stolpmann
2014-04-10 22:03 ` Raphael 'kena' Poss

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=5346FB8F.3010406@cs.bham.ac.uk \
    --to=krishnan@cs.bham.ac.uk \
    --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;
as well as URLs for NNTP newsgroup(s).