From: Christophe TROESTLER <Christophe.Troestler+ocaml@umh.ac.be>
To: till.varoquaux@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Re: Empty polymorphic variant type
Date: Thu, 07 Feb 2008 16:18:18 +0100 (CET) [thread overview]
Message-ID: <20080207.161818.541003217877080499.Christophe.Troestler+ocaml@umh.ac.be> (raw)
In-Reply-To: <9d3ec8300802060424j6fbe3378m8ac51d14cc399d6b@mail.gmail.com>
On Wed, 6 Feb 2008 12:24:21 +0000, Till Varoquaux wrote:
>
> On Feb 6, 2008 12:16 PM, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> > Polymorohic variants are great to encode permissions as phantom types.
> > This usage is hindered by the apparent impossibility to declare the
> > empty polymorphic type.
> >
> > To illustrate this use case I'll take a bogus example: suppose one is
> > handling accounts, you might want to be able to add a permission on
> > private information (NIN, passwords etc..). So you could be handling
> > accounts where you'd have read permission, write permission both or
> > none. By using a polymorphic variant type:
> >
> > type 'a account constraint 'a = [< `Read | `Write ];;
> >
> Small addendum:
>
> type 'a account constraint 'a = [< `Permission |`Read | `Write > `Permission ];;
>
> would work but it seems clunky and convoluted.
The empty polymorphic type may be declared with the camlp5 revised
syntax but does not behave as a universal polymorphic variant
subtype :
$ cat a.ml
type account 'a = 'b constraint 'a = [ < `Read | `Write ];
type t = account [= ];
$ ocamlc -pp camlp5r a.ml
File "a.ml", line 2, characters 17-21:
This type [ ] should be an instance of type [< `Read | `Write ]
These two variant types have no intersection
IMHO I guess your best solution for readability (at least in the code
you write, not always for the generated types) ATM is to define type
aliases:
type 'a account constraint 'a = [< `None |`Read | `Write > `None ]
type none = [ `None ]
type read = [ `None | `Read ]
type write = [ `None | `Write ]
Then you can have :
val x : [> read] account
val y : [> write] account
# [x; y; z];;
- : [ `None | `Read | `Write ] account list = [<abstr>; <abstr>; <abstr>]
# let f (x : [< read ] account) = ...
# let g (x : [ read | write ] account) = ...
My 0.02€,
ChriS
prev parent reply other threads:[~2008-02-07 15:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-06 12:16 Till Varoquaux
2008-02-06 12:24 ` Till Varoquaux
2008-02-06 12:33 ` [Caml-list] " Bünzli Daniel
2008-02-06 13:13 ` Till Varoquaux
2008-02-07 15:18 ` Christophe TROESTLER [this message]
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=20080207.161818.541003217877080499.Christophe.Troestler+ocaml@umh.ac.be \
--to=christophe.troestler+ocaml@umh.ac.be \
--cc=caml-list@inria.fr \
--cc=till.varoquaux@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).