* Re: Create a constraint between variant type and data list
@ 2010-09-04 10:17 oleg
0 siblings, 0 replies; 5+ 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] 5+ messages in thread
* 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
` (2 more replies)
0 siblings, 3 replies; 5+ 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] 5+ messages in thread
* Re: [Caml-list] Create a constraint between variant type and data list
2010-09-03 17:16 Sylvain Le Gall
@ 2010-09-03 17:38 ` bluestorm
2010-09-03 21:28 ` Sylvain Le Gall
2010-09-03 21:13 ` [Caml-list] " Maxence Guesdon
2010-09-17 8:57 ` Sylvain Le Gall
2 siblings, 1 reply; 5+ messages in thread
From: bluestorm @ 2010-09-03 17:38 UTC (permalink / raw)
To: Sylvain Le Gall; +Cc: caml-list
Hi,
I do not have a direct solution to your problem.
If you want to associate a data with each case, you can use a pattern
matching, wich will do the exhaustiveness check :
let to_string : license -> _ = function
| `GPL -> "GPL"
| `LGPL -> "LGPL"
You will be warned if you add (or remove) some licenses and forget to
change the function.
You may, however, want to have a list of all licenses values, instead
of a case-handling on each value. I don't know how you could have the
compiler check completeness for you, but if you really want that
check, there is an extra-linguistic method : declare your value
without giving it a type annotation, so that the compiler will infer
the covered case, then use a script outside the program, calling
"ocamlc -i" to check the inferred signature, and comparing it to you
datatype declaration.
Finally, I have a third solution based on code generation : given my
first solution (turning the association list into a function), what
you need is only a list of all the constructors (and you can build
your assoc list with List.map (fun x -> x, assoc_function x)). This
can easily be generated from the datatype declaration using direct
camlp4, or Markus Mottl's type-conv (
http://www.ocaml.info/home/ocaml_sources.html#toc11 ).
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Create a constraint between variant type and data list
2010-09-03 17:38 ` [Caml-list] " bluestorm
@ 2010-09-03 21:28 ` Sylvain Le Gall
0 siblings, 0 replies; 5+ messages in thread
From: Sylvain Le Gall @ 2010-09-03 21:28 UTC (permalink / raw)
To: caml-list
On 03-09-2010, bluestorm <bluestorm.dylc@gmail.com> wrote:
> Hi,
>
> Finally, I have a third solution based on code generation : given my
> first solution (turning the association list into a function), what
> you need is only a list of all the constructors (and you can build
> your assoc list with List.map (fun x -> x, assoc_function x)). This
> can easily be generated from the datatype declaration using direct
> camlp4, or Markus Mottl's type-conv (
> http://www.ocaml.info/home/ocaml_sources.html#toc11 ).
>
Your answer and the one from Martin/Ashish, makes me think that I need
to go back to camlp4/type-conv... I would have like to avoid this
solution, but I think it is the best one.
Thanks for all your answer...
Regards,
Sylvain Le Gall
p.s.: this remains an open problem, so solutions are still welcome ;-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] 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
@ 2010-09-03 21:13 ` Maxence Guesdon
2010-09-03 21:25 ` Sylvain Le Gall
2010-09-17 8:57 ` Sylvain Le Gall
2 siblings, 1 reply; 5+ messages in thread
From: Maxence Guesdon @ 2010-09-03 21:13 UTC (permalink / raw)
To: caml-list
Le Fri, 3 Sep 2010 17:16:48 +0000 (UTC),
Sylvain Le Gall <sylvain@le-gall.net> a écrit :
> 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.
A solution is to add your new license to your list of associations, then
the compiler will complain about the unknown variant :)
Regards,
--
Maxence Guesdon
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Create a constraint between variant type and data list
2010-09-03 21:13 ` [Caml-list] " Maxence Guesdon
@ 2010-09-03 21:25 ` Sylvain Le Gall
0 siblings, 0 replies; 5+ messages in thread
From: Sylvain Le Gall @ 2010-09-03 21:25 UTC (permalink / raw)
To: caml-list
On 03-09-2010, Maxence Guesdon <maxence.guesdon@inria.fr> wrote:
> Le Fri, 3 Sep 2010 17:16:48 +0000 (UTC),
> Sylvain Le Gall <sylvain@le-gall.net> a écrit :
>
> A solution is to add your new license to your list of associations, then
> the compiler will complain about the unknown variant :)
>
This is my current solutions. I try to find something better ;-)
Regards,
Sylvain Le Gall
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: 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
2010-09-03 21:13 ` [Caml-list] " Maxence Guesdon
@ 2010-09-17 8:57 ` Sylvain Le Gall
2 siblings, 0 replies; 5+ messages in thread
From: Sylvain Le Gall @ 2010-09-17 8:57 UTC (permalink / raw)
To: caml-list
Hello all,
On 03-09-2010, Sylvain Le Gall <sylvain@le-gall.net> 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" ]
>
>
Thank you for all your answer. I pick the one from oleg@okmij.org, I
hide the license with a type and the creation of license is done in the
module. The to_string/from_string is done by registering extra data in
an Hashtable.
See the implementation here.
http://darcs.ocamlcore.org/cgi-bin/darcsweb.cgi?r=oasis;a=headblob;f=/src/oasis/OASISLicense.mli
http://darcs.ocamlcore.org/cgi-bin/darcsweb.cgi?r=oasis;a=headblob;f=/src/oasis/OASISLicense.ml
Regards,
Sylvain Le Gall
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-09-17 8:57 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-04 10:17 Create a constraint between variant type and data list oleg
-- strict thread matches above, loose matches on Subject: below --
2010-09-03 17:16 Sylvain Le Gall
2010-09-03 17:38 ` [Caml-list] " bluestorm
2010-09-03 21:28 ` Sylvain Le Gall
2010-09-03 21:13 ` [Caml-list] " Maxence Guesdon
2010-09-03 21:25 ` Sylvain Le Gall
2010-09-17 8:57 ` Sylvain Le Gall
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox