[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Markus Mottl <mottl@m...> |
| Subject: | Re: [Caml-list] question about modules |
On Wed, 06 Jun 2001, Mark Wotton wrote: > I'm using the translation of Chris Okasaki's code that Markus Mottl has > provided, and I've got a question about the way modules work. I had > assumed that it worked in a similar way to lists: ie, i write "int list", > i'd assumed I'd do something similar with Deques, "int Deque" for > instance. Obviously this doesn't work: I understand that once I've added > an element to a Deque, the polymorphic type is fixed, so there's no > worries about type safety there; however, I had something like this: I assume you have created a module "Deque" using one of the functors in chapter 8, e.g.: module Deque = RealTimeDeque (struct let c = 3 end) As you can see from the signature restrictions used in the functor head of "RealTimeDeque", this functor generates modules that match the signature "DEQUE": module RealTimeDeque (C : sig val c : int end) : DEQUE Therefore, we have to look at this signature to see, what options we have to use deques: --------------------------------------------------------------------------- module type DEQUE = sig type 'a queue val empty : 'a queue val is_empty : 'a queue -> bool (* insert, inspect, and remove the front element *) val cons : 'a -> 'a queue -> 'a queue val head : 'a queue -> 'a (* raises Empty if queue is empty *) val tail : 'a queue -> 'a queue (* raises Empty if queue is empty *) (* insert, inspect, and remove the rear element *) val snoc : 'a queue -> 'a -> 'a queue val last : 'a queue -> 'a (* raises Empty if queue is empty *) val init : 'a queue -> 'a queue (* raises Empty if queue is empty *) end --------------------------------------------------------------------------- Obviously, the type used to refer to deques must be: 'a Deque.queue > type tree = CompTree of tree list * tree list;; > > before I realised that I needed deques. It would seem that the translation > is to > > type tree = CompTree of Deque * Deque;; Thus, you'll have to write: type tree = CompTree of tree Deque.queue * tree Deque.queue If it bothers you to write so much, you can also define a type synonym for deques: type 'a deque = 'a Deque.queue and use it instead: type tree = CompTree of tree deque * tree deque This is probably the most intuitive version. Regards, Markus Mottl -- Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr