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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 063E0BC57 for ; Fri, 3 Sep 2010 20:51:30 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApYAAOvggExCbwQZkGdsb2JhbAChCRUBAQEBCQkMBxEEHr92BYU7hEU X-IronPort-AV: E=Sophos;i="4.56,314,1280700000"; d="scan'208";a="56645484" Received: from out1.smtp.messagingengine.com ([66.111.4.25]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 03 Sep 2010 20:51:29 +0200 Received: from compute2.internal (compute2.nyi.mail.srv.osa [10.202.2.42]) by gateway1.messagingengine.com (Postfix) with ESMTP id 57287458; Fri, 3 Sep 2010 14:51:27 -0400 (EDT) Received: from frontend2.messagingengine.com ([10.202.2.161]) by compute2.internal (MEProxy); Fri, 03 Sep 2010 14:51:27 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=messagingengine.com; h=message-id:date:from:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; s=smtpout; bh=V3Dm+2byJoUuNXZmQWOExcoXC6k=; b=McPn37Ft1VBI0vARKSp1/tVUC84HqFMIx81efVSllptsbgqPbgrSUnatQuNFUMnFmSkhLTZUVma/7JA25LkVL/leXUxt/x4GZ+Rd/quKVwcnaQ5BwczzL1L71DtGP6vRA2kZpDHTM3P1P85CCAyUAnTYjK0v7ookfuxiO0sgEiY= X-Sasl-enc: SgOz+PqgcKTORAimZUCtOMKIKJlrR8kA4elv/26PxYtV 1283539887 Received: from [192.168.1.61] (64-71-1-165.static.wiline.com [64.71.1.165]) by mail.messagingengine.com (Postfix) with ESMTPSA id E43115E16C7; Fri, 3 Sep 2010 14:51:26 -0400 (EDT) Message-ID: <4C8143AD.4060409@ens-lyon.org> Date: Fri, 03 Sep 2010 11:51:25 -0700 From: Martin Jambon User-Agent: Thunderbird 2.0.0.22 (X11/20090908) MIME-Version: 1.0 To: Sylvain Le Gall Cc: caml-list@inria.fr Subject: Re: [Caml-list] Create a constraint between variant type and data list References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; 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 polymorphic:01 gpl:01 gpl:01 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/