Browse thread
Pattern matching but no construction?
- Harrison, John R
[
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: | Harrison, John R <johnh@i...> |
| Subject: | Pattern matching but no construction? |
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.