From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id DC071BB84 for ; Mon, 22 Sep 2008 01:56:02 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoQDADp81kjAXQImiGdsb2JhbACTHwEBARUioGOBZg X-IronPort-AV: E=Sophos;i="4.32,443,1217800800"; d="scan'208";a="17576008" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 22 Sep 2008 01:56:02 +0200 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m8LNu1KN028175 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 22 Sep 2008 01:56:02 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoQDAIJ71kiCNhABiGdsb2JhbACTHwEBARUioGiBZg X-IronPort-AV: E=Sophos;i="4.32,443,1217800800"; d="scan'208";a="15199242" Received: from kurims.kurims.kyoto-u.ac.jp ([130.54.16.1]) by mail2-smtp-roc.national.inria.fr with ESMTP; 22 Sep 2008 01:56:00 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.8/8.13.8) with ESMTP id m8LNttRh015777; Mon, 22 Sep 2008 08:55:55 +0900 (JST) Date: Mon, 22 Sep 2008 08:55:50 +0900 (JST) Message-Id: <20080922.085550.173866523.garrigue@math.nagoya-u.ac.jp> To: angela22.zhu@gmail.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Ocaml type with constraints? From: Jacques Garrigue In-Reply-To: References: X-Mailer: Mew version 4.2 on Emacs 22.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 48D6DF12.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ocaml:01 sig:01 val:01 struct:01 angela:98 angela:98 22.:98 9.95:98 9.95:98 1.0:98 exception:01 caml-list:01 int:01 int:01 From: "Angela Zhu" > I want to define an OCaml type with constraints. > For example: > > type item = Item of int * float;; > > If here this float type is for price of some item, and I want to make sure > it is positive. In other words, if x = (xi, xf) of type item, > I want to enforce, xf must >= 0. > > Is there a way to define OCaml type like this? This is the goal of private types. module Item : sig type item = private Item of int * float val create : int -> float -> item end = struct type item = Item of int * float let create n p = if p >= 0. then Item (n, p) else invalid_arg "Item.create" end # Item.create 3 9.95;; - : Item.item = Item.Item (3, 9.95) # Item.create 3 (-1.0);; Exception: Invalid_argument "Item.create". Note that you must imperatively use Item.create to create an item, but you can use pattern-matching as usual to access its contents. Jacques Garrigue