0 | module Hedgehog.Internal.Gen
  1 |
  2 | import Data.Bounded
  3 | import Data.Colist
  4 | import Data.Cotree
  5 | import Data.Fin
  6 | import Data.List
  7 | import Data.List.Quantifiers
  8 | import Data.List1
  9 | import Data.Nat
 10 | import Data.SOP
 11 | import Data.String
 12 | import Data.Tree
 13 | import Data.Vect
 14 | import Data.Vect.Quantifiers
 15 |
 16 | import Control.Monad.Maybe
 17 |
 18 | import Hedgehog.Internal.Range
 19 | import Hedgehog.Internal.Shrink
 20 | import Hedgehog.Internal.Util
 21 |
 22 | import System.Random.Pure.StdGen
 23 |
 24 | %hide Prelude.Range
 25 |
 26 | %default total
 27 |
 28 | --------------------------------------------------------------------------------
 29 | --          Random Value Generator with Integrated Shrinking
 30 | --------------------------------------------------------------------------------
 31 |
 32 | ||| Generates random values of type `a`
 33 | public export
 34 | record Gen (a : Type) where
 35 |   constructor MkGen
 36 |   unGen   : Size -> StdGen -> Cotree a
 37 |
 38 | public export %inline
 39 | runGen : Size -> StdGen -> Gen a -> Cotree a
 40 | runGen si se g = unGen g si se
 41 |
 42 | public export
 43 | mapGen :  (f : Cotree a -> Cotree b) -> Gen a -> Gen b
 44 | mapGen f (MkGen run) = MkGen $ \si,se => f (run si se)
 45 |
 46 | ||| Lift a predefined shrink tree in to a generator, ignoring the seed and the
 47 | ||| size.
 48 | public export
 49 | fromTree : Cotree a -> Gen a
 50 | fromTree ct = MkGen $ \_,_ => ct
 51 |
 52 | ||| Observe a generator's shrink tree.
 53 | public export
 54 | toTree : Gen a -> Gen (Cotree a)
 55 | toTree (MkGen unGen) = MkGen $ \si,se => pure (unGen si se)
 56 |
 57 | --------------------------------------------------------------------------------
 58 | --          Interface Implementations
 59 | --------------------------------------------------------------------------------
 60 |
 61 | public export
 62 | Functor Gen where
 63 |   map f (MkGen g) = MkGen $ \si,se => map f (g si se)
 64 |
 65 | public export
 66 | Applicative Gen where
 67 |   pure a = MkGen $ \_,_ => pure a
 68 |
 69 |   MkGen ff <*> MkGen fa =
 70 |     MkGen $ \si,se =>
 71 |       let (se1,se2) := split se
 72 |        in interleave (ff si se1) (fa si se2)
 73 |
 74 | public export
 75 | Monad Gen where
 76 |   MkGen run >>= f =
 77 |     MkGen $ \si,se =>
 78 |       let (se1,se2) := split se
 79 |           ta        := run si se1
 80 |        in bind ta (runGen si se2 . f)
 81 |
 82 | --------------------------------------------------------------------------------
 83 | --          Combinators
 84 | --------------------------------------------------------------------------------
 85 |
 86 | public export
 87 | generate : (Size -> StdGen -> a) -> Gen a
 88 | generate  f = MkGen $ \si,se => pure (f si se)
 89 |
 90 | --------------------------------------------------------------------------------
 91 | --          Shrinking
 92 | --------------------------------------------------------------------------------
 93 |
 94 | public export
 95 | shrink : (a -> Colist a) -> Gen a -> Gen a
 96 | shrink f = mapGen (expand f)
 97 |
 98 | public export %inline
 99 | prune : Gen a -> Gen a
100 | prune = mapGen prune
101 |
102 | --------------------------------------------------------------------------------
103 | --          Size
104 | --------------------------------------------------------------------------------
105 |
106 | ||| Construct a generator that depends on the size parameter.
107 | public export
108 | sized : (Size -> Gen a) -> Gen a
109 | sized f = generate (\si,_ => si) >>= f
110 |
111 | ||| Adjust the size parameter by transforming it with the given function.
112 | public export
113 | scale : (Size -> Size) -> Gen a -> Gen a
114 | scale f (MkGen run) = MkGen $ \si,se => run (f si) se
115 |
116 | ||| Override the size parameter. Returns a generator which uses the given size
117 | ||| instead of the runtime-size parameter.
118 | public export %inline
119 | resize : Size -> Gen a -> Gen a
120 | resize size = scale (const size)
121 |
122 | ||| Scale a size using the golden ratio.
123 | |||
124 | |||   > golden x = x / φ
125 | |||   > golden x = x / 1.61803..
126 | public export
127 | golden : Size -> Size
128 | golden = resize $ \n => round (0.61803398875 * cast n)
129 |
130 | ||| Make a generator smaller by scaling its size parameter.
131 | public export %inline
132 | small : Gen a -> Gen a
133 | small = scale golden
134 |
135 | --------------------------------------------------------------------------------
136 | --          Integral
137 | --------------------------------------------------------------------------------
138 |
139 | ||| Generates a random integral number in the [inclusive,inclusive] range.
140 | |||
141 | ||| This generator does not shrink.
142 | public export
143 | integral_ : ToInteger a => Range a -> Gen a
144 | integral_ range = generate $ \si,se =>
145 |   let (x, y) := bounds si range
146 |    in fromInteger . snd $ randomR (toInteger x, toInteger y) se
147 |
148 | ||| Generates a random integral number in the given @[inclusive,inclusive]@ range.
149 | |||
150 | ||| When the generator tries to shrink, it will shrink towards the
151 | ||| 'Range.origin' of the specified 'Range'.
152 | |||
153 | ||| For example, the following generator will produce a number between @1970@
154 | ||| and @2100@, but will shrink towards @2000@:
155 | |||
156 | ||| ```
157 | ||| integral (Range.'Range.constantFrom' 2000 1970 2100) :: 'Gen' 'Int'
158 | ||| ```
159 | |||
160 | |||   Some sample outputs from this generator might look like:
161 | |||
162 | |||   > === Outcome ===
163 | |||   > 1973
164 | |||   > === Shrinks ===
165 | |||   > 2000
166 | |||   > 1987
167 | |||   > 1980
168 | |||   > 1976
169 | |||   > 1974
170 | |||
171 | |||   > === Outcome ===
172 | |||   > 2061
173 | |||   > === Shrinks ===
174 | |||   > 2000
175 | |||   > 2031
176 | |||   > 2046
177 | |||   > 2054
178 | |||   > 2058
179 | |||   > 2060
180 | |||
181 | public export
182 | integral : ToInteger a => Range a -> Gen a
183 | integral range = shrink (towards $ origin range) (integral_ range)
184 |
185 | ||| Generates a random machine integer in the given range.
186 | |||
187 | ||| This is a specialization of `integral`, offered for convenience.
188 | public export %inline
189 | int : Range Int -> Gen Int
190 | int = integral
191 |
192 | ||| Generates a random 8-bit integer in the given range.
193 | |||
194 | ||| This is a specialization of `integral`, offered for convenience.
195 | public export %inline
196 | int8 : Range Int8 -> Gen Int8
197 | int8 = integral
198 |
199 | ||| Generates a random 8-bit integer in the full available range.
200 | |||
201 | ||| This shrinks exponentially towards 0.
202 | public export %inline
203 | anyInt8 : Gen Int8
204 | anyInt8 = int8 (exponentialFrom 0 minBound maxBound)
205 |
206 | ||| Generates a random 16-bit integer in the given range.
207 | |||
208 | ||| This is a specialization of `integral`, offered for convenience.
209 | public export %inline
210 | int16 : Range Int16 -> Gen Int16
211 | int16 = integral
212 |
213 | ||| Generates a random 16-bit integer in the full available range.
214 | |||
215 | ||| This shrinks exponentially towards 0.
216 | public export %inline
217 | anyInt16 : Gen Int16
218 | anyInt16 = int16 (exponentialFrom 0 minBound maxBound)
219 |
220 | ||| Generates a random 32-bit integer in the given range.
221 | |||
222 | ||| This is a specialization of `integral`, offered for convenience.
223 | public export %inline
224 | int32 : Range Int32 -> Gen Int32
225 | int32 = integral
226 |
227 | ||| Generates a random 32-bit integer in the full available range.
228 | |||
229 | ||| This shrinks exponentially towards 0.
230 | public export %inline
231 | anyInt32 : Gen Int32
232 | anyInt32 = int32 (exponentialFrom 0 minBound maxBound)
233 |
234 | ||| Generates a random 64-bit integer in the given range.
235 | |||
236 | ||| This is a specialization of `integral`, offered for convenience.
237 | public export %inline
238 | int64 : Range Int64 -> Gen Int64
239 | int64 = integral
240 |
241 | ||| Generates a random 64-bit integer in the full available range.
242 | |||
243 | ||| This shrinks exponentially towards 0.
244 | public export %inline
245 | anyInt64 : Gen Int64
246 | anyInt64 = int64 (exponentialFrom 0 minBound maxBound)
247 |
248 | ||| Generates a random 8-bit integer in the given range.
249 | |||
250 | ||| This is a specialization of 'integral', offered for convenience.
251 | public export %inline
252 | bits8 : Range Bits8 -> Gen Bits8
253 | bits8 = integral
254 |
255 | ||| Generates a random 8-bit signed integer in the full available range.
256 | |||
257 | ||| This shrinks exponentially towards 0.
258 | public export %inline
259 | anyBits8 : Gen Bits8
260 | anyBits8 = bits8 (exponential 0 maxBound)
261 |
262 | ||| Generates a random 16-bit integer in the given range.
263 | |||
264 | ||| This is a specialization of 'integral', offered for convenience.
265 | public export %inline
266 | bits16 : Range Bits16 -> Gen Bits16
267 | bits16 = integral
268 |
269 | ||| Generates a random 16-bit signed integer in the full available range.
270 | |||
271 | ||| This shrinks exponentially towards 0.
272 | public export %inline
273 | anyBits16 : Gen Bits16
274 | anyBits16 = bits16 (exponential 0 maxBound)
275 |
276 | ||| Generates a random 32-bit integer in the given range.
277 | |||
278 | ||| This is a specialization of 'integral', offered for convenience.
279 | public export %inline
280 | bits32 : Range Bits32 -> Gen Bits32
281 | bits32 = integral
282 |
283 | ||| Generates a random 32-bit signed integer in the full available range.
284 | |||
285 | ||| This shrinks exponentially towards 0.
286 | public export %inline
287 | anyBits32 : Gen Bits32
288 | anyBits32 = bits32 (exponential 0 maxBound)
289 |
290 | ||| Generates a random 64-bit integer in the given range.
291 | |||
292 | ||| This is a specialization of 'integral', offered for convenience.
293 | public export %inline
294 | bits64 : Range Bits64 -> Gen Bits64
295 | bits64 = integral
296 |
297 | ||| Generates a random 64-bit signed integer in the full available range.
298 | |||
299 | ||| This shrinks exponentially towards 0.
300 | public export %inline
301 | anyBits64 : Gen Bits64
302 | anyBits64 = bits64 (exponential 0 maxBound)
303 |
304 | ||| Generates a random Integer in the given range.
305 | |||
306 | ||| This is a specialization of 'integral', offered for convenience.
307 | public export %inline
308 | integer : Range Integer -> Gen Integer
309 | integer = integral
310 |
311 | ||| Generates a random Nat in the given range.
312 | |||
313 | ||| This is a specialization of 'integral', offered for convenience.
314 | public export %inline
315 | nat : Range Nat -> Gen Nat
316 | nat = integral
317 |
318 | ||| Generates a random Size in the given range.
319 | |||
320 | ||| This is a specialization of 'integral', offered for convenience.
321 | public export %inline
322 | size : Range Size -> Gen Size
323 | size = integral
324 |
325 | ||| Generates a random (Fin n) in the given range.
326 | public export
327 | fin : {n : _} -> Range (Fin n) -> Gen (Fin n)
328 | fin range =
329 |   let rangeInt := map finToInteger range
330 |    in map toFin (integer rangeInt)
331 |
332 |   where
333 |     toFin : Integer -> Fin n
334 |     toFin k = fromMaybe range.origin (integerToFin k n)
335 |
336 | ||| Generates a random (Fin n), where n must be positive.
337 | export
338 | anyFin : (n : Nat) -> (0 prf : IsSucc n) => Gen (Fin n)
339 | anyFin Z impossible
340 | anyFin (S k) = fin (linearFin k)
341 |
342 | --------------------------------------------------------------------------------
343 | --          Floating Point
344 | --------------------------------------------------------------------------------
345 |
346 | ||| Generates a random fractional number in the [inclusive,exclusive) range.
347 | |||
348 | ||| This generator does not shrink.
349 | export
350 | double_ : Range Double -> Gen Double
351 | double_ range =
352 |   generate $ \si,se =>
353 |     let (x, y) := bounds si range
354 |      in snd $ randomR (x, y) se
355 |
356 | ||| Generates a random floating-point number in the given range.
357 | |||
358 | ||| This generator works the same as 'integral', but for floating point numbers.
359 | |||
360 | export %inline
361 | double : Range Double -> Gen Double
362 | double range = shrink (towardsDouble $ origin range) (double_ range)
363 |
364 | --------------------------------------------------------------------------------
365 | --          Choice
366 | --------------------------------------------------------------------------------
367 |
368 | ||| Trivial generator that always produces the same element.
369 | |||
370 | ||| This is another name for `pure`.
371 | public export %inline
372 | constant : a -> Gen a
373 | constant = pure
374 |
375 | ||| Randomly selects one of the elements in the vector.
376 | |||
377 | ||| This generator shrinks towards the first element in the vector.
378 | public export
379 | element : {k : _} -> Vect (S k) a -> Gen a
380 | element vs = map (`index` vs) (fin $ constant FZ last)
381 |
382 | ||| Randomly selects one of the elements in the vector.
383 | |||
384 | ||| This generator does not shrink.
385 | public export %inline
386 | element_ : {k : _} -> Vect (S k) a -> Gen a
387 | element_ = prune . element
388 |
389 | ||| Randomly selects one of the generators in the vector.
390 | |||
391 | ||| This generator shrinks towards the first generator in the vector.
392 | public export %inline
393 | choice : {k : _} -> Vect (S k) (Gen a) -> Gen a
394 | choice vs = element vs >>= id
395 |
396 | ||| Randomly selects one of the generators in the vector.
397 | |||
398 | ||| This generator does not shrink towards a particular
399 | ||| generator in the vector
400 | public export %inline
401 | choice_ : {k : _} -> Vect (S k) (Gen a) -> Gen a
402 | choice_ vs = element_ vs >>= id
403 |
404 | ||| Uses a weighted distribution to randomly select one of the generators in
405 | ||| the vector.
406 | |||
407 | ||| This generator shrinks towards the first generator in the vector.
408 | |||
409 | ||| Note that if the given frequencies sum up to 0, the first element
410 | ||| of the vector
411 | public export
412 | frequency : Vect (S k) (Nat, Gen a) -> Gen a
413 | frequency ps =
414 |   let acc   := scanl1 addFst $ map (mapFst toInteger) ps
415 |       gen   := integral_ . constant 0 . fst $ last acc
416 |       lower := \n => takeWhile (< n) (fromFoldable $ map fst acc)
417 |
418 |   in shrink lower gen >>= choose acc
419 |
420 |   where
421 |     addFst : (Integer,x) -> (Integer,x) -> (Integer,x)
422 |     addFst (x,_) (y,v) = (x + y,v)
423 |
424 |     choose : Vect n (Integer, Gen a) -> Integer -> Gen a
425 |     choose []             _ = snd $ head ps
426 |     choose ((i, v) :: ps) k = if i >= k then v else choose ps k
427 |
428 | ||| Generates a random boolean.
429 | |||
430 | ||| This generator shrinks to `False`.
431 | public export %inline
432 | bool : Gen Bool
433 | bool = element [False,True]
434 |
435 | ||| Generates constant values of type `Unit"
436 | export %inline
437 | unit : Gen ()
438 | unit = pure ()
439 |
440 | --------------------------------------------------------------------------------
441 | --          Character
442 | --------------------------------------------------------------------------------
443 |
444 | ||| Generates a character in the given `Range`.
445 | |||
446 | ||| Shrinks towards the origin of the range.
447 | export
448 | char : Range Char -> Gen Char
449 | char = map chr . int . map ord
450 |
451 | ||| Generates a character in the interval [lower,uppper].
452 | |||
453 | ||| Shrinks towards the lower value.
454 | export %inline
455 | charc : (lower : Char) -> (upper : Char) -> Gen Char
456 | charc l u = char $ constant l u
457 |
458 |
459 | ||| Generates an ASCII binit: `'0'..'1'`
460 | export %inline
461 | binit : Gen Char
462 | binit = charc '0' '1'
463 |
464 | ||| Generates an ASCII octit: `'0'..'7'`
465 | export %inline
466 | octit : Gen Char
467 | octit = charc '0' '7'
468 |
469 | ||| Generates an ASCII digit: `'0'..'9'`
470 | export %inline
471 | digit : Gen Char
472 | digit = charc '0' '9'
473 |
474 | ||| Generates an ASCII hexit: `'0'..'9', 'a'..'f', 'A'..'F'`
475 | export
476 | hexit : Gen Char
477 | hexit = frequency [(10, digit),(6, charc 'a' 'f'),(6, charc 'A' 'F')]
478 |
479 | ||| Generates an ASCII lowercase letter: `'a'..'z'`
480 | export %inline
481 | lower : Gen Char
482 | lower = charc 'a' 'z'
483 |
484 | ||| Generates an ASCII uppercase letter: `'A'..'Z'`
485 | export %inline
486 | upper : Gen Char
487 | upper = charc 'A' 'Z'
488 |
489 | ||| Generates an ASCII letter: `'a'..'z', 'A'..'Z'`
490 | export %inline
491 | alpha : Gen Char
492 | alpha = choice [lower,upper]
493 |
494 | ||| Generates an ASCII letter or digit: `'a'..'z', 'A'..'Z', '0'..'9'`
495 | export
496 | alphaNum : Gen Char
497 | alphaNum = frequency [(10,digit),(24,lower),(24,upper)]
498 |
499 | ||| Generates an ASCII character: `'\0'..'\127'`
500 | export %inline
501 | ascii : Gen Char
502 | ascii = charc '\0' '\127'
503 |
504 | ||| Generates an printable ASCII character: `'\32'..'\126'`
505 | ||| Note: This includes the space character but no
506 | |||       line breaks or tabs
507 | export %inline
508 | printableAscii : Gen Char
509 | printableAscii = charc '\32' '\126'
510 |
511 | ||| Generates a latin character: `'\0'..'\255'`
512 | export %inline
513 | latin : Gen Char
514 | latin = charc '\0' '\255'
515 |
516 | ||| Generates a printable latin character: `'\32'..'\126'` and `'\160'..'\255'`
517 | export
518 | printableLatin : Gen Char
519 | printableLatin = frequency [ (95, printableAscii), (96, charc '\160' '\255') ]
520 |
521 | ||| Generates a Unicode character, excluding noncharacters
522 | ||| and invalid standalone surrogates:
523 | ||| `'\0'..'\1114111'` (excluding '\55296'..'\57343', '\65534', '\65535')`
524 | export
525 | unicode : Gen Char
526 | unicode =
527 |   frequency
528 |     [ (55296, charc '\0' '\55295')
529 |     , (8190, charc '\57344' '\65533')
530 |     , (1048576, charc '\65536' '\1114111')
531 |     ]
532 |
533 | ||| Generates a printable Unicode character, excluding noncharacters
534 | ||| and invalid standalone surrogates:
535 | ||| `'\0'..'\1114111'` (excluding '\0' .. '\31', '\127' .. '\159',
536 | ||| '\55296'..'\57343', and '\65534', '\65535')`
537 | export
538 | printableUnicode : Gen Char
539 | printableUnicode =
540 |   frequency
541 |     [ (95, printableAscii)
542 |     , (55136, charc '\160' '\55295')
543 |     , (8190, charc '\57344' '\65533')
544 |     , (1048576, charc '\65536' '\1114111')
545 |     ]
546 |
547 | ||| Generates a Unicode character, including noncharacters
548 | ||| but excluding invalid standalone surrogates: `0000..10FFFF`
549 | ||| (excluding D800..DFFFF)
550 | export %inline
551 | unicodeAll : Gen Char
552 | unicodeAll =
553 |   frequency
554 |     [ (55296, charc '\0' '\55295')
555 |     , (1056768, charc '\57344' '\1114111')
556 |     ]
557 |
558 | --------------------------------------------------------------------------------
559 | --          Containers
560 | --------------------------------------------------------------------------------
561 |
562 | ||| Generates a 'Nothing' some of the time.
563 | export
564 | maybe : Gen a -> Gen (Maybe a)
565 | maybe gen =
566 |   sized $ \s =>
567 |     frequency
568 |       [ (2, constant Nothing)
569 |       , (S s.size, Just <$> gen)
570 |       ]
571 |
572 | ||| Generates either an 'a' or a 'b'.
573 | |||
574 | ||| As the size grows, this generator generates @Right@s more often than @Left@s.
575 | export
576 | either : Gen a -> Gen b -> Gen (Either a b)
577 | either genA genB =
578 |   sized $ \s =>
579 |     frequency
580 |       [ (2, Left <$> genA)
581 |       , (S s.size, Right <$> genB)
582 |       ]
583 |
584 | ||| Generates either an 'a' or a 'b', without bias.
585 | |||
586 | ||| This generator generates as many @Right@s as it does @Left@s.
587 | export %inline
588 | either_ : Gen a -> Gen b -> Gen (Either a b)
589 | either_ genA genB = choice [Left <$> genA, Right <$> genB]
590 |
591 | ||| Generates a list using a 'Range' to determine the length.
592 | export
593 | vect : (n : Nat) -> Gen a -> Gen (Vect n a)
594 | vect 0     _ = pure []
595 | vect (S k) g = [| g :: vect k g |]
596 |
597 | ||| Generates a list using a 'Range' to determine the length.
598 | export
599 | list : Range Nat -> Gen a -> Gen (List a)
600 | list range gen =
601 |   sized $ \si =>
602 |     let minLength := lowerBound si range
603 |      in mapGen (interleave minLength . value) $
604 |         integral_ range >>= (\n => map toList (vect n (toTree gen)))
605 |
606 | ||| Generates a snoc list using a 'Range' to determine the length.
607 | export
608 | snocList : Range Nat -> Gen a -> Gen (SnocList a)
609 | snocList r g = ([<]<><) <$> list r g
610 |
611 |
612 | ||| Generates a non-empty list using a `Range` to determine the length.
613 | export
614 | list1 : Range Nat -> Gen a -> Gen (List1 a)
615 | list1 range gen = [| gen ::: list (map pred range) gen |]
616 |
617 | ||| Generates a string using 'Range' to determine the length.
618 | export
619 | string : Range Nat -> Gen Char -> Gen String
620 | string range = map fastPack . list range
621 |
622 | ||| Generates an heterogeneous list being provided a generator for each element
623 | export
624 | hlist : All Gen ts -> Gen (HList ts)
625 | hlist []      = [| [] |]
626 | hlist (x::xs) = [| x :: hlist xs |]
627 |
628 | ||| Generates an heterogeneous vect being provided a generator for each element
629 | export
630 | hvect : All Gen ts -> Gen (HVect ts)
631 | hvect []      = [| [] |]
632 | hvect (x::xs) = [| x :: hvect xs |]
633 |
634 | --------------------------------------------------------------------------------
635 | --          SOP
636 | --------------------------------------------------------------------------------
637 |
638 | collapseNPV : NP (K a) ks -> {auto 0 _ : NonEmpty ks} -> (k ** Vect (S k) a)
639 | collapseNPV {ks = _} [] impossible
640 | collapseNPV {ks = _} (v::[]) = (0 ** [v])
641 | collapseNPV {ks = t::t2::ts}(v::v2::vs) =
642 |   let (k ** vs2) := collapseNPV {ks = t2 :: ts} (v2 :: vs)
643 |    in (S k ** (v :: vs2))
644 |
645 | ||| Turns an n-ary product of generators into a generator
646 | ||| of n-ary products of the same type.
647 | export
648 | np : NP Gen ts -> Gen (NP I ts)
649 | np = sequenceNP
650 |
651 | ||| Turns an n-ary product of generators into a generator
652 | ||| of n-ary sums of the same type. This is a generalisation
653 | ||| of choice and shrinks towards the first sum type
654 | ||| in the list.
655 | export
656 | ns : NP Gen ts -> {auto 0 prf : NonEmpty ts} -> Gen (NS I ts)
657 | ns np =
658 |   let (_ ** vs) := collapseNPV (apInjsNP_ np)
659 |    in choice (map sequenceNS vs)
660 |
661 | ||| Turns an n-ary product of generators into a generator
662 | ||| of n-ary sums of the same type. This is a generalisation
663 | ||| of `choice_` and does not shrink towards a particular
664 | ||| sum type.
665 | export
666 | ns_ : NP Gen ts -> {auto 0 prf : NonEmpty ts} -> Gen (NS I ts)
667 | ns_ np =
668 |   let (_ ** vs) := collapseNPV (apInjsNP_ np)
669 |    in choice_ (map sequenceNS vs)
670 |
671 | export
672 | sop : POP Gen tss -> {auto 0 prf : NonEmpty tss} -> Gen (SOP I tss)
673 | sop p =
674 |   let (_ ** vs) := collapseNPV {a = SOP Gen tss} (apInjsPOP_ p)
675 |    in choice (map sequenceSOP vs)
676 |
677 | --------------------------------------------------------------------------------
678 | --          Sampling
679 | --------------------------------------------------------------------------------
680 |
681 | ||| Print the value produced by a generator, and the first level of shrinks,
682 | ||| for the given size and seed.
683 | |||
684 | ||| Use 'print' to generate a value from a random seed.
685 | export
686 | printWith : (HasIO io, Show a) => Size -> StdGen -> Gen a -> io ()
687 | printWith si se gen =
688 |   let (MkCotree v fo) := runGen si se gen
689 |       shrinks         := value <$> take 1000 fo
690 |    in do
691 |      putStrLn "=== Outcome ==="
692 |      putStrLn (show v)
693 |      putStrLn "=== Shrinks ==="
694 |      traverse_ printLn shrinks
695 |
696 | ||| Run a generator with a random seed and print the outcome, and the first
697 | ||| level of shrinks.
698 | |||
699 | ||| @
700 | ||| Gen.print (Gen.'enum' \'a\' \'f\')
701 | ||| @
702 | |||
703 | |||   > === Outcome ===
704 | |||   > 'd'
705 | |||   > === Shrinks ===
706 | |||   > 'a'
707 | |||   > 'b'
708 | |||   > 'c'
709 | export
710 | print : Show a => HasIO io => Gen a -> io ()
711 | print gen = initSeed >>= \se => printWith 100 se gen
712 |
713 | ||| Generate a sample from a generator.
714 | export
715 | sample : HasIO io => Gen a -> io a
716 | sample gen = (value . gen.unGen 100) <$> initSeed
717 |
718 | ||| Render the shrink tree produced by a generator, for the given size and
719 | ||| seed up to the given depth and width.
720 | export
721 | renderTree :
722 |      {auto _ : Show a}
723 |   -> (maxDepth : Nat)
724 |   -> (maxWidth : Nat)
725 |   -> Size
726 |   -> StdGen
727 |   -> Gen a
728 |   -> String
729 | renderTree md mw si se = drawTree
730 |                        . map show
731 |                        . toTree md mw
732 |                        . runGen si se
733 |
734 | ||| Print the shrink tree produced by a generator, for the given size and
735 | ||| seed.
736 | |||
737 | ||| Use 'printTree' to generate a value from a random seed.
738 | export
739 | printTreeWith :
740 |      {auto _ : Show a}
741 |   -> {auto _ : HasIO io}
742 |   -> (maxDepth : Nat)
743 |   -> (maxWidth : Nat)
744 |   -> Size
745 |   -> StdGen
746 |   -> Gen a
747 |   -> io ()
748 | printTreeWith md mw si se = putStrLn . renderTree md mw si se
749 |
750 | ||| Run a generator with a random seed and print the resulting shrink tree.
751 | |||
752 | ||| @
753 | ||| Gen.printTree (Gen.'enum' \'a\' \'f\')
754 | ||| @
755 | |||
756 | |||   > 'd'
757 | |||   >  ├╼'a'
758 | |||   >  ├╼'b'
759 | |||   >  │  └╼'a'
760 | |||   >  └╼'c'
761 | |||   >     ├╼'a'
762 | |||   >     └╼'b'
763 | |||   >        └╼'a'
764 | |||
765 | |||   /This may not terminate when the tree is very large./
766 | |||
767 | export
768 | printTree :
769 |      {auto _ : Show a}
770 |   -> {auto _ : HasIO io}
771 |   -> (maxDepth : Nat)
772 |   -> (maxWidth : Nat)
773 |   -> Gen a
774 |   -> io ()
775 | printTree md mw gen = initSeed >>= \se => printTreeWith md mw 100 se gen
776 |