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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 8808EBC57 for ; Fri, 3 Sep 2010 21:40:19 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvIBALLrgEzRVdY2kGdsb2JhbACTMoVVAYd5CBUBAQEBCQkMBxEDH6R4iTOCFYZKLogyAQEDBYU2BIFYiEE X-IronPort-AV: E=Sophos;i="4.56,314,1280700000"; d="scan'208";a="58528302" Received: from mail-bw0-f54.google.com ([209.85.214.54]) by mail2-smtp-roc.national.inria.fr with ESMTP; 03 Sep 2010 21:40:19 +0200 Received: by bwz20 with SMTP id 20so2553053bwz.27 for ; Fri, 03 Sep 2010 12:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type; bh=mBSS29haBBZjI7X3oxUpkFEGD44vchMi/srFIu6cRXQ=; b=pxK9rtxGnz5nAuRE6vVAGn/IbFmD40N2NUpLch2erypwih1Svsf1TYRHclIc+WXg71 1iQpEDiGSQnC7rlKPJdW2GmM7A0XknKmOowWbp1Wq/huwDE9kq1ZpQFqCwrUJHYePRSc Ko0t1kUksjBUi9g/L0Qwl0Mg0SkGurK3NV7jU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=v07ua9dHK4fCO2Qd8J7Ja7gUjrF2Bu19ZcJ1s989a0ykeI105bUd+juNsxIPbmpSOh ixWrY96phSVsGkRwpHCeCq6XWhcVxJx5UMv5Pa9vQ1Fk3+cgD4lRYFMyrUnfK+VB2h3V 5rXPNaFzGh/fZ1ec/Vnemy4gB3fxpq+cI8nus= Received: by 10.204.73.13 with SMTP id o13mr765830bkj.173.1283542818352; Fri, 03 Sep 2010 12:40:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.204.126.218 with HTTP; Fri, 3 Sep 2010 12:39:58 -0700 (PDT) In-Reply-To: <4C8143AD.4060409@ens-lyon.org> References: <4C8143AD.4060409@ens-lyon.org> From: Ashish Agarwal Date: Fri, 3 Sep 2010 15:39:58 -0400 Message-ID: Subject: Re: [Caml-list] Create a constraint between variant type and data list To: Sylvain Le Gall , caml-list@inria.fr Content-Type: multipart/alternative; boundary=001636c5bd85bdede4048f60191d X-Spam: no; 0.00; enum:01 wiki:01 ens-lyon:01 lgpl:01 lgpl:01 variants:01 variants:01 compiler:01 runtime:01 printf:01 printf:01 iter:01 iter:01 ocaml:01 beginner's:01 --001636c5bd85bdede4048f60191d Content-Type: text/plain; charset=ISO-8859-1 See the Enum section of deriving: http://code.google.com/p/deriving/wiki/Introduction I haven't used it myself so cannot comment on how well it works. On Fri, Sep 3, 2010 at 2:51 PM, Martin Jambon wrote: > 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. > > I don't see a solution other than meta-programming or runtime checks. > > Here is a simple code generator that would do the job: > > (* license_gen.ml *) > open Printf > > let print_licenses l = > printf "type license ="; > List.iter (fun (k, v) -> printf " | %s" k) l; > printf "\n"; > printf "let licences = [\n"; > List.iter (fun (k, v) -> printf " %s, %S;\n" k v) l; > printf "]\n" > > let () = > print_licenses [ > "GPL", "GNU Public license"; > "LGPL", "GNU Lesser General Public license"; > ] > > (* end *) > > $ ocaml license_gen.ml > license.ml > > > > Martin > > -- > http://mjambon.com/ > > _______________________________________________ > 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 > --001636c5bd85bdede4048f60191d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
See the Enum section of=A0deriving:

I have= n't used it myself so cannot comment on how well it works.


On Fri, Sep 3, 2010 at 2:51 PM, Martin Jambon <martin.jambon@ens-= lyon.org> wrote:
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 =3D GPL | LGPL
>
> and
>
> let data =3D [ GPL, "GNU Public license";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0LGPL, "GNU Lesser General Public licen= se" ]
>
>
> 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 enf= orce
> this constraint.
>
> The point, is that if I add a new variant to license (e.g. BSD3), the<= br> > 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.

I don't see a solution other than meta-programming or runtime che= cks.

Here is a simple code generator that would do the job:

(* license_gen.ml *= )
open Printf

let print_licenses l =3D
=A0printf "type license =3D";
=A0List.iter (fun (k, v) -> printf " | %s" k) l;
=A0printf "\n";
=A0printf "let licences =3D [\n";
=A0List.iter (fun (k, v) -> printf " =A0%s, %S;\n" k v) l; =A0printf "]\n"

let () =3D
=A0print_licenses [
=A0 =A0"GPL", "GNU Public license";
=A0 =A0"LGPL", "GNU Lesser General Public license"; =A0]

(* end *)

$ ocaml license_gen.ml<= /a> > license.ml



Martin

--
http://mjambon.com/

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--001636c5bd85bdede4048f60191d--