Skip to content

Commit 377b94e

Browse files
authored
Merge pull request #390 from jmid/frequency-renaming
The Great Renaming, Chapter 16: `frequency` renaming
2 parents 2339ce4 + 20ba9c3 commit 377b94e

File tree

15 files changed

+165
-82
lines changed

15 files changed

+165
-82
lines changed

README.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ let tree_gen = QCheck.Gen.(sized @@ fix
183183
(fun self n -> match n with
184184
| 0 -> map leaf nat
185185
| n ->
186-
frequency
186+
oneof_weighted
187187
[1, map leaf nat;
188188
2, map2 node (self (n/2)) (self (n/2))]
189189
));;
@@ -221,7 +221,7 @@ generation of lists, arrays, and a choice function.
221221
Then, we define `arbitrary_tree`, a `tree QCheck.arbitrary` value, which
222222
contains everything needed for testing on trees:
223223

224-
- a random generator (mandatory), weighted with `frequency` to
224+
- a random generator (mandatory), weighted with `oneof_weighted` to
225225
increase the chance of generating deep trees
226226
- a printer (optional), very useful for printing counterexamples
227227
- a *shrinker* (optional), very useful for trying to reduce big
@@ -313,7 +313,7 @@ let tree_gen = QCheck2.Gen.(sized @@ fix
313313
(fun self n -> match n with
314314
| 0 -> map leaf nat
315315
| n ->
316-
frequency
316+
oneof_weighted
317317
[1, map leaf nat;
318318
2, map2 node (self (n/2)) (self (n/2))]
319319
));;

example/QCheck_runner_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ let gen_tree = QCheck.Gen.(sized @@ fix
157157
(fun self n -> match n with
158158
| 0 -> map leaf nat
159159
| n ->
160-
frequency
160+
oneof_weighted
161161
[1, map leaf nat;
162162
2, map2 node (self (n/2)) (self (n/2))]
163163
))

example/alcotest/QCheck_alcotest_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let gen_tree = QCheck.Gen.(sized @@ fix
4242
(fun self n -> match n with
4343
| 0 -> map leaf nat
4444
| n ->
45-
frequency
45+
oneof_weighted
4646
[1, map leaf nat;
4747
2, map2 node (self (n/2)) (self (n/2))]
4848
))

example/ounit/QCheck_ounit_test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let gen_tree = QCheck.Gen.(sized @@ fix
4545
(fun self n -> match n with
4646
| 0 -> map leaf nat
4747
| n ->
48-
frequency
48+
oneof_weighted
4949
[1, map leaf nat;
5050
2, map2 node (self (n/2)) (self (n/2))]
5151
))

src/core/QCheck.ml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,24 @@ module Gen = struct
142142
let oneof_array xs st = Array.get xs (Random.State.int st (Array.length xs))
143143
let oneofa = oneof_array
144144

145-
let frequencyl l st =
145+
let oneof_weighted_list l st =
146146
let sums = sum_int (List.map fst l) in
147147
let i = Random.State.int st sums in
148148
let rec aux acc = function
149149
| ((x,g)::xs) -> if i < acc+x then g else aux (acc+x) xs
150-
| _ -> failwith "frequency"
150+
| _ -> failwith "Gen.oneof_weighted_list"
151151
in
152152
aux 0 l
153153

154-
let frequencya a = frequencyl (Array.to_list a)
154+
let frequencyl = oneof_weighted_list
155155

156-
let frequency l st = frequencyl l st st
156+
let oneof_weighted_array a = oneof_weighted_list (Array.to_list a)
157+
158+
let frequencya = oneof_weighted_array
159+
160+
let oneof_weighted l st = oneof_weighted_list l st st
161+
162+
let frequency = oneof_weighted
157163

158164
let int_pos_small st =
159165
let p = RS.float st 1. in
@@ -1873,19 +1879,26 @@ let always ?print x =
18731879
make ?print gen
18741880

18751881
(** like oneof, but with weights *)
1876-
let frequency ?print ?small ?shrink ?collect l =
1882+
let oneof_weighted ?print ?small ?shrink l =
18771883
let first = snd (List.hd l) in
18781884
let small = _opt_sum small first.small in
18791885
let print = _opt_sum print first.print in
18801886
let shrink = _opt_sum shrink first.shrink in
1881-
let collect = _opt_sum collect first.collect in
18821887
let gens = List.map (fun (x,y) -> x, y.gen) l in
1883-
make ?print ?small ?shrink ?collect (Gen.frequency gens)
1888+
make ?print ?small ?shrink (Gen.oneof_weighted gens)
1889+
1890+
let frequency ?print ?small ?shrink ?collect l =
1891+
let arb = oneof_weighted ?print ?small ?shrink l in
1892+
match collect with
1893+
| None -> arb
1894+
| Some c -> set_collect c arb
18841895

