caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jonathan Bryant <jtbryant@valdosta.edu>
To: Chris King <colanderman@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Functors + polymorphism = confusion
Date: Thu, 21 Jun 2007 22:45:30 -0400	[thread overview]
Message-ID: <607D28EB-7290-4D9C-9124-D44DF7B04765@valdosta.edu> (raw)
In-Reply-To: <875c7e070706210708u2ab3719er6e24f4ec52bf6398@mail.gmail.com>


On Jun 21, 2007, at 10:08 AM, Chris King wrote:

> The important thing to remember with Caml modules is that it will hide
> type relations unless you tell it otherwise:
>
> Here modules A and B are of type Message, which define an abstract
> type t.  Caml doesn't automatically fill in this type definition for
> you, so the module returned by BindFunction will only be able to
> operate over a pair of abstract types.  Not good!

Ah, I see the error now!

>
> The solution to this (which you will see used in functorized library
> modules such as Map and Set) is to add the derivation of types A.t and
> B.t to your output signature using the "with" syntax:
>
> module BindFunction :
> functor (F : PolyFunction) ->
> functor (A : Message) ->
> functor (B : Message) ->
> Function with type A.t = A.t and type B.t = B.t
>

Works perfectly!  I had always been a little confused as to exactly  
what the "with" syntax was accomplishing, and even with your  
explanation it took a while for me to see how to use it.  Thanks for  
the clarification.

>
>> module BindMessage =
>>   functor (P : PolyMessage) ->
>>   functor (M : Message) ->
>>   struct
>>     module P = P (M)
>>     type t = M.t P.t
>>     let to_string m = P.to_string m
>>     let of_string s = P.of_string s
>>   end
>
> Because you are not restricting the output signatures in your
> implementation, Caml preserves the type relationships (it only
> discards them at the interface level).  However if you did specify the
> output signature here (as the library modules do) you would need to
> use the same construct.
>

I have gone back and used the same "with" syntax on my prebuilt  
messages and it solved another type issue I was having :)

I do specify the output signature (in the .mli file):

module BindMessage :
   functor (P : PolyMessage) ->
   functor (M : Message) ->
   Message

but now it's:

module BindMessage :
   functor (P : PolyMessage) ->
   functor (M : Message) ->
   Message with type t = M.t P(M).t

>
> Hope this helps... I can attest that this stuff is pretty confusing  
> at first. :)

Yes.  I am learning more about the module system than I think I ever  
wanted to know.  My project makes heavy use of (read: abuses) the  
modules system :)

--Jonathan

>
> - Chris
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


      reply	other threads:[~2007-06-22  2:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-21  7:16 Jonathan T Bryant
2007-06-21 14:08 ` [Caml-list] " Chris King
2007-06-22  2:45   ` Jonathan Bryant [this message]

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=607D28EB-7290-4D9C-9124-D44DF7B04765@valdosta.edu \
    --to=jtbryant@valdosta.edu \
    --cc=caml-list@yquem.inria.fr \
    --cc=colanderman@gmail.com \
    /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).