caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Create a constraint between variant type and data list
@ 2010-09-03 17:16 Sylvain Le Gall
  2010-09-03 17:38 ` [Caml-list] " bluestorm
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Sylvain Le Gall @ 2010-09-03 17:16 UTC (permalink / raw)
  To: caml-list

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.

Thanks all,
Sylvain Le Gall


^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: Create a constraint between variant type and data list
@ 2010-09-04 10:17 oleg
  0 siblings, 0 replies; 13+ messages in thread
From: oleg @ 2010-09-04 10:17 UTC (permalink / raw)
  To: sylvain; +Cc: caml-list


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).




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2010-09-17  8:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-03 17:16 Create a constraint between variant type and data list Sylvain Le Gall
2010-09-03 17:38 ` [Caml-list] " bluestorm
2010-09-03 21:28   ` Sylvain Le Gall
2010-09-17  7:29     ` [Caml-list] " Maxence Guesdon
2010-09-03 18:51 ` [Caml-list] " Martin Jambon
2010-09-03 19:39   ` Ashish Agarwal
2010-09-03 21:13 ` Maxence Guesdon
2010-09-03 21:25   ` Sylvain Le Gall
2010-09-04  6:35 ` [Caml-list] " Julien Signoles
2010-09-04  6:40   ` Julien Signoles
2010-09-04 16:25 ` Anil Madhavapeddy
2010-09-17  8:57 ` Sylvain Le Gall
2010-09-04 10:17 oleg

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).