caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Pattern matching but no construction?
@ 2004-10-28 21:34 Harrison, John R
  2004-10-28 22:30 ` [Caml-list] " Jon Harrop
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Harrison, John R @ 2004-10-28 21:34 UTC (permalink / raw)
  To: caml-list

Is there a way to use the OCaml module system to declare an
abstract type with an implementation as a recursive type in
such a way that:

 * You can use the constructors to pattern-match against

 * You cannot use the constructors to construct values

For example, suppose I do the following:

  module type Wibble =
    sig type thing = Integer of int | Boolean of bool
        val mk_thing : int -> thing
        val dest_thing: thing -> int
    end;;

  module Thing : Wibble = struct
    type thing = Integer of int | Boolean of bool
         let mk_thing i = Integer i
         let dest_thing t = match t with
           Integer i -> i
         | Boolean b -> if b then 1 else 0
    end;;

  include Thing;;

I can now define functions by pattern-matching, which I want:

  fun (Boolean b) -> b;;

but I can also use the constructors to construct, which I don't:

  Integer(3);;

On the other hand, if I change the signature to just

  module type Wibble =
    sig type thing
        val mk_thing : int -> thing
        val dest_thing: thing -> int
    end;;

then I can do neither. Is there any way to get one and not the
other?

John.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-10-28 22:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-28 21:34 Pattern matching but no construction? Harrison, John R
2004-10-28 22:30 ` [Caml-list] " Jon Harrop
2004-10-28 22:33 ` William Lovas
2004-10-28 22:36 ` brogoff

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).