Browse thread
How do apply functors with module constraints?
-
Alan Falloon
- Julien Moutinho
- Nicolas Pouillard
- Andreas Rossberg
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Andreas Rossberg <rossberg@m...> |
| Subject: | Re: [Caml-list] How do apply functors with module constraints? |
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