Version française
Home     About     Download     Resources     Contact us    
Browse thread
Polymorphic values in local modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Hans Ole Rafaelsen <hans@s...>
Subject: Polymorphic values in local modules
Hi,

I'm trying to construct a module that have a function taking polymorphic 
arguments. I want to use this module as a local module in different 
places, where the function implementation is different. The module has 
the signature:

module type Foo_sig =
sig
  val foo : 'a -> 'a
end

If I have a function definition:

let foo_impl a =
  a

I'm allowed to write:
let _ = let module Foo : Foo_sig =
        struct
          let foo = foo_impl
        end
        in
           ()

However I want to have a function that creates the module and take the 
"foo_impl" as argument:
let test f =
  let module Foo : Foo_sig =
      struct
    let foo = f
      end
  in
    ()

and then later I would like to use it like:
let _ = test foo_impl

However, compiling it fails with:
Error: Signature mismatch:
       Modules do not match: sig val foo : '_a end is not included in 
Foo_sig
       Values do not match:
         val foo : '_a
       is not included in
         val foo : 'a -> 'a

Is there some trick to force f to have ('a -> ')? I have tried with "let 
test (f : 'a -> 'a)" but then it complains that f has type ('_a -> '_a). 
Any other suggestions/design patterns for solving these situations, 
without rewriting the whole structure of the application, would also be 
highly appreciated.

Thanks,

Hans