caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Polymorphic values in local modules
@ 2010-02-03 10:05 Tiphaine Turpin
  2010-02-03 11:35 ` Alain Frisch
  0 siblings, 1 reply; 2+ messages in thread
From: Tiphaine Turpin @ 2010-02-03 10:05 UTC (permalink / raw)
  To: caml-list

Hans Ole Rafaelsen a écrit :

> 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
>
> let test f =
>  let module Foo : Foo_sig =
>      struct
>    let foo = f
>      end
>  in
>    ()
The problem here is that the test function itself is not typeable
(because the 'a parameter would have to be quantified under an arrow,
which the type system does not directly allows.
It is possible however, provided you wrap the argument inside a
polymorphic record field (or object) as follows:

type t = {f : 'a. 'a -> 'a}

let test f =
 let module Foo : Foo_sig =
     struct
   let foo = f.f
     end
 in
   ()

Tiphaine




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Polymorphic values in local modules
  2010-02-03 10:05 [Caml-list] Polymorphic values in local modules Tiphaine Turpin
@ 2010-02-03 11:35 ` Alain Frisch
  0 siblings, 0 replies; 2+ messages in thread
From: Alain Frisch @ 2010-02-03 11:35 UTC (permalink / raw)
  To: Tiphaine Turpin; +Cc: caml-list

On 03/02/2010 11:05, Tiphaine Turpin wrote:
> It is possible however, provided you wrap the argument inside a
> polymorphic record field (or object) as follows:
>
> type t = {f : 'a. 'a ->  'a}
>
> let test f =
>   let module Foo : Foo_sig =
>       struct
>     let foo = f.f
>       end
>   in
>     ()

Another option (that will be available in OCaml 3.12) is to build the 
module as a first-class value outside the function "test".

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

let test foo =
   let module Foo = (val foo : Foo_sig) in
   (* use the polymorphic function here... *)
   ()

let () =
   let foo_impl = (module struct let f a = a end : Foo_sig) in
   test foo_impl
=================================

The type for test is:

val test : (module Foo_sig) -> unit



Alain



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-02-03 11:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-03 10:05 [Caml-list] Polymorphic values in local modules Tiphaine Turpin
2010-02-03 11:35 ` Alain Frisch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).