Skip to content

Commit

Permalink
Refactor and improve design for recursive generators
Browse files Browse the repository at this point in the history
  • Loading branch information
vch9 committed Dec 9, 2021
1 parent 63f7b18 commit e5823ac
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 355 deletions.
25 changes: 13 additions & 12 deletions src/ppx_deriving_qcheck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,19 @@ let gen_color =
type tree = Leaf of int | Node of tree * tree
[@@deriving qcheck]
let gen_tree =
QCheck.Gen.sized @@
(QCheck.Gen.fix
(fun self -> function
| 0 -> QCheck.Gen.map (fun gen0 -> Leaf gen0) QCheck.Gen.int
| n ->
QCheck.Gen.frequency
[(1,
(QCheck.Gen.map (fun gen0 -> Leaf gen0) QCheck.Gen.int));
(1,
(QCheck.Gen.map (fun (gen0, gen1) -> Node (gen0, gen1))
(QCheck.Gen.pair (self (n / 2)) (self (n / 2)))))]))
(* ==> *)
let rec gen_tree_sized n =
match n with
| 0 -> QCheck.Gen.map (fun gen0 -> Leaf gen0) QCheck.Gen.int
| n ->
QCheck.Gen.frequency
[(1, (QCheck.Gen.map (fun gen0 -> Leaf gen0) QCheck.Gen.int));
(1,
(QCheck.Gen.map (fun (gen0, gen1) -> Node (gen0, gen1))
(QCheck.Gen.pair (self (n / 2)) (self (n / 2)))))]))
let gen_tree = QCheck.Gen.sized @@ gen_tree_sized
```

* Recursive polymorphic variants
Expand Down
Loading

0 comments on commit e5823ac

Please sign in to comment.