From: Markus Mottl <markus.mottl@gmail.com>
To: lpw25@cam.ac.uk
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Open datatypes
Date: Thu, 14 Jul 2011 10:48:46 -0400 [thread overview]
Message-ID: <CAP_800pYXT7mvnuQs+RVK6vJaVU5cb1C0-uv6+98Do3t16D+qA@mail.gmail.com> (raw)
In-Reply-To: <sympa.1310649571.28612.849@inria.fr>
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, <lpw25@cam.ac.uk> 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
next prev parent reply other threads:[~2011-07-14 14:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-14 13:38 lpw25
2011-07-14 14:48 ` Markus Mottl [this message]
2011-07-14 15:10 ` Gerd Stolpmann
2011-07-14 16:05 ` Leo P White
2011-07-14 15:15 ` Leo P White
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAP_800pYXT7mvnuQs+RVK6vJaVU5cb1C0-uv6+98Do3t16D+qA@mail.gmail.com \
--to=markus.mottl@gmail.com \
--cc=caml-list@inria.fr \
--cc=lpw25@cam.ac.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox