From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p6EFFAYR031026 for ; Thu, 14 Jul 2011 17:15:10 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhoCAKMHH06DbwiXnGdsb2JhbABTmCGPMhQBAQEBAQgLCQkUJcsJhjoEl2SLUg X-IronPort-AV: E=Sophos;i="4.65,529,1304287200"; d="scan'208";a="113154324" Received: from ppsw-51.csi.cam.ac.uk ([131.111.8.151]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Jul 2011 17:15:05 +0200 X-Cam-AntiVirus: no malware found X-Cam-SpamDetails: not scanned X-Cam-ScannerInfo: http://www.cam.ac.uk/cs/email/scanner/ Received: from hermes-2.csi.cam.ac.uk ([131.111.8.54]:56191) by ppsw-51.csi.cam.ac.uk (smtp.hermes.cam.ac.uk [131.111.8.158]:25) with esmtpa (EXTERNAL:lpw25) id 1QhNd2-0003vN-ZL (Exim 4.72) (return-path ); Thu, 14 Jul 2011 16:15:04 +0100 Received: from prayer by hermes-2.csi.cam.ac.uk (hermes.cam.ac.uk) with local (PRAYER:lpw25) id 1QhNd2-0007NR-UM (Exim 4.67) (return-path ); Thu, 14 Jul 2011 16:15:04 +0100 Received: from [128.232.9.0] by webmail.hermes.cam.ac.uk with HTTP (Prayer-1.3.4); 14 Jul 2011 16:15:04 +0100 Date: 14 Jul 2011 16:15:04 +0100 From: Leo P White To: Markus Mottl Cc: caml-list@inria.fr Message-ID: In-Reply-To: References: X-Mailer: Prayer v1.3.4 Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=ISO-8859-1 Sender: "L.P. White" Subject: Re: [Caml-list] Open datatypes Hi, Polymorphic variants aren't really the same as open datatypes like exn. Firstly the typechecking for polymorphic variants is much weaker, i.e. the same variant can be used with any polymorphic variant type. Secondly it is not possible to create a reference to a proper open polymorphic variant, while references to exn are allowed. And lastly it is not possible to create local polymorphic variants, along the same lines as a local exception. Regards, Leo On Jul 14 2011, Markus Mottl wrote: >Hi, > >it is possible to create open (i.e. extensible) datatypes with >polymorphic variants, even allowing recursion. E.g.: > > type 't add_open = [ `Add of 't * 't ] > type 't sub_open = [ `Sub of 't * 't ] > > let eval_add_open eval (`Add (l, r)) = eval l + eval r > let eval_sub_open eval (`Sub (l, r)) = eval l - eval r > >"add_open" and "sub_open" are clearly completely independent from each >other, both the datatypes and the evaluation functions. > >Now we combine these two datatypes and evaluation functions, still >leaving the result extensible: > > type 't add_sub_open = [ 't add_open | 't sub_open ] > > let eval_add_sub_open eval = function > | #add_open as t -> eval_add_open eval t > | #sub_open as t -> eval_sub_open eval t > >Eventually you will want to "close" the extensible definitions for >actual use. This basically just means tying the open ends: > > type add_sub = add_sub add_sub_open > > let rec eval_add_sub t = eval_add_sub_open eval_add_sub t > >In my experience using polymorphic variants for that purpose is hands >down the most elegant way of achieving extensibility and >composability. It is especially useful for creating domain-specific >languages that can be quickly combined and extended. > >Regards, >Markus > >On Thu, Jul 14, 2011 at 09:38, wrote: >> >> Hi all, >> >> I was wondering whether there was any particular reason why OCaml >> doesn't allow the user to create open extensible datatypes like exn. >> >> I know that something similar can be created using local exceptions: >> >> module T = struct >> type t = exn >> >> type 'a tvariant = (('a -> t), (t -> 'a option)) >> >> let createVariant (type s) () = >> let module M = struct exception E of s end in >> (fun x -> M.E x), (function M.E x -> Some x | _ -> None) >> >> let mkTVariant ((cnstr, _) : 'a tvariant) (x: 'a) = cnstr x >> >> let matchTVariant ((_, destr) : 'a tvariant) (xt: t) = destr xt >> end >> >> but it isn't very neat, and it seems that it would not be that >> difficult to allow the user to declare types with the same properties as >> exn. >> >> Thanks, >> >> Leo >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa-roc.inria.fr/wws/info/caml-list >> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs > >