From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 63EF4BC57 for ; Sat, 4 Sep 2010 18:25:51 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwEAPYPgkxZELGa/2dsb2JhbACTQ45KumeFPQSKFw X-IronPort-AV: E=Sophos;i="4.56,318,1280700000"; d="scan'208";a="66883067" Received: from recoil.dh.bytemark.co.uk (HELO dark.recoil.org) ([89.16.177.154]) by mail1-smtp-roc.national.inria.fr with SMTP; 04 Sep 2010 18:25:51 +0200 Received: (qmail 12867 invoked by uid 634); 4 Sep 2010 16:25:50 -0000 X-Spam-Level: * X-Spam-Check-By: dark.recoil.org Received: from avsm2.wolfson.cam.ac.uk (HELO [10.0.1.3]) (131.111.242.112) (smtp-auth username remote@recoil.org, mechanism cram-md5) by dark.recoil.org (qpsmtpd/0.83) with ESMTPA; Sat, 04 Sep 2010 17:25:45 +0100 Subject: Re: [Caml-list] Create a constraint between variant type and data list Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=us-ascii From: Anil Madhavapeddy In-Reply-To: Date: Sat, 4 Sep 2010 17:25:43 +0100 Cc: caml-list@inria.fr Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Sylvain Le Gall X-Mailer: Apple Mail (2.1081) X-Virus-Checked: Checked by ClamAV on dark.recoil.org X-Spam: no; 0.00; anil:01 anil:01 desc:01 lgpl:01 desc:01 lgpl:01 failwith:01 failwith:01 printf:01 printf:01 variants:01 variants:01 compiler:01 beginner's:01 ocaml:01 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 =3D GPL | LGPL with type_of,value (* For a type, generate a description string *) let desc_of_license =3D function |"GPL",_ -> "GNU General Public License" |"LGPL",_ -> "GNU Lesser GPL" |_ -> failwith "unknown license" (* Generate a list of license types *) let licenses =3D match type_of_license with=20 | Type.Ext ("license", Type.Sum ts) -> ts=20 | _ -> assert false (* =46rom a license type, generate a Value of that type *) let value_of_license_t =3D function |name,[] -> Value.Ext (("",0L), Value.Sum (name,[])) |name,_ -> failwith "no args allowed" (* Populate data with licenses and their description *) let data =3D List.map=20 (fun lic -> (license_of_value (value_of_license_t lic)) ,=20 (desc_of_license lic) ) licenses let _ =3D 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, >=20 > I would like to somehow enforce that a variant type is associated with > an entry in a data list.=20 >=20 > For example,=20 >=20 > I would like to define: >=20 > type license =3D GPL | LGPL=20 >=20 > and=20 >=20 > let data =3D [ GPL, "GNU Public license";=20 > LGPL, "GNU Lesser General Public license" ] >=20 >=20 > I would like to enforce that all variants of license are in the > association list. >=20 > I have tried to use polymorphic variants, but don't see how to enforce > this constraint. >=20 > 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. >=20 > 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. >=20 > Thanks all, > Sylvain Le Gall >=20 > _______________________________________________ > 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