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 p6EFAB5I030736 for ; Thu, 14 Jul 2011 17:10:11 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArwCAGUGH07U436rkGdsb2JhbABThEaTW48yFAEBAQEJCQ0HFAMiiHoCsRKRAQKBKYQAgQ8Ei2qLeoRchnc X-IronPort-AV: E=Sophos;i="4.65,529,1304287200"; d="scan'208";a="113154132" Received: from moutng.kundenserver.de ([212.227.126.171]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Jul 2011 17:10:06 +0200 Received: from office1.lan.sumadev.de (dslb-188-097-004-189.pools.arcor-ip.net [188.97.4.189]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0LqaYA-1RBmMI0Dp4-00eHqw; Thu, 14 Jul 2011 17:10:04 +0200 Received: from [192.168.5.106] (dslb-188-097-004-189.pools.arcor-ip.net [188.97.4.189]) by office1.lan.sumadev.de (Postfix) with ESMTPA id B93E75F702; Thu, 14 Jul 2011 17:10:03 +0200 (CEST) From: Gerd Stolpmann To: Markus Mottl Cc: lpw25@cam.ac.uk, caml-list@inria.fr In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Thu, 14 Jul 2011 17:10:02 +0200 Message-ID: <1310656202.10871.10.camel@thinkpad> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:vOc5afemSG7ClsayTL6I7Mu0IERl2Yt+qNky8lCeCRr 67TIWcx9Xah+lfExT4a65uFBoP+GvZKlDEill18ooZYajtSB3X EWdyBLIU0mbQA/TbDT7g8EDDAQHHELyyYvOJflqC90UhMGcnw1 Q4Mysg2b+V0x5CXG20Ahh1PYmt81rs+8Z7Hf7qXUPNlUmKfeuy M3e4kDrL62Ye6wXgXbnhQ== Subject: Re: [Caml-list] Open datatypes Am Donnerstag, den 14.07.2011, 10:48 -0400 schrieb Markus Mottl: > 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. Especially, polymorphic variants are not restricted to monomorphic types as exn is. For a complete example look here: https://godirepo.camlcity.org/svn/lib-pxp/trunk/src/pxp-engine/pxp_xpath.ml. It's an attempt to define xpath as an open polymorphic variant. The type open_expr is the open version of the xpath terms, expr is the closed. The corresponding evaluators are eval_open_expr and eval_expr. There is also a paper by Jacques Garrigue about this topic, but I cannot find it anymore in the web. Gerd > > 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 > > -- > Markus Mottl http://www.ocaml.info markus.mottl@gmail.com > > -- ------------------------------------------------------------ Gerd Stolpmann, Bad Nauheimer Str.3, 64289 Darmstadt,Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de Phone: +49-6151-153855 Fax: +49-6151-997714 ------------------------------------------------------------