English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
a question about recursiv type defintions and functors like Set.Make?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-21 (15:01)
From: Phillip Heidegger <heidegger@i...>
Subject: a question about recursiv type defintions and functors like Set.Make?

I have a question about using the set functor. I need a type

type a = BaseCase of string
      | BSet of b set
      | ASet of a set
and  b = BaseCaseB of string
      | ASetB of a set

In my first implementation I used instead of sets lists
and write some functions to manipulate the values of type
a and b. But now I need a faster representation for the sets,
and I try to use the module "Set". But I didn't find a way
using it because of the recursion in the type definition.

I would like to write something like:

type a = BaseCase of string        | BSet of BSet.t
      | ASet of ASet.t
and  b = BaseCaseB of string
      | ASetB of ASet.t
and module ASet = Set.Make(struct type a let compare x y = ...end)
and module BSet = Set.Make(struct type b let compare x y = ...end)
(of cause this is not valid OCaml Code, but I hope it helps to
understand, what I would like to do).

This code did not work because I used the type ASet.t in the
definition of a, and the type a in the functor call of ASet.
Because modules are not recursive in OCaml, I'm not able to
write code like this I think.

Now my next approach was not to use the Set module, but change
the code of this module, so I get a module with polymorph

module Set :
   type 'a t
   val empty : 'a t
   val is_empty : 'a t -> bool
   val mem : ('a -> 'a -> int) -> 'a -> 'a t -> bool
   .... (* nearly all functions need a method compare like mem *)

I can write my type as I desired in CODE1, but I have to pass to
all functions, every time I used the set, the compare Function as
a parameter. For example:

if (mem cmpTypeA element aSet) then ......

Is there a better way to implement this set Module? Is there a
way to use the Set functor code?
Is there a way to get recursiv moduls in OCaml? How should I solve
my problem, if I have recursive modules. If it's possible to solve
my problem without using recursive modules, what should I do?