Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] how to use functors?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] how to use functors?
On Saturday 19 November 2005 21:37, Jonathan Roewen wrote:
> Hi,
>
> Prolly the wrong list, but I can't access caml.inria.fr's documentation ATM
> =/

This is probably one for the beginners list. There is a brief explanation of 
functors on my "Benefits of OCaml" pages:

  http://www.ffconsultancy.com/free/ocaml/

under "Modules and Functors".

Feed the functor a module with a sig that matches that required by the functor 
and it will spit out another module based upon the one you gave.

For example, the built-in "String" module contains the "t" type and "compare" 
function definitions required by the "Set.Make" functor. So applying "String" 
to "Set.Make" gives you a module implementing sets of strings:

# module SetOfStrings = Set.Make(String);;
module SetOfStrings :
  sig
    type elt = String.t
    type t = Set.Make(String).t
    val empty : t
    val is_empty : t -> bool
    val mem : elt -> t -> bool
    val add : elt -> t -> t
    val singleton : elt -> t
    val remove : elt -> t -> t
    val union : t -> t -> t
    val inter : t -> t -> t
    val diff : t -> t -> t
    val compare : t -> t -> int
    val equal : t -> t -> bool
    val subset : t -> t -> bool
    val iter : (elt -> unit) -> t -> unit
    val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
    val for_all : (elt -> bool) -> t -> bool
    val exists : (elt -> bool) -> t -> bool
    val filter : (elt -> bool) -> t -> t
    val partition : (elt -> bool) -> t -> t * t
    val cardinal : t -> int
    val elements : t -> elt list
    val min_elt : t -> elt
    val max_elt : t -> elt
    val choose : t -> elt
    val split : elt -> t -> t * bool * t
  end

> How do I use a functor? For example: build a weak hashtable from Weak
> module? Seems useful for building a cache... if only I knew how to use
> it =P

You may want something more controllable.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists