| 1 | ppx_enumerate |
| 2 | ============= |
| 3 | |
| 4 | Generate a list containing all values of a finite type. |
| 5 | |
| 6 | `ppx_enumerate` is a ppx rewriter which generates a definition for the |
| 7 | list of all values of a type with (for a type which only has finitely |
| 8 | many values). |
| 9 | |
| 10 | Basic Usage |
| 11 | ----------- |
| 12 | |
| 13 | The basic usage is simply to add "[@@deriving enumerate]" after the type |
| 14 | definition. For example: |
| 15 | |
| 16 | ```ocaml |
| 17 | type t = |
| 18 | | Foo |
| 19 | | Bar of bool |
| 20 | | Baz of [`A | `B of unit option] |
| 21 | [@@deriving enumerate] |
| 22 | ``` |
| 23 | |
| 24 | will produce a value `val all : t list`, whose value is equal to |
| 25 | |
| 26 | ```ocaml |
| 27 | [ Foo; Bar true; Bar false; Baz `A; Baz (`B None); Baz (`B Some ()) ] |
| 28 | ``` |
| 29 | |
| 30 | in some order (that is, there is no guarantee about the order of the list). |
| 31 | |
| 32 | Polymorphic types |
| 33 | ----------------- |
| 34 | |
| 35 | In a similar fashion as sexplib, using '[@@deriving enumerate]' on polymorphic |
| 36 | types produces a function for [all]. For example, |
| 37 | |
| 38 | ```ocaml |
| 39 | type 'a t = |
| 40 | | Foo |
| 41 | | Bar of 'a option |
| 42 | [@@deriving enumerate] |
| 43 | ``` |
| 44 | |
| 45 | will produce a value `val all : 'a list -> 'a t list`, whose value is |
| 46 | semantically equal to |
| 47 | |
| 48 | ```ocaml |
| 49 | fun all_of_a -> Foo :: Bar None :: List.map all_of_a ~f:(fun x -> Bar (Some x)) |
| 50 | ``` |
| 51 | |
| 52 | Types not named `t` |
| 53 | ------------------- |
| 54 | |
| 55 | If the type is not named `t`, then the enumeration is called |
| 56 | `all_of_<type_name>` instead of `all`. |
| 57 | |
| 58 | Records and Tuples |
| 59 | ------------------ |
| 60 | |
| 61 | Product types are supported as well as sum types. For example, |
| 62 | |
| 63 | ```ocaml |
| 64 | type t = |
| 65 | { foo : [`A | `B] |
| 66 | ; bar : [`C | `D] |
| 67 | } [@@deriving enumerate] |
| 68 | ``` |
| 69 | |
| 70 | produces a `val all : t list` whose value is equal (up to order) to: |
| 71 | |
| 72 | ```ocaml |
| 73 | [ { foo = `A; bar = `C }; { foo = `A; bar = `D }; |
| 74 | { foo = `B; bar = `C }; { foo = `B; bar = `D }; |
| 75 | ] |
| 76 | ``` |
| 77 | |
| 78 | Tuples and variants with multiple arguments are similarly supported. |
| 79 | |
| 80 | Overriding the `all` value |
| 81 | --------------------------- |
| 82 | |
| 83 | Just like with sexplib, it can sometimes be useful to provide a custom |
| 84 | value of `all`. For example, you might define a type of bounded |
| 85 | integers: |
| 86 | |
| 87 | ```ocaml |
| 88 | module Small_int : sig |
| 89 | type t = private int [@@deriving enumerate] |
| 90 | val create_exn : int -> t |
| 91 | end = struct |
| 92 | type t = int |
| 93 | let limit = 100 |
| 94 | let create_exn i = if i < 0 || i >= limit then failwith "out of bounds"; i |
| 95 | let all = List.init limit ~f:(fun i -> i) |
| 96 | end |
| 97 | ``` |
| 98 | |
| 99 | You could then use `Small_int.t` as normal with other types using |
| 100 | `[@@deriving enumerate]`: |
| 101 | |
| 102 | ```ocaml |
| 103 | type t = |
| 104 | | Foo |
| 105 | | Bar of Small_int.t option |
| 106 | [@@deriving enumerate] |
| 107 | ``` |
| 108 | |
| 109 | Using `all` without defining a type name |
| 110 | ---------------------------------------- |
| 111 | |
| 112 | You don't have to define a type name to be able to create the list of |
| 113 | values of a type. You do it for any type expression by using the `all` |
| 114 | quotation. For example: |
| 115 | |
| 116 | ```ocaml |
| 117 | [%all: bool * bool] |
| 118 | ``` |
| 119 | |
| 120 | which will evaluate to: |
| 121 | |
| 122 | ```ocaml |
| 123 | [ (true, true); (true, false); (false, false); (false, true) ] |
| 124 | ``` |
| 125 | |
| 126 | Known issues |
| 127 | ------------ |
| 128 | |
| 129 | Using `all` for polymorphic variants with duplicated constructors leads |
| 130 | to duplicate values in the resulting lists: |
| 131 | |
| 132 | ```ocaml |
| 133 | type t = [ `A ] [@@deriving enumerate] |
| 134 | let () = assert ([%all: [ t | t ] ] = [ `A; `A ]) |
| 135 | ``` |