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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 11A8BBC69 for ; Fri, 19 Oct 2007 10:22:32 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMoFGEfAXQInh2dsb2JhbACOTwIBCAop X-IronPort-AV: E=Sophos;i="4.21,299,1188770400"; d="scan'208";a="4840198" Received: from concorde.inria.fr ([192.93.2.39]) by mail3-smtp-sop.national.inria.fr with ESMTP; 19 Oct 2007 10:22:31 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l9J8MRdY022560 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Fri, 19 Oct 2007 10:22:31 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aj0KAEQGGEeLEwECYmdsb2JhbACORRUEBhAZ X-IronPort-AV: E=Sophos;i="4.21,299,1188770400"; d="scan'208";a="18337730" Received: from mx2.mpi-sb.mpg.de (HELO francois.mpi-sb.mpg.de) ([139.19.1.2]) by mail4-smtp-sop.national.inria.fr with ESMTP; 19 Oct 2007 10:22:30 +0200 Received: from loopback.mpi-sb.mpg.de ([127.0.0.1]:53888 helo=localhost ident=amavis) by francois.mpi-sb.mpg.de (envelope-from ) with esmtp (Exim 4.50) id 1Iin7l-0004jB-GZ; Fri, 19 Oct 2007 10:22:29 +0200 Received: from francois.mpi-sb.mpg.de ([127.0.0.1]) by localhost (aspirin.mpi-sb.mpg.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17631-05; Fri, 19 Oct 2007 10:22:29 +0200 (CEST) Received: from zak.mpi-sb.mpg.de ([139.19.1.28]:50641) by francois.mpi-sb.mpg.de (envelope-from ) with esmtp (Exim 4.50) id 1Iin7g-0004iA-GO; Fri, 19 Oct 2007 10:22:24 +0200 Received: from p54a59c05.dip0.t-ipconnect.de ([84.165.156.5]:65042 helo=[192.168.178.21]) by zak.mpi-sb.mpg.de with esmtpsa (TLS-1.0:RSA_AES_128_CBC_SHA:16) (Exim 4.50) id 1Iin7Q-00042m-KY; Fri, 19 Oct 2007 10:22:24 +0200 X-Notes-Item: NOT CHECKED; name=$DNSBLSite In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <188637FD-0995-4FA4-A26B-4782F8398182@mpi-sws.mpg.de> Cc: caml-list@inria.fr Content-Transfer-Encoding: 7bit From: Andreas Rossberg Subject: Re: [Caml-list] How do apply functors with module constraints? Date: Fri, 19 Oct 2007 10:21:43 +0200 To: Alan Falloon X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at mpi-sb.mpg.de X-Miltered: at concorde with ID 47186943.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; rossberg:01 rossberg:01 functors:01 functor:01 sig:01 val:01 val:01 sig:01 struct:01 iter:01 struct:01 abstraction:01 syntax:01 abstraction:01 submodules:01 On Oct 19, 2007, at 01.19h, Alan Falloon wrote: > I am having a really hard time figuring out how to instantiate a > functor when its arguments have module constraints. Here is an > example: > > module type PIE = sig > type pie > val bake : unit -> pie > val eat : pie -> unit > end > > module type BAKER = sig > module Pie : PIE > val dozen : unit -> Pie.pie array > end > > module type PIG = sig > module Pie : PIE > val feed : Pie.pie -> unit > end > > (* Need a module constraint to make sure that the baker and pig are > talking about the same pie *) > module Farmer (Baker : BAKER) (Pig : PIG with module Pie = > Baker.Pie) = struct > let feed_pigs () = > let pies = Baker.dozen () in > Array.iter Pig.feed pies > end > > module Apple : PIE = struct > type pie = Pie of string > let bake () = Pie "apple" > let eat (Pie "apple") = () > end > > > module Joe = Farmer(Bob)(Daisy) (*BOOM*) > let () = Joe.feed_pigs() > > > However, this fails to compile because when I try to make module > Joe because Bob.Pie and Daisy.Pie aren't the same. Fair enough, > thats what abstraction is for, however I can't figure out the > syntax to instantiate Joe! I tried this: > > module Joe = Farmer(Bob)(Daisy:PIG with module Pie=Bob.Pie) > As you noted, abstraction prevents the types in the Pie submodules from being equivalent. And there is no way to retract that abstraction later - it wouldn't be abstraction otherwise. So you will not be able to apply Joe as you tried. This is not a question of syntax. The solution is to avoid the over-abstraction and keep the Pie submodules transparent. Try: > module Bob : (BAKER with module Pie = Apple) = struct > module Pie = Apple > let dozen () = Array.init 13 (fun _ -> Pie.bake ()) > end > > module Daisy : (PIG with module Pie = Apple) = struct > module Pie = Apple > let feed p = Pie.eat p; print_endline "OINK!" > end - Andreas