Version française
Home     About     Download     Resources     Contact us    
Browse thread
How do apply functors with module constraints?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Moutinho <julien.moutinho@g...>
Subject: Re: [Caml-list] How do apply functors with module constraints?
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.