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 0E426BC40 for ; Fri, 29 Oct 2004 00:34:45 +0200 (CEST) Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.212]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i9SMYiAt015342 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 29 Oct 2004 00:34:44 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay01.plus.net with esmtp (Exim) id 1CNIqw-000PE1-8c for caml-list@yquem.inria.fr; Thu, 28 Oct 2004 22:34:42 +0000 From: Jon Harrop To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Pattern matching but no construction? Date: Thu, 28 Oct 2004 23:30:01 +0100 User-Agent: KMail/1.6.2 References: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407> In-Reply-To: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407> MIME-Version: 1.0 Content-Disposition: inline Message-Id: <200410282330.01081.jon@jdh30.plus.com> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41817404.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 constructors:01 sig:01 val:01 val:01 struct:01 bool:01 sig:01 bool:01 stringset:01 cheers:01 ...:98 expression:01 typing:01 constructor: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: 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 = A better example might be: # let is_bool = function Integer _ -> false | Boolean _ -> true;; val is_bool : Thing.t -> bool = 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.