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 5FC42BBAF for ; Wed, 8 Apr 2009 04:43:55 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEANar20mCNhAB/2dsb2JhbADNKIN7Bg X-IronPort-AV: E=Sophos;i="4.39,341,1235948400"; d="scan'208";a="25845541" Received: from 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 04:43:53 +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 n382hnYQ013594; Wed, 8 Apr 2009 11:43:49 +0900 (JST) Date: Wed, 08 Apr 2009 11:43:35 +0900 (JST) Message-Id: <20090408.114335.221144129.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: <20090408.103501.38023937.garrigue@math.nagoya-u.ac.jp> References: <873aclf1z8.fsf@frosties.localdomain> <87bpr86kti.fsf@frosties.localdomain> <20090408.103501.38023937.garrigue@math.nagoya-u.ac.jp> 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 iter:01 existential:01 syntax:01 ocaml:01 iter:01 verbose:01 1.2:98 1.2:98 closures:01 caml-list:01 functions:01 int:01 int:01 closure:01 Here is a slightly more usable version, using helper functions to avoid writing intermediate closures by hand. type 'a base = {x : 'a; fn : 'a -> unit} (* 4 next lines are boilerplate, could be auto-generated *) type 'b base_op = {bop: 'a. 'a base -> 'b} type base_wrapper = {base: 'b. 'b base_op -> 'b} let wrap a = {base = fun x -> x.bop a} let apply op w = w.base op let l = [wrap {x = 1; fn = print_int}; wrap {x = 1.2; fn = print_float}];; List.iter (apply {bop = fun r -> r.fn r.x}) l The only thing you cannot abbreviate is the {bop = ...} part, as this is where universality is checked, to ensure that no cross application can be done. Jacques Garrigue > 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...