From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 47C02BC6C for ; Fri, 22 Jun 2007 04:45:47 +0200 (CEST) Received: from sccmmhc91.asp.att.net (sccmmhc91.asp.att.net [204.127.203.211]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5M2jkjP020069 for ; Fri, 22 Jun 2007 04:45:46 +0200 Received: from [192.168.0.104] (12-208-242-210.client.mchsi.com[12.208.242.210]) by sccmmhc91.asp.att.net (sccmmhc91) with SMTP id <20070622024543m9100d3s1je>; Fri, 22 Jun 2007 02:45:43 +0000 In-Reply-To: <875c7e070706210708u2ab3719er6e24f4ec52bf6398@mail.gmail.com> References: <6372856.1182410172319.JavaMail.jtbryant@valdosta.edu> <875c7e070706210708u2ab3719er6e24f4ec52bf6398@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <607D28EB-7290-4D9C-9124-D44DF7B04765@valdosta.edu> Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: 7bit From: Jonathan Bryant Subject: Re: [Caml-list] Functors + polymorphism = confusion Date: Thu, 21 Jun 2007 22:45:30 -0400 To: Chris King X-Mailer: Apple Mail (2.752.3) X-Miltered: at concorde with ID 467B37DA.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; functors:01 polymorphism:01 derivation:01 syntax:01 functor:01 functor:01 syntax:01 struct:01 restricting:01 mli:01 beginner's:01 ocaml:01 bug:01 2007,:98 beginners:01 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