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.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 7B6C7BBAF for ; Wed, 8 Apr 2009 03:35:25 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAF+c20mCNhAB/2dsb2JhbADNHYN7Bg X-IronPort-AV: E=Sophos;i="4.39,340,1235948400"; d="scan'208";a="25843447" Received: from unknown (HELO kurims.kurims.kyoto-u.ac.jp) ([130.54.16.1]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 08 Apr 2009 03:35:23 +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 n381ZHDk027251; Wed, 8 Apr 2009 10:35:17 +0900 (JST) Date: Wed, 08 Apr 2009 10:35:01 +0900 (JST) Message-Id: <20090408.103501.38023937.garrigue@math.nagoya-u.ac.jp> To: goswin-v-b@web.de Cc: caml-list@inria.fr Subject: Re: [Caml-list] Subtyping From: Jacques Garrigue In-Reply-To: <87bpr86kti.fsf@frosties.localdomain> References: <873aclf1z8.fsf@frosties.localdomain> <87bpr86kti.fsf@frosties.localdomain> 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-Spam: no; 0.00; subtyping:01 ocaml:01 iter:01 iter:01 existential:01 syntax:01 ocaml:01 verbose:01 1.2:98 disturbing:98 1.2:98 abstract:01 caml-list:01 int:01 int:01 From: Goswin von Brederlow > Small extra question concerning this. Can I get ocaml to recognise a > type like this? > > type base = 'a. { > x : 'a; > fn : 'a -> unit; > } > > List.iter > (fun r -> r.fn r) > [{x = 1; fn = (fun r -> print_int r.x); }; > {x = 1.2; fn = (fun r -> print_float r.x); }] > > The difference to a "'a base" type would be that the 'a is only > infered and fixed inside the record but remains abstract outside of > it. First reaction: but your "base" type is just a closure. But this is certainly not your question. More disturbing: the rest of your code does not agree with base. So I will assume you actually meant List.iter (fun r -> r.fn r.x) [{x = 1; fn = print_int}; {x = 1.2; fn = print_float}] Then what you are asking for is existential types. There is no syntax for them in ocaml, but they can be encoded through universal types (interestingly, the dual is not true). type 'a base = {x : 'a; fn : 'a -> unit} type 'b base_op = {bop: 'a. 'a base -> 'b} type base_wrapper = {base: 'b. 'b base_op -> 'b} let l = let a = {x = 1; fn = print_int} and b = {x = 1.2; fn = print_float} in [{base = fun x -> x.bop a}; {base = fun x -> x.bop b}] List.iter (fun w -> w.base {bop = fun r -> r.fn r.x}) l As you can see, the result is rather verbose, but this works. Fortunately, closure and objects are usually enough... Jacques Garrigue