From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 69F63BC48 for ; Thu, 7 Apr 2005 03:58:45 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j371wiEE029472 for ; Thu, 7 Apr 2005 03:58:44 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA24092 for ; Thu, 7 Apr 2005 03:58:44 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j371wgb1007570 for ; Thu, 7 Apr 2005 03:58:43 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j371we7a027826; Thu, 7 Apr 2005 10:58:40 +0900 (JST) Date: Thu, 07 Apr 2005 10:58:30 +0900 (JST) Message-Id: <20050407.105830.78444406.garrigue@math.nagoya-u.ac.jp> To: wlovas@stwing.upenn.edu Cc: caml-list@inria.fr Subject: Re: [Caml-list] ambitious proposal: polymorphic arithmetics From: Jacques Garrigue In-Reply-To: <20050407001308.GA26438@force.stwing.upenn.edu> References: <20050406163915.GC15547@force.stwing.upenn.edu> <20050406.153356.35007840.eijiro_sumii@anet.ne.jp> <20050407001308.GA26438@force.stwing.upenn.edu> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 425493D4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 425493D2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 lovas:01 wlovas:01 stwing:01 upenn:01 abstraction:01 val:01 abstr:01 sig:01 val:01 struct:01 sig:01 abstr:01 bool:01 runtime:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: William Lovas > > Yes, polymorphic comparison already breaks type abstraction. > > I did not realize this! Can somebody explain the following interactions? > > # let r = Ratio.ratio_of_int 5;; > val r : Ratio.ratio = > # r = r;; > Exception: Invalid_argument "equal: abstract value". > > # module M : sig type t;; val of_int : int -> t end = > struct type t = int;; let of_int x = x end;; > module M : sig type t val of_int : int -> t end > # let t = M.of_int 5;; > val t : M.t = > # t = t;; > - : bool = true > > I thought that perhaps all comparisons of abstract values resulted in a > runtime error, but evidently i was mistaken :/ What's the secret to making > a type "really" abstract? The error message is a bit misleading: it actually means an internal object with abstract tag. You can only produce those on the C side. However, this gives you a trick if you want to protect a type from comparisons: include an incomparable field in it. Better to put it as first field, as it then makes sure that even ordering cannot be be checked. type 'a protected = {prot: unit Weak.t; desc: 'a} let protect d = {prot = Weak.create 1; desc = d } Now protected values are impossible to compare. More precisely, they are only comparable when physically equal, which you can anyway check with ==. Even in this case polymorphic equality will fail (it checks even physically equal values.) # let x = protect 1;; val x : int protected = {prot = ; desc = 1} # x=x;; Exception: Invalid_argument "equal: abstract value". # compare x x;; - : int = 0 # let y = protect 1;; val y : int protected = {prot = ; desc = 1} # compare x y;; Exception: Invalid_argument "equal: abstract value". So, if you really need abstraction, you can have it. (You can still break it using the Obj module, but this is another story...) Jacques Garrigue