The exenum library offers constructors to build enumerations for datatypes, that is, functions from (arbitrarily large) integers to values. Such enumerations are very useful for unit testing.
The library is efficient: the n-th element of an enumeration is returned without having computed the (n-1) previous elements. Complexity is in log(n), except for some pathological datatypes.
Homepage: https://github.com/lebotlan/ocaml-exenum
Inspired by Feat: Functional Enumeration of Algebraic Types, by Duregard, Jansson, Wang, Chalmers University.
Contact: D. Le Botlan (github.lebotlan@dfgh.met where you replace .met by .net.)
As an example, consider the following familiar datatype:
type term = Var of string | App of term * term | Lambda of string * term
Using exenum, one may easily generate zillions of different lambda-terms. For this example, we limit ourselves to four variable names : x, y, u, and v. Then, one may compute for instance term number 2000000000000, which happens to be
((((x v) (fun u -> y)) ((fun u -> y) (fun y -> y))) (((x v) (fun u -> v)) (fun u -> y)))
Building an enumeration from a datatype is straightforward. For instance, the enumeration corresponding to type term
is
built as follows:
(* We restrict ourselves to four variable names. *)
let e_vars = from_list ~name:"variables" ["x" ; "y" ; "u" ; "v"]
(* Type term is recursive, hence we need a lazy enumeration first. *)
let rec e_term = lazy
begin
(* In order to use the enumeration recursively, we need to "pay" a recursive fee. *)
let r_term = pay e_term in
(* Now, this is the direct translation of the datatype definition. *)
union
[ map e_vars (fun x -> Var x) ;
map (pair r_term r_term) (fun (t1, t2) -> App (t1, t2)) ;
map (pair e_vars r_term) (fun (x, t) -> Lambda (x, t)) ]
end
(* Here is the enumeration for lambda-terms. *)
let e_term = Lazy.force e_term
See examples in https://github.com/lebotlan/ocaml-exenum/tree/master/examples
type 'a enum
The type of exhaustive enumerations of values of type 'a. Enumerations can be finite of infinite.
val from_list : ?name:string ‑> 'a list ‑> 'a t
Builds a finite enumeration from a finite set of values. The name is used for nicer debugging.
val cardinal : 'a t ‑> Z.t option
cardinal enum
Returns the cardinality of enum
. None means infinity.
val get : 'a t ‑> Z.t ‑> 'a
get enum n
Returns the nth value of type 'a, starting at 0.
n
is greater or equal than the cardinality of enum
.For these enumerations of integers, do not expect the n-th value to be equal to the integer n. Integers are shuffled.
val e_bigint : Z.t t
All numbers: ] -infty, +infty [ This enumeration starts from 0 and alternates between positive and negative values.
val e_pos : int t
Strictly positive integers: [1, max_int] as an infinite enumeration (hence, non-injective).
val e_int : int t
All integers: [min_int, max_int]. This enumeration starts from 0 and alternates between positive and negative values. This enumeration is infinite, hence non-injective.
union enums
builds an enumeration from a union of enumerations.
If enums
are disjoint enumerations, the resulting enumeration is injective.
Convenience function to build an enumeration of pairs from two enumerations (derived from product and projection functions).
An enumeration is a possibly-infinite set of finite parts.
Recursive, infinite enumerations must be built by increasing the cost of each part.
The enumeration given in argument must be infinite (which is usually the case when building a recursive enumeration).
See examples to understand how to use pay
.
val show : 'a t ‑> ('a ‑> string) ‑> int ‑> int ‑> unit
show enum to_string index len
outputs values of the given enumeration, using the to_string
conversion function,
from index index to index (index + len - 1).
val bigshow : 'a t ‑> ('a ‑> string) ‑> Z.t ‑> int ‑> unit
bigshow enum to_string index len
is similar to show
, except that index is a Z.t.
val tester : 'a t ‑> ?from:Z.t ‑> ?upto:Z.t ‑> ?verbose_period:int ‑> ?tos:('a ‑> string) ‑> len:int ‑> ('a ‑> unit) ‑> unit
tester enum ~len f
applies f in sequence to different values of the enumeration.
First, len
values are taken starting at 0 (or starting from from
, if specified).
Then, the current index is multiplied by 2 (that is, we start at 2*len) and again len
values are considered.
This is repeated forever (or while the current index is lower than the upper bound upto
).
If verbose_period
is strictly positive, a message giving the current index is printed on stdout every verbose_period
tests.
If tos is given, the test value is printed too.
Typical usage is: tester enum ~len:10000 f
, where f is a testing function.
len <= 0