Browse thread
Pattern matching but no construction?
-
Harrison, John R
- William Lovas
- Jon Harrop
- brogoff
[
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: | brogoff <brogoff@s...> |
| Subject: | Re: [Caml-list] Pattern matching but no construction? |
private types do exactly what you want. How lucky. See where I inserted private
below, that should do it.
On Thu, 28 Oct 2004, Harrison, John R wrote:
> 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
^
--- private
> 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.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>