caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
To: caml-list@inria.fr
Subject: recursive module and types
Date: Wed, 19 May 2010 14:24:29 +0200	[thread overview]
Message-ID: <4BF3D87D.6010805@univ-savoie.fr> (raw)


Hello,

I needed to define a (large) recursive type using sets basically as in the example below.
What I do not like is that I need to repeat the definition of the type twice (in my real code the 
type is 100 lines long) ...

Does anyone see a way to avoid it, apart from using parametric types, which is also done below

Cheers,
Christophe

(* data type of finitely branching trees
    with sets for leafs ... *)

module rec T : sig
   type t =
     Node of S.t
   | Leaf

   val compare : t -> t -> int
end = struct
   type t =
     Node of S.t
   | Leaf

   let compare u v = match u, v with
     Leaf, Leaf -> 0
   | Node u', Node v' -> S.compare u' v'
   | Leaf, Node _ -> -1
   | Node _, Leaf -> 1
end

and S : Set.S with type elt = T.t = Set.Make(T)

(* a solution with parametric types
    pb: the parametric type is not
    very readable by itself especially when it will be longer *)

type 'a pre_t  =
     Node of 'a
   | Leaf

module rec T' : sig
   type t = S'.t pre_t

   val compare : t -> t -> int
end = struct
   type t = S'.t pre_t

   let compare u v = match u, v with
     Leaf, Leaf -> 0
   | Node u', Node v' -> S'.compare u' v'
   | Leaf, Node _ -> -1
   | Node _, Leaf -> 1
end
and S' : Set.S with type elt = T'.t = Set.Make(T')

(* The same as a functor to allow storing data in
    the tree *)

module DT (D:Set.OrderedType) = struct
   module rec T : sig
     type t =
       Node of D.t * S.t
     | Leaf

     val compare : t -> t -> int
   end = struct
     type t =
       Node of D.t * S.t
     | Leaf

     let compare u v = match u, v with
       Leaf, Leaf -> 0
     | Node (du,u'), Node (dv,v') ->
	(match D.compare du dv with
	   0 -> S.compare u' v'
	 | c -> c)
     | Leaf, Node _ -> -1
     | Node _, Leaf -> 1
     end

   and S : Set.S with type elt = T.t = Set.Make(T)

end
-- 
Christophe Raffalli
Universite de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tel: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------


             reply	other threads:[~2010-05-19 12:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-19 12:24 Christophe Raffalli [this message]
2010-05-19 22:58 ` [Caml-list] " Alain Frisch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BF3D87D.6010805@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).