From: oleg@okmij.org
To: sylvain@le-gall.net
Cc: caml-list@inria.fr
Subject: Re: Create a constraint between variant type and data list
Date: Sat, 4 Sep 2010 03:17:58 -0700 (PDT) [thread overview]
Message-ID: <20100904101758.4798517415@Adric.ern.nps.edu> (raw)
Sylvain Le Gall wrote:
> 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.
In other words, you would like a set of values of a distinct type,
each of which is associated with a printable string. One should be
able to add to the set in some `authorized way'; one should not be
able to make a value of that distinct type in an unauthorized way
(that is, by accident). Right?
It seems the following solution then would satisfy the specification:
module LICENSE : sig
type t
val print_lic : t -> string
val gpl : t
val lgpl : t
(* more can be added *)
end = struct
type t = string
let print_lic x = x
let gpl = "GNU Public license"
let lgpl = "GNU Lesser General Public license"
end;;
(* Usage example *)
type package = {pkg_name : string; pkg_lic : LICENSE.t};;
let packages = [{pkg_name = "gcc"; pkg_lic = LICENSE.gpl};
{pkg_name = "lgpled"; pkg_lic = LICENSE.lgpl}]
;;
let rec pr_lics = function [] -> ()
| {pkg_lic = l}::t -> Printf.printf "%s\n" (LICENSE.print_lic l);
pr_lics t
;;
let rec all_of_lic l = function [] -> []
| {pkg_name = name; pkg_lic = l'}::t when l' == l -> name :: all_of_lic l t
| _::t -> all_of_lic l t
;;
all_of_lic LICENSE.lgpl packages;;
One can't overlook specifying the description string. One can't create
new values of the type LICENSE.t `by accident' (the only possible
values of the type are the ones exported by the module).
next reply other threads:[~2010-09-04 10:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-04 10:17 oleg [this message]
-- strict thread matches above, loose matches on Subject: below --
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-03 21:13 ` [Caml-list] " Maxence Guesdon
2010-09-03 21:25 ` Sylvain Le Gall
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=20100904101758.4798517415@Adric.ern.nps.edu \
--to=oleg@okmij.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;
as well as URLs for NNTP newsgroup(s).