18851896
(** Given list of [(frequency,value)] pairs, returns value with probability proportional
18861897
to given frequency *)
1887-
let frequencyl ?print ?small l = make ?print ?small (Gen.frequencyl l)
1888-
let frequencya ?print ?small l = make ?print ?small (Gen.frequencya l)
1898+
let oneof_weighted_list ?print ?small l = make ?print ?small (Gen.oneof_weighted_list l)
1899+
let frequencyl = oneof_weighted_list
1900+
let oneof_weighted_array ?print ?small a = make ?print ?small (Gen.oneof_weighted_array a)
1901+
let frequencya = oneof_weighted_array
18891902

18901903
let map_same_type f a =
18911904
adapt_ a (fun st -> f (a.gen st))

src/core/QCheck.mli

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ all rights reserved.
6969
(fun self n -> match n with
7070
| 0 -> map leaf nat
7171
| n ->
72-
frequency
72+
oneof_weighted
7373
[1, map leaf nat;
7474
2, map2 node (self (n/2)) (self (n/2))]
7575
))
@@ -224,17 +224,35 @@ module Gen : sig
224224
@raise Invalid_argument or Failure if list is empty
225225
@deprecated use {!oneof_array} instead. *)
226226

227+
val oneof_weighted : (int * 'a t) list -> 'a t
228+
(** Constructs a generator that selects among a given list of generators.
229+
Each of the given generators are chosen based on a positive integer weight.
230+
@since NEXT_RELEASE *)
231+
227232
val frequency : (int * 'a t) list -> 'a t
228233
(** Constructs a generator that selects among a given list of generators.
229-
Each of the given generators are chosen based on a positive integer weight. *)
234+
Each of the given generators are chosen based on a positive integer weight.
235+
@deprecated use {!oneof_weighted} instead. *)
236+
237+
val oneof_weighted_list : (int * 'a) list -> 'a t
238+
(** Constructs a generator that selects among a given list of values.
239+
Each of the given values are chosen based on a positive integer weight.
240+
@since NEXT_RELEASE *)
230241

231242
val frequencyl : (int * 'a) list -> 'a t
232243
(** Constructs a generator that selects among a given list of values.
233-
Each of the given values are chosen based on a positive integer weight. *)
244+
Each of the given values are chosen based on a positive integer weight.
245+
@deprecated use {!oneof_weighted_list} instead. *)
246+
247+
val oneof_weighted_array : (int * 'a) array -> 'a t
248+
(** Constructs a generator that selects among a given array of values.
249+
Each of the array entries are chosen based on a positive integer weight.
250+
@since NEXT_RELEASE *)
234251

235252
val frequencya : (int * 'a) array -> 'a t
236253
(** Constructs a generator that selects among a given array of values.
237-
Each of the array entries are chosen based on a positive integer weight. *)
254+
Each of the array entries are chosen based on a positive integer weight.
255+
@deprecated use {!oneof_weighted_array} instead. *)
238256

239257
val shuffle_array : 'a array -> 'a array t
240258
(** Creates a generator of shuffled arrays.
@@ -735,7 +753,7 @@ module Gen : sig
735753
(fun self n -> match n with
736754
| 0 -> map leaf nat
737755
| n ->
738-
frequency
756+
oneof_weighted
739757
[1, map leaf nat;
740758
2, map2 node (self (n/2)) (self (n/2))]
741759
))
@@ -1782,19 +1800,38 @@ val oneof : 'a arbitrary list -> 'a arbitrary
17821800
val always : ?print:'a Print.t -> 'a -> 'a arbitrary
17831801
(** Always return the same element. *)
17841802

1803+
val oneof_weighted : ?print:'a Print.t -> ?small:('a -> int) ->
1804+
?shrink:'a Shrink.t -> (int * 'a arbitrary) list -> 'a arbitrary
1805+
(** Similar to {!oneof} but with frequencies.
1806+
@since NEXT_RELEASE *)
1807+
17851808
val frequency : ?print:'a Print.t -> ?small:('a -> int) ->
17861809
?shrink:'a Shrink.t -> ?collect:('a -> string) ->
17871810
(int * 'a arbitrary) list -> 'a arbitrary
1788-
(** Similar to {!oneof} but with frequencies. *)
1811+
(** Similar to {!oneof} but with frequencies.
1812+
@deprecated use {!oneof_weighted} instead. *)
1813+
1814+
val oneof_weighted_list : ?print:'a Print.t -> ?small:('a -> int) ->
1815+
(int * 'a) list -> 'a arbitrary
1816+
(** Same as {!oneof_list}, but each element is paired with its frequency in
1817+
the probability distribution (the higher, the more likely).
1818+
@since NEXT_RELEASE *)
17891819

17901820
val frequencyl : ?print:'a Print.t -> ?small:('a -> int) ->
17911821
(int * 'a) list -> 'a arbitrary
17921822
(** Same as {!oneof_list}, but each element is paired with its frequency in
1793-
the probability distribution (the higher, the more likely). *)
1823+
the probability distribution (the higher, the more likely).
1824+
@deprecated use {!oneof_weighted_list} instead. *)
1825+
1826+
val oneof_weighted_array : ?print:'a Print.t -> ?small:('a -> int) ->
1827+
(int * 'a) array -> 'a arbitrary
1828+
(** Same as {!oneof_frequency_list}, but with an array.
1829+
@since NEXT_RELEASE *)
17941830

17951831
val frequencya : ?print:'a Print.t -> ?small:('a -> int) ->
17961832
(int * 'a) array -> 'a arbitrary
1797-
(** Same as {!frequencyl}, but with an array. *)
1833+
(** Same as {!oneof_frequency_list}, but with an array.
1834+
@deprecated use {!oneof_weighted_array} instead. *)
17981835

17991836
val map : ?rev:('b -> 'a) -> ('a -> 'b) -> 'a arbitrary -> 'b arbitrary
18001837
(** [map f a] returns a new arbitrary instance that generates values using

src/core/QCheck2.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,10 @@ module Gen = struct
646646
let small_signed_int = int_small
647647

648648
(** Shrink towards the first element of the list *)
649-
let frequency (l : (int * 'a t) list) : 'a t =
650-
if l = [] then failwith "QCheck2.frequency called with an empty list";
649+
let oneof_weighted (l : (int * 'a t) list) : 'a t =
650+
if l = [] then failwith "QCheck2.Gen.oneof_weighted called with an empty list";
651651
let sums = sum_int (List.map fst l) in
652-
if sums < 1 then failwith "QCheck2.frequency called with weight sum < 1";
652+
if sums < 1 then failwith "QCheck2.Gen.oneof_weighted called with weight sum < 1";
653653
int_bound (sums - 1)
654654
>>= fun i ->
655655
let rec aux acc = function
@@ -658,11 +658,17 @@ module Gen = struct
658658
in
659659
aux 0 l
660660

661-
let frequencyl (l : (int * 'a) list) : 'a t =
661+
let frequency = oneof_weighted
662+
663+
let oneof_weighted_list (l : (int * 'a) list) : 'a t =
662664
List.map (fun (weight, value) -> (weight, pure value)) l
663-
|> frequency
665+
|> oneof_weighted
666+
667+
let frequencyl = oneof_weighted_list
668+
669+
let oneof_weighted_array a = oneof_weighted_list (Array.to_list a)
664670

665-
let frequencya a = frequencyl (Array.to_list a)
671+
let frequencya = oneof_weighted_array
666672

667673
let char_range ?(origin : char option) (a : char) (b : char) : char t =
668674
(int_range ~origin:(Char.code (Option.value ~default:a origin)) (Char.code a) (Char.code b)) >|= Char.chr

src/core/QCheck2.mli

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ content will appear. *)
8282
(fun self n -> match n with
8383
| 0 -> map leaf nat
8484
| n ->
85-
frequency
85+
oneof_weighted
8686
[1, map leaf nat;
8787
2, map2 node (self (n/2)) (self (n/2))]
8888
));;
@@ -721,25 +721,52 @@ module Gen : sig
721721
@deprecated use {!oneof_array} instead.
722722
*)
723723

724+
val oneof_weighted : (int * 'a t) list -> 'a t
725+
(** Constructs a generator that selects among a given list of generators.
726+
Each of the given generators are chosen based on a positive integer weight.
727+
728+
Shrinks towards the first element of the list.
729+
@since NEXT_RELEASE
730+
*)
731+
724732
val frequency : (int * 'a t) list -> 'a t
725733
(** Constructs a generator that selects among a given list of generators.
726734
Each of the given generators are chosen based on a positive integer weight.
727735
728736
Shrinks towards the first element of the list.
737+
@deprecated use {!oneof_weighted} instead.
738+
*)
739+
740+
val oneof_weighted_list : (int * 'a) list -> 'a t
741+
(** Constructs a generator that selects among a given list of values.
742+
Each of the given values are chosen based on a positive integer weight.
743+
744+
Shrinks towards the first element of the list.
745+
@since NEXT_RELEASE
729746
*)
730747

731748
val frequencyl : (int * 'a) list -> 'a t
732749
(** Constructs a generator that selects among a given list of values.
733750
Each of the given values are chosen based on a positive integer weight.
734751
735752
Shrinks towards the first element of the list.
753+
@deprecated use {!oneof_weighted_list} instead.
754+
*)
755+
756+
val oneof_weighted_array : (int * 'a) array -> 'a t
757+
(** Constructs a generator that selects among a given array of values.
758+
Each of the array entries are chosen based on a positive integer weight.
759+
760+
Shrinks towards the first element of the array.
761+
@since NEXT_RELEASE
736762
*)
737763

738764
val frequencya : (int * 'a) array -> 'a t
739765
(** Constructs a generator that selects among a given array of values.
740766
Each of the array entries are chosen based on a positive integer weight.
741767
742768
Shrinks towards the first element of the array.
769+
@deprecated use {!oneof_weighted_array} instead.
743770
*)
744771

745772
(** {3 Shuffling elements} *)
@@ -1068,7 +1095,7 @@ module Gen : sig
10681095
(fun self n -> match n with
10691096
| 0 -> map leaf nat
10701097
| n ->
1071-
frequency
1098+
oneof_weighted
10721099
[1, map leaf nat;
10731100
2, map2 node (self (n/2)) (self (n/2))]
10741101
))
@@ -1185,18 +1212,18 @@ module Gen : sig
11851212
then int >|= Result.ok
11861213
else string_printable >|= Result.error)
11871214
1188-
(* Another allternative syntax with OCaml 4.08+ binding operators *)
1215+
(* Another alternative syntax with OCaml 4.08+ binding operators *)
11891216
let int_string_result : (int, string) result Gen.t = Gen.(
11901217
let* n = int_range 0 9 in
11911218
if n < 9
11921219
then int >|= Result.ok
11931220
else string_printable >|= Result.error)
11941221
]}
11951222
1196-
Note that this particular use case can be simplified by using [frequency]:
1223+
Note that this particular use case can be simplified by using [oneof_weighted]:
11971224
{[
11981225
let int_string_result : (int, string) result Gen.t = Gen.(
1199-
frequency [
1226+
oneof_weighted [
12001227
(9, int >|= Result.ok);
12011228
(1, string_printable >|= Result.error)
12021229
])

src/ppx_deriving_qcheck/QCheck_generators.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ let pure ~loc ~version e =
6868
let gen = with_prefix_gen loc version "pure" in
6969
apply1 loc gen e
7070

71-
let frequency ~loc ~version l =
71+
let oneof_weighted ~loc ~version l =
7272
match l with
7373
| [%expr [([%e? _], [%e? x])]] -> x
7474
| _ ->
75-
let gen = with_prefix_gen loc version "frequency" in
75+
let gen = with_prefix_gen loc version "oneof_weighted" in
7676
apply1 loc gen l
7777

7878
let map ~loc ~version pat expr gen =
@@ -167,7 +167,7 @@ module Make (Version : sig val version : version end) = struct
167167
let list ~loc = list ~loc ~version
168168
let array ~loc = array ~loc ~version
169169
let pure ~loc x = pure ~loc ~version x
170-
let frequency ~loc l = frequency ~loc ~version l
170+
let oneof_weighted ~loc l = oneof_weighted ~loc ~version l
171171
let map ~loc pat expr gen = map ~loc ~version pat expr gen
172172
let pair ~loc a b = pair ~loc ~version a b
173173
let triple ~loc a b c = triple ~loc ~version a b c

0 commit comments

Comments
 (0)