Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: brogoff <brogoff@speakeasy.net>
To: "Harrison, John R" <johnh@ichips.intel.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Pattern matching but no construction?
Date: Thu, 28 Oct 2004 15:36:55 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.58.0410281534190.22051@shell1.speakeasy.net> (raw)
In-Reply-To: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407>

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
>


      parent reply	other threads:[~2004-10-28 22:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-28 21:34 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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0410281534190.22051@shell1.speakeasy.net \
    --to=brogoff@speakeasy.net \
    --cc=caml-list@inria.fr \
    --cc=johnh@ichips.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox