From: Anil Madhavapeddy <anil@recoil.org>
To: Sylvain Le Gall <sylvain@le-gall.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Create a constraint between variant type and data list
Date: Sat, 4 Sep 2010 17:25:43 +0100 [thread overview]
Message-ID: <CFE193C7-7F17-4BF9-9AB8-7E24B29B332F@recoil.org> (raw)
In-Reply-To: <slrni82bc0.skq.sylvain@gallu.homelinux.org>
Here's a (very) quick and dirty implementation that will automatically populate data, using our dynamic typing library ( http://github.com/mirage/dyntype ). You just need to fill in desc_of_license with the matching descriptions, and data will be auto-populated at program startup.
-anil
--
type license = GPL | LGPL with type_of,value
(* For a type, generate a description string *)
let desc_of_license = function
|"GPL",_ -> "GNU General Public License"
|"LGPL",_ -> "GNU Lesser GPL"
|_ -> failwith "unknown license"
(* Generate a list of license types *)
let licenses = match type_of_license with
| Type.Ext ("license", Type.Sum ts) -> ts
| _ -> assert false
(* From a license type, generate a Value of that type *)
let value_of_license_t = function
|name,[] -> Value.Ext (("",0L), Value.Sum (name,[]))
|name,_ -> failwith "no args allowed"
(* Populate data with licenses and their description *)
let data = List.map
(fun lic ->
(license_of_value (value_of_license_t lic)) ,
(desc_of_license lic)
) licenses
let _ =
Printf.printf "GPL: %s\n%!" (List.assoc GPL data);
Printf.printf "LGPL: %s\n%!" (List.assoc LGPL data)
--
-anil
On 3 Sep 2010, at 18:16, Sylvain Le Gall wrote:
> Hello all,
>
> I would like to somehow enforce that a variant type is associated with
> an entry in a data list.
>
> For example,
>
> I would like to define:
>
> type license = GPL | LGPL
>
> and
>
> let data = [ GPL, "GNU Public license";
> LGPL, "GNU Lesser General Public license" ]
>
>
> I would like to enforce that all variants of license are in the
> association list.
>
> I have tried to use polymorphic variants, but don't see how to enforce
> this constraint.
>
> The point, is that if I add a new variant to license (e.g. BSD3), the
> compiler output an error because this new variant is not in data list.
>
> Any ideas ? If you need to use another type expression rather than
> variant, please do so, as long as I am able to link the license type
> and data list.
>
> Thanks all,
> Sylvain Le Gall
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
next prev parent reply other threads:[~2010-09-04 16:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-03 17:16 Sylvain Le Gall
2010-09-03 17:38 ` [Caml-list] " bluestorm
2010-09-03 21:28 ` Sylvain Le Gall
2010-09-17 7:29 ` [Caml-list] " Maxence Guesdon
2010-09-03 18:51 ` [Caml-list] " Martin Jambon
2010-09-03 19:39 ` Ashish Agarwal
2010-09-03 21:13 ` Maxence Guesdon
2010-09-03 21:25 ` Sylvain Le Gall
2010-09-04 6:35 ` [Caml-list] " Julien Signoles
2010-09-04 6:40 ` Julien Signoles
2010-09-04 16:25 ` Anil Madhavapeddy [this message]
2010-09-17 8:57 ` Sylvain Le Gall
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=CFE193C7-7F17-4BF9-9AB8-7E24B29B332F@recoil.org \
--to=anil@recoil.org \
--cc=caml-list@inria.fr \
--cc=sylvain@le-gall.net \
/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