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 38F95BC57 for ; Sat, 4 Sep 2010 08:35:39 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwBAK6FgUzRVdi2kGdsb2JhbACBRJ8NQQgVAQEBAQkJDAcRAx+hQIkzghWGGS6IOAEBAwWFNgSKFg X-IronPort-AV: E=Sophos;i="4.56,317,1280700000"; d="scan'208";a="66870932" Received: from mail-qy0-f182.google.com ([209.85.216.182]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Sep 2010 08:35:38 +0200 Received: by qyk4 with SMTP id 4so2855781qyk.6 for ; Fri, 03 Sep 2010 23:35:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=Lrvq4+ySY6jvijc8wxNw5YL2MWgQvm/MXABZLT4+DgE=; b=C9AQwxzghtNCyfjSRvyOm6+JZz+WMlRb82ODBqrWZm4N6cuYrziAxEH1IU8AKrP082 llkOn8oDAPiYQlu0do24eGYGm5dBUiJLbyYYQNU08BcTMMpW6DO8G7fitKP7Hj+HAxpY q5TnQphVeO+08rv5mJb7+QNa259aXXjjMlqa8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=a/8YSx1f4sHvufJjaEQtX+qOZXZz2uXGWwX0sPmFjTIqR5MAirVsCWxoc5UtWwaut5 ACwGDOP4gKnjwJNGrmyUoPNZSKg6eWOJV9kt3IC29+sqdXKsFDfXSSX0BVDlsNETMeNn DzZ1zbeglBpE0jJiryfTfB5gvTamqS5PryVZ0= MIME-Version: 1.0 Received: by 10.229.181.205 with SMTP id bz13mr1541593qcb.137.1283582137919; Fri, 03 Sep 2010 23:35:37 -0700 (PDT) Received: by 10.229.246.146 with HTTP; Fri, 3 Sep 2010 23:35:37 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 Sep 2010 08:35:37 +0200 Message-ID: Subject: Re: [Caml-list] Create a constraint between variant type and data list From: Julien Signoles To: Sylvain Le Gall Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=0016e68ce6f25ee9e1048f69417b X-Spam: no; 0.00; signoles:01 signoles:01 le-gall:01 lgpl:01 lgpl:01 variants:01 statically:01 ocaml:01 constructors:01 le-gall:01 variants:01 statically:01 ocaml:01 constructors:01 W8:98 --0016e68ce6f25ee9e1048f69417b Content-Type: text/plain; charset=ISO-8859-1 Hello Sylvain, 2010/9/3 Sylvain Le Gall > 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. > As said by other guys, you can't enforce statically such an invariant without a code generator. In pure ocaml, the below solution use a "social check" : it works only if the developer who adds a new constructor is not too bad and takes care of context when solving an error ;-). type license = GPL | LGPL let data = [ GPL, "GNU Public license"; LGPL, "GNU Lesser General Public license" ] let () = match GPL with | GPL | LGPL -> assert (let s = "Do not forget to add new constructors to the data list above." in Format.ifprintf Format.std_formatter "%s" s; true) Hope this helps, Julien --0016e68ce6f25ee9e1048f69417b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello Sylvain,

2010/9/3 Sylvain Le Gall <= span dir=3D"ltr"><sylvain@le-gall= .net>
I would like to define:

type license =3D GPL | LGPL

and

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


I would like to enforce that all variants of license are in the
association list.

As said by other guys, you can&#= 39;t enforce statically such an invariant without a code generator.
In = pure ocaml, the below solution use a "social check" : it works on= ly if the developer who adds a new constructor is not too bad and takes car= e of context when solving an error ;-).

type license =3D GPL | LGPL

let data =3D
=A0 [ GPL, "GN= U Public license";
=A0 =A0=A0 LGPL, "GNU Lesser General Public license" ]

let () =3D match GPL with
=A0 | GPL | LGPL ->
=A0=A0=A0=A0 assert
=A0=A0=A0=A0=A0=A0 (let s =3D "Do not forget to add new constructors t= o the data=20 list above." in
=A0=A0=A0=A0=A0=A0=A0=A0Format.ifprintf Format.std_formatter "%s"= s;
=A0=A0=A0=A0=A0=A0=A0 true)

Hope this helps,
Julien