From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 17445BC40 for ; Fri, 29 Oct 2004 00:33:56 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i9SMXtgw015142 for ; Fri, 29 Oct 2004 00:33:55 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA11312 for ; Fri, 29 Oct 2004 00:33:55 +0200 (MET DST) Received: from nexus.stwing.upenn.edu (NEXUS.STWING.UPENN.EDU [165.123.132.61]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i9SMXr8u006823 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Fri, 29 Oct 2004 00:33:54 +0200 Received: from force.stwing.upenn.edu (force.stwing.upenn.edu [165.123.132.65]) by nexus.stwing.upenn.edu (8.12.10/8.12.9) with ESMTP id i9SMXlHA001993 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 28 Oct 2004 18:33:48 -0400 (EDT) Received: from force.stwing.upenn.edu (localhost [127.0.0.1]) by force.stwing.upenn.edu (8.12.10/8.12.9) with ESMTP id i9SMXlh4001119 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 28 Oct 2004 18:33:47 -0400 (EDT) Received: (from wlovas@localhost) by force.stwing.upenn.edu (8.12.10/8.12.9/Submit) id i9SMXk7h001086; Thu, 28 Oct 2004 18:33:47 -0400 (EDT) Date: Thu, 28 Oct 2004 18:33:46 -0400 From: William Lovas To: "Harrison, John R" Cc: caml-list Subject: Re: [Caml-list] Pattern matching but no construction? Message-ID: <20041028223346.GB5076@force.stwing.upenn.edu> Mail-Followup-To: "Harrison, John R" , caml-list References: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407> User-Agent: Mutt/1.5.4i X-Miltered: at nez-perce with ID 418173D3.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 418173D1.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; lovas:01 wlovas:01 stwing:01 upenn:01 caml-list:01 wrote:01 ocaml:01 recursive:01 constructors:01 constructors:01 ocaml:01 htmlman:01 kwd:01 sig:01 bool:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: 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