package ppx_enumerate
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=a27f1797b1315bdf7678fde783dff493bd348f1c5b644d7616b660bd295dad36
Description
Part of the Jane Street's PPX rewriters collection.
Added to opam-repository:
README
ppx_enumerate
Generate a list containing all values of a finite type.
ppx_enumerate is a ppx rewriter which generates a definition for the list of all values of a type with (for a type which only has finitely many values).
Basic Usage
The basic usage is simply to add "[@@deriving enumerate]" after the type definition. For example:
type t =
| Foo
| Bar of bool
| Baz of [`A | `B of unit option]
[@@deriving enumerate]will produce a value val all : t list, whose value is equal to
[ Foo; Bar true; Bar false; Baz `A; Baz (`B None); Baz (`B Some ()) ]in some order (that is, there is no guarantee about the order of the list).
Polymorphic types
In a similar fashion as sexplib, using '[@@deriving enumerate]' on polymorphic types produces a function for [all]. For example,
type 'a t =
| Foo
| Bar of 'a option
[@@deriving enumerate]will produce a value val all : 'a list -> 'a t list, whose value is semantically equal to
fun all_of_a -> Foo :: Bar None :: List.map all_of_a ~f:(fun x -> Bar (Some x))Types not named t
If the type is not named t, then the enumeration is called all_of_<type_name> instead of all.
Records and Tuples
Product types are supported as well as sum types. For example,
type t =
{ foo : [`A | `B]
; bar : [`C | `D]
} [@@deriving enumerate]produces a val all : t list whose value is equal (up to order) to:
[ { foo = `A; bar = `C }; { foo = `A; bar = `D };
{ foo = `B; bar = `C }; { foo = `B; bar = `D };
]Tuples and variants with multiple arguments are similarly supported.
Overriding the all value
Just like with sexplib, it can sometimes be useful to provide a custom value of all. For example, you might define a type of bounded integers:
module Small_int : sig
type t = private int [@@deriving enumerate]
val create_exn : int -> t
end = struct
type t = int
let limit = 100
let create_exn i = if i < 0 || i >= limit then failwith "out of bounds"; i
let all = List.init limit ~f:(fun i -> i)
endYou could then use Small_int.t as normal with other types using [@@deriving enumerate]:
type t =
| Foo
| Bar of Small_int.t option
[@@deriving enumerate]Using all without defining a type name
You don't have to define a type name to be able to create the list of values of a type. You do it for any type expression by using the all quotation. For example:
[%all: bool * bool]which will evaluate to:
[ (true, true); (true, false); (false, false); (false, true) ]Known issues
Using all for polymorphic variants with duplicated constructors leads to duplicate values in the resulting lists:
type t = [ `A ] [@@deriving enumerate]
let () = assert ([%all: [ t | t ] ] = [ `A; `A ])
Dependencies (5)
-
ppxlib
>= "0.28.0" -
dune
>= "3.11.0" -
ppxlib_jane
>= "v0.17" & < "v0.18~" -
base
>= "v0.17" & < "v0.18~" -
ocaml
>= "5.1.0"
Dev Dependencies
None
Optional Dependencies
None
Used by (15)
- checked_oint
-
cmdlang-to-base
< "0.0.11" -
crs
< "0.0.20260307" - dunolint
-
dunolint-lib
< "0.0.20251006" - dunolint-lib-base
-
n_ary
>= "v0.17.0" -
ppx_bap
< "v0.14.0" -
ppx_base
>= "v0.17.0" - ppx_diff
-
ppx_log
>= "v0.17.0" -
ppx_typed_fields
>= "v0.17.0" -
smtml
>= "0.24.0" -
volgo
< "0.0.21" -
volgo-base
< "0.0.22"
Conflicts
None
Build status
Results of building ppx_enumerate with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| v0.17.0 (this version) | n/a | n/a | good | good | good |
| v0.16.0 | n/a | good | good | good | good |
| v0.15.0 | good | good | good | good | good |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure