Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Harrison, John R" <johnh@ichips.intel.com>
To: "caml-list" <caml-list@inria.fr>
Subject: Pattern matching but no construction?
Date: Thu, 28 Oct 2004 14:34:49 -0700	[thread overview]
Message-ID: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407> (raw)

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.


             reply	other threads:[~2004-10-28 21:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-28 21:34 Harrison, John R [this message]
2004-10-28 22:30 ` [Caml-list] " Jon Harrop
2004-10-28 22:33 ` William Lovas
2004-10-28 22:36 ` brogoff

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=012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407 \
    --to=johnh@ichips.intel.com \
    --cc=caml-list@inria.fr \
    /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