Documentation

Plausible.Gen

Gen Monad #

This monad is used to formulate randomized computations with a parameter to specify the desired size of the result.

Main definitions #

References #

Error thrown on generation failure, e.g. because you've run out of resources.

Instances For
    Equations
      Instances For
        Equations
          Instances For
            @[reducible, inline]
            abbrev Plausible.Gen (α : Type u) :

            Monad to generate random examples to test properties with. It has a Nat parameter so that the caller can decide on the size of the examples. It allows failure to generate via the Except monad

            Equations
              Instances For
                Equations
                  Instances For
                    @[inline]
                    def Plausible.Gen.up {α : Type u} (x : Gen α) :
                    Gen (ULift α)
                    Equations
                      Instances For
                        @[inline]
                        def Plausible.Gen.down {α : Type u_1} (x : Gen (ULift α)) :
                        Gen α
                        Equations
                          Instances For
                            def Plausible.Gen.chooseAny (α : Type u) [Random Id α] :
                            Gen α

                            Lift Random.random to the Gen monad.

                            Equations
                              Instances For
                                def Plausible.Gen.choose (α : Type u) [LE α] [BoundedRandom Id α] (lo hi : α) (h : lo hi) :
                                Gen { a : α // lo a a hi }

                                Lift BoundedRandom.randomR to the Gen monad.

                                Equations
                                  Instances For
                                    def Plausible.Gen.chooseNatLt (lo hi : Nat) (h : lo < hi) :
                                    Gen { a : Nat // lo a a < hi }

                                    Generate a Nat example between lo and hi (exclusively).

                                    Equations
                                      Instances For

                                        Get access to the size parameter of the Gen monad.

                                        Equations
                                          Instances For
                                            def Plausible.Gen.resize {α : Type u_1} (f : NatNat) (x : Gen α) :
                                            Gen α

                                            Apply a function to the size parameter.

                                            Equations
                                              Instances For

                                                Choose a Nat between 0 and getSize.

                                                Equations
                                                  Instances For

                                                    The following section defines various combinators for generators, which are used in the body of derived generators (for derived Arbitrary instances).

                                                    The code for these combinators closely mirrors those used in Rocq/Coq QuickChick (see link in the References section below).

                                                    References #

                                                    Raised when a fueled generator fails due to insufficient fuel.

                                                    Equations
                                                      Instances For
                                                        def Plausible.Gen.pick {α : Type u_1} (default : Gen α) (xs : List (Nat × Gen α)) (n : Nat) :

                                                        pick default xs n chooses a weight & a generator (k, gen) from the list xs such that n < k. If xs is empty, the default generator with weight 0 is returned.

                                                        Equations
                                                          Instances For
                                                            instance Plausible.Gen.inhabitedGen {α : Type u_1} :
                                                            Equations
                                                              def Plausible.Gen.backtrack {α : Type u_1} (gs : List (Nat × Gen α)) :
                                                              Gen α

                                                              Tries all generators until one returns a Some value or all the generators failed once with None. The generators are picked at random according to their weights (like frequency in Haskell QuickCheck), and each generator is run at most once.

                                                              Equations
                                                                Instances For
                                                                  def Plausible.Gen.oneOfWithDefault {α : Type} (default : Gen α) (gs : List (Gen α)) :
                                                                  Gen α

                                                                  Picks one of the generators in gs at random, returning the default generator if gs is empty.

                                                                  (This is a more ergonomic version of Plausible's Gen.oneOf which doesn't require the caller to supply a proof that the list index is in bounds.)

                                                                  Equations
                                                                    Instances For
                                                                      def Plausible.Gen.frequency {α : Type} (default : Gen α) (gs : List (Nat × Gen α)) :
                                                                      Gen α

                                                                      frequency picks a generator from the list gs according to the weights in gs. If gs is empty, the default generator is returned.

                                                                      Equations
                                                                        Instances For
                                                                          def Plausible.Gen.sized {α : Type} (f : NatGen α) :
                                                                          Gen α

                                                                          sized f constructs a generator that depends on its size parameter

                                                                          Equations
                                                                            Instances For
                                                                              def Plausible.Gen.arrayOf {α : Type u} (x : Gen α) :
                                                                              Gen (Array α)

                                                                              Create an Array of examples using x. The size is controlled by the size parameter of Gen.

                                                                              Equations
                                                                                Instances For
                                                                                  def Plausible.Gen.listOf {α : Type u} (x : Gen α) :
                                                                                  Gen (List α)

                                                                                  Create a List of examples using x. The size is controlled by the size parameter of Gen.

                                                                                  Equations
                                                                                    Instances For
                                                                                      def Plausible.Gen.oneOf {α : Type u} (xs : Array (Gen α)) (pos : 0 < xs.size := by decide) :
                                                                                      Gen α

                                                                                      Given a list of example generators, choose one to create an example.

                                                                                      Equations
                                                                                        Instances For
                                                                                          def Plausible.Gen.elements {α : Type u} (xs : List α) (pos : 0 < xs.length) :
                                                                                          Gen α

                                                                                          Given a list of examples, choose one to create an example.

                                                                                          Equations
                                                                                            Instances For
                                                                                              def Plausible.Gen.permutationOf {α : Type u} (xs : List α) :
                                                                                              Gen { ys : List α // xs.Perm ys }

                                                                                              Generate a random permutation of a given list.

                                                                                              Equations
                                                                                                Instances For
                                                                                                  def Plausible.Gen.prodOf {α : Type u} {β : Type v} (x : Gen α) (y : Gen β) :
                                                                                                  Gen (α × β)

                                                                                                  Given two generators produces a tuple consisting out of the result of both

                                                                                                  Equations
                                                                                                    Instances For
                                                                                                      def Plausible.Gen.run {α : Type} (x : Gen α) (size : Nat) :
                                                                                                      IO α

                                                                                                      Execute a Gen inside the IO monad using size as the example size

                                                                                                      Equations
                                                                                                        Instances For

                                                                                                          Print (at most) 10 samples of a given type to stdout for debugging. Sadly specialized to Type 0

                                                                                                          Equations
                                                                                                            Instances For
                                                                                                              def Plausible.Gen.runUntil {α : Type} (attempts : Option Nat := none) (x : Gen α) (size : Nat) :
                                                                                                              IO α

                                                                                                              Execute a Gen until it actually produces an output. May diverge for bad generators!

                                                                                                              Equations
                                                                                                                Instances For