caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Neel Krishnaswami <krishnan@cs.bham.ac.uk>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] First class modules question
Date: Thu, 10 Apr 2014 23:16:07 +0200	[thread overview]
Message-ID: <1397164567.4084.24.camel@thinkpad> (raw)
In-Reply-To: <5346FB8F.3010406@cs.bham.ac.uk>

[-- Attachment #1: Type: text/plain, Size: 2809 bytes --]

Am Donnerstag, den 10.04.2014, 21:14 +0100 schrieb Neel Krishnaswami:
> 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:

But you don't mean the applicative functor feature of OCaml, right? This
doesn't work with first class modules. (Just to avoid
misunderstandings.)

>    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?

I guess the problem is that it is unclear/unexpressable then what the
constraint means for 'a. Imagine you had a constraint
type 'a t = 'a list. This would implicitly mean that any 'a works on the
right side (and thus on the left, too). However, as we don't know
anything about the right side, we cannot conclude that in your example.
If e.g. the right side were int list, you could read this as a
constraint 'a=int. This just means that a parameterized constraint can
also constrain the parameter, but we don't have any means to express
that. So I guess this is the reason why it is not supported. (You
probably need a notation like "type 'a t = s where 'a = u".)

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

  reply	other threads:[~2014-04-10 21:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-10 20:14 Neel Krishnaswami
2014-04-10 21:16 ` Gerd Stolpmann [this message]
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=1397164567.4084.24.camel@thinkpad \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=krishnan@cs.bham.ac.uk \
    /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).