* How do apply functors with module constraints?
@ 2007-10-18 23:19 Alan Falloon
2007-10-19 0:32 ` [Caml-list] " Julien Moutinho
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Alan Falloon @ 2007-10-18 23:19 UTC (permalink / raw)
To: caml-list
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 Bob : BAKER = struct
module Pie = Apple
let dozen () = Array.init 13 (fun _ -> Pie.bake ())
end
module Daisy : PIG = struct
module Pie = Apple
let feed p = Pie.eat p; print_endline "OINK!"
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)
But then I get a syntax error (?)
File "bar.ml", line 39, characters 30-31:
Syntax error: ')' expected
File "bar.ml", line 39, characters 24-25:
This '(' might be unmatched
Now I'm fresh out of ideas.
Thanks
Alan Falloon
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] How do apply functors with module constraints?
2007-10-18 23:19 How do apply functors with module constraints? Alan Falloon
@ 2007-10-19 0:32 ` Julien Moutinho
2007-10-19 8:20 ` Nicolas Pouillard
2007-10-19 8:21 ` Andreas Rossberg
2 siblings, 0 replies; 5+ messages in thread
From: Julien Moutinho @ 2007-10-19 0:32 UTC (permalink / raw)
To: caml-list
On Thu, Oct 18, 2007 at 07:19:05PM -0400, 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:
> [...]
>
> 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!
>
> [...]
>
> Now I'm fresh out of ideas.
Here is a new one:
% cat fapie.ml
module type PIE =
sig
type pie
val bake : unit -> pie
val eat : pie -> unit
end
module type BAKER =
functor (Pie: PIE) ->
sig
val dozen : unit -> Pie.pie array
end
module type PIG =
functor (Pie: PIE) ->
sig
val feed : Pie.pie -> unit
end
module Farmer
(Pie : PIE)
(Baker : BAKER)
(Pig : PIG) =
struct
module Baker = Baker(Pie)
module Pig = Pig(Pie)
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 Bob
(Pie : PIE) =
struct
let dozen () = Array.init 13 (fun _ -> Pie.bake ())
end
module Daisy
(Pie : PIE) =
struct
let feed p = Pie.eat p; print_endline "OINK!"
end
module Joe = Farmer(Apple)(Bob)(Daisy)
let () = Joe.feed_pigs()
% ocaml fapie.ml
File "fapie.ml", line 36, characters 12-30:
Warning P: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Pie ""
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
OINK!
Hope this helps,
Julien.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] How do apply functors with module constraints?
2007-10-18 23:19 How do apply functors with module constraints? Alan Falloon
2007-10-19 0:32 ` [Caml-list] " Julien Moutinho
@ 2007-10-19 8:20 ` Nicolas Pouillard
2007-10-19 8:21 ` Andreas Rossberg
2 siblings, 0 replies; 5+ messages in thread
From: Nicolas Pouillard @ 2007-10-19 8:20 UTC (permalink / raw)
To: Alan Falloon; +Cc: caml-list
Excerpts from Alan Falloon's message of Fri Oct 19 01:19:05 +0200 2007:
> I am having a really hard time figuring out how to instantiate a functor
> when its arguments have module constraints. Here is an example:
You can also expose that Bob is an apple pie baker, and Daisy an apple pie
eater:
module Bob : BAKER with module Pie = Apple = ...
module Daisy : PIG with module Pie = Apple = ...
HTH,
> 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 Bob : BAKER = struct
> module Pie = Apple
> let dozen () = Array.init 13 (fun _ -> Pie.bake ())
> end
>
> module Daisy : PIG = struct
> module Pie = Apple
> let feed p = Pie.eat p; print_endline "OINK!"
> 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)
>
> But then I get a syntax error (?)
> File "bar.ml", line 39, characters 30-31:
> Syntax error: ')' expected
> File "bar.ml", line 39, characters 24-25:
> This '(' might be unmatched
>
> Now I'm fresh out of ideas.
>
> Thanks
> Alan Falloon
>
--
Nicolas Pouillard aka Ertai
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] How do apply functors with module constraints?
2007-10-18 23:19 How do apply functors with module constraints? Alan Falloon
2007-10-19 0:32 ` [Caml-list] " Julien Moutinho
2007-10-19 8:20 ` Nicolas Pouillard
@ 2007-10-19 8:21 ` Andreas Rossberg
2007-10-22 14:54 ` Alan Falloon
2 siblings, 1 reply; 5+ messages in thread
From: Andreas Rossberg @ 2007-10-19 8:21 UTC (permalink / raw)
To: Alan Falloon; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How do apply functors with module constraints?
2007-10-19 8:21 ` Andreas Rossberg
@ 2007-10-22 14:54 ` Alan Falloon
0 siblings, 0 replies; 5+ messages in thread
From: Alan Falloon @ 2007-10-22 14:54 UTC (permalink / raw)
To: caml-list
Thanks for all the responses. I had a mistake with my mental model of
modules, I was treating the 'with' statement as a declaration to aid
type checking instead of seeing it as syntax for deriving a new
signature (that didn't match in my case) from an existing one.
I have started using the style Julien suggested for my code. Again,
thanks for the help.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-10-22 14:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-18 23:19 How do apply functors with module constraints? Alan Falloon
2007-10-19 0:32 ` [Caml-list] " Julien Moutinho
2007-10-19 8:20 ` Nicolas Pouillard
2007-10-19 8:21 ` Andreas Rossberg
2007-10-22 14:54 ` Alan Falloon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox