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
_______________________________________________
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
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 ).
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
I haven't used it myself so cannot comment on how well it works.
> 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
This is my current solutions. I try to find something better ;-)
Regards,
Sylvain Le Gall
2010/9/3 Sylvain Le Gall <syl...@le-gall.net>
> 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
> | 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)
>
Sorry could be much better like this:
let () = match GPL with GPL | LGPL ->
ignore "Do not forget to add new constructors to the data list above."
--
Julien
> 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).
-anil
--
type license = GPL | LGPL with type_of,value
(* For a type, generate a description string *)
let desc_of_license = function
|"GPL",_ -> "GNU General Public License"
|"LGPL",_ -> "GNU Lesser GPL"
|_ -> failwith "unknown license"
(* Generate a list of license types *)
let licenses = match type_of_license with
| Type.Ext ("license", Type.Sum ts) -> ts
| _ -> assert false
(* From a license type, generate a Value of that type *)
let value_of_license_t = function
|name,[] -> Value.Ext (("",0L), Value.Sum (name,[]))
|name,_ -> failwith "no args allowed"
(* Populate data with licenses and their description *)
let data = List.map
(fun lic ->
(license_of_value (value_of_license_t lic)) ,
(desc_of_license lic)
) licenses
let _ =
Printf.printf "GPL: %s\n%!" (List.assoc GPL data);
Printf.printf "LGPL: %s\n%!" (List.assoc LGPL data)
--
-anil
> On 03-09-2010, bluestorm <bluesto...@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.
Hello,
Here is another solution, based on oug[1]. The idea is to:
1. make oug analyse your source files and dump the result,
2. write a program loading the dump and performing the checks you want.
Here is an example, which performs 1. and 2., with only one source file "foo.ml".
=== myoug.ml
let (data, _) = Ouglib.Analyze.analyze ["foo.ml"];;
let number_variants =
Ouglib.Lang.filter_elements data
(Ouglib.Lang.Name
{
Ouglib.Lang.sel_name = "Foo.number.*" ;
sel_kind = [ Ouglib.Data.Type_field ] ;
}
)
;;
let numbers_list_id =
match
Ouglib.Lang.filter_elements data
(Ouglib.Lang.Name
{
Ouglib.Lang.sel_name = "Foo.numbers" ;
sel_kind = [ Ouglib.Data.Value ] ;
}
)
with
[id] -> id
| _ -> assert false
;;
let graph = data.Ouglib.Data.graph ;;
let created_by_numbers_list =
let l = Ouglib.Graph.succ graph numbers_list_id in
let f acc (id, k) =
match k with
Ouglib.Data.Create -> id :: acc
| _ -> acc
in
List.fold_left f [] l
;;
let (_,missing) =
List.partition (fun id -> List.mem id created_by_numbers_list) number_variants
;;
match missing with
[] -> exit 0
| _ ->
let b = Buffer.create 256 in
Ouglib.Dump.print_element_list data b missing;
prerr_endline
(Printf.sprintf "The following constructors are not present in Foo.numbers:\n%s"
(Buffer.contents b)
);
exit 1
;;
=== /myoug.ml ==
=== foo.ml ===
type number = One | Two | Three | Four;;
let numbers = [ One, 1 ; Two, 2 ];;
=== /foo.ml ==
Compilation of myoug.ml to myoug.x:
# ocamlc -I +oug str.cma toplevellib.cma oug.cma -o myoug.x myoug.ml
Launching myoug.x gives the following:
The following constructors are not present in Foo.numbers:
f Foo.number.Three
f Foo.number.Four
Of course, you can adapt the program to fit your needs: have a makefile
target to create the oug dump and use this dump in various checking
programs, or in one program performing various checks.
Hope this helps,
Maxence
[1] http://home.gna.org/oug/index.fr.html
On 03-09-2010, Sylvain Le Gall <syl...@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 ol...@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,