Version française
Home     About     Download     Resources     Contact us    
Browse thread
Instanciating functor types with extra parameters
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: rossberg@p...
Subject: Re: [Caml-list] Instanciating functor types with extra parameters
From: "Arnaud Spiwack" <aspiwack@lix.polytechnique.fr>
>
> When I have a functor type, like for example (not too innocent
> I'm afraid) :
>
> module type OrderedType =
>   sig
>     type t
>     val compare : t -> t -> int
>   end
>
> Something that naive intuition would allow you to do is something like :
>
> module GenOrder : OrderedType =
>   struct
>     type t = 'a
>     let compare = compare
>   end
>
> Though this is more or less nonsense. And is not currently possible
> under OCaml type system.
>
> My point is that I know absolutely no way of doing such a thing.

If you have control over the functor type then you can do this:

  module type OrderedPolyType =
  sig
     type 'a t
     val compare : 'a t -> 'a t -> int
  end

  module GenOrder : OrdererPolyType =
  struct
     type 'a t = 'a
     let compare = compare
  end

  module InvIntOrder : OrdererPolyType =
  struct
     type 'a t = int
     let compare x y = compare y x
  end

Not a general solution, but one that's often good enough.

- Andreas