* 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
* Re: [Caml-list] Pattern matching but no construction?
  2004-10-28 21:34 Pattern matching but no construction? Harrison, John R
@ 2004-10-28 22:30 ` Jon Harrop
  2004-10-28 22:33 ` William Lovas
  2004-10-28 22:36 ` brogoff
  2 siblings, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2004-10-28 22:30 UTC (permalink / raw)
  To: caml-list
You can hide the types held by the Integer and Boolean constructors and not 
hide the variant type itself:
# module Thing : sig
  type integer
  type boolean
  type t = Integer of integer | Boolean of boolean
  val mk_thing : int -> t
  val dest_thing : t -> int
end = struct
  type integer = int
  type boolean = bool
  type t = Integer of integer | Boolean of boolean
  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;;
module Thing :
  sig
    type integer
    type boolean
    type t = Integer of integer | Boolean of boolean
    val mk_thing : int -> t
    val dest_thing : t -> int
  end
Then you could "open" the namespace of the Thing module, saving enormously on 
typing:
# open Thing;;
Then you can use pattern matching to determine if a value of type "Thing.t" 
uses the "Integer" or the "Boolean" constructor:
# fun (Boolean b) -> b;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Integer _
- : Thing.t -> Thing.boolean = <fun>
A better example might be:
# let is_bool = function Integer _ -> false | Boolean _ -> true;;
val is_bool : Thing.t -> bool = <fun>
Of course, if your pattern catches a value of type "Thing.integer" or 
"Thing.boolean" then you can't do anything with it except hand it to a 
function in the "Thing" module.
But you can't actually construct a "Thing.t" because you don't have access to 
the hidden "integer" and "boolean" types in "Thing":
# Integer(3);;
This expression has type int but is here used with type Thing.integer
As an aside which you may well already know, convention is to use a type "t" 
for the main type of a module (e.g. List.t, Array.t, String.t) and a function 
"make" to construct it. Possibly also a function "compare" so you can build 
sets and maps over the type, e.g.:
# module StringSet = Set.Make(String);;
...
Cheers,
Jon.
^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: [Caml-list] Pattern matching but no construction?
  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
  2 siblings, 0 replies; 4+ messages in thread
From: William Lovas @ 2004-10-28 22:33 UTC (permalink / raw)
  To: Harrison, John R; +Cc: caml-list
On Thu, Oct 28, 2004 at 02:34:49PM -0700, 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
Indeed, there is!  The relevant keyword is `private'.  See:
    http://caml.inria.fr/ocaml/htmlman/manual021.html#@manual.kwd173
for details.
In your example, we just do:
> For example, suppose I do the following:
> 
>   module type Wibble =
>  (* sig type thing = Integer of int | Boolean of bool *)
      sig type thing = private Integer of int | Boolean of bool
>         val mk_thing : int -> thing
>         val dest_thing: thing -> int
>     end;;
>
> [...]
And then it works as expected:
    # Thing.Integer 5;;
    Cannot create values of the private type Thing.thing
    # let thing = Thing.mk_thing 5;;
    val thing : Thing.thing = Thing.Integer 5
    # match thing with
        Thing.Integer i -> i
      | Thing.Boolean b -> 0;;
    - : int = 5
cheers,
William
^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: [Caml-list] Pattern matching but no construction?
  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
  2 siblings, 0 replies; 4+ messages in thread
From: brogoff @ 2004-10-28 22:36 UTC (permalink / raw)
  To: Harrison, John R; +Cc: caml-list
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
>
^ 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