Documentation

Mathlib.Testing.SlimCheck.Functions

slim_check: generators for functions #

This file defines Sampleable instances for α → β functions and ℤ → ℤ injective functions.

Functions are generated by creating a list of pairs and one more value using the list as a lookup table and resorting to the additional value when a value is not found in the table.

Injective functions are generated by creating a list of numbers and a permutation of that list. The permutation insures that every input is mapped to a unique output. When an input is not found in the list the input itself is used as an output.

Injective functions f : α → α could be generated easily instead of ℤ → ℤ by generating a List α, removing duplicates and creating a permutation. One has to be careful when generating the domain to make it vast enough that, when generating arguments to apply f to, they argument should be likely to lie in the domain of f. This is the reason that injective functions f : ℤ → ℤ are generated by fixing the domain to the range [-2*size .. -2*size], with size the size parameter of the gen monad.

Much of the machinery provided in this file is applicable to generate injective functions of type α → α and new instances should be easy to define.

Other classes of functions such as monotone functions can generated using similar techniques. For monotone functions, generating two lists, sorting them and matching them should suffice, with appropriate default values. Some care must be taken for shrinking such functions to make sure their defining property is invariant through shrinking. Injective functions are an example of how complicated it can get.

inductive SlimCheck.TotalFunction (α : Type u) (β : Type v) :
Type (max u v)

Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function.

with_default f y encodes x ↦ f x when x ∈ f and x ↦ y otherwise.

We use Σ to encode mappings instead of × because we rely on the association list API defined in data.list.sigma.

Instances For
    def SlimCheck.TotalFunction.comp {α : Type u} {β : Type v} {γ : Type w} (f : βγ) :

    Compose a total function with a regular function on the left

    Instances For
      def SlimCheck.TotalFunction.apply {α : Type u} {β : Type v} [DecidableEq α] :
      SlimCheck.TotalFunction α βαβ

      Apply a total function to an argument.

      Instances For
        def SlimCheck.TotalFunction.reprAux {α : Type u} {β : Type v} [Repr α] [Repr β] (m : List ((_ : α) × β)) :

        Implementation of has_repr (total_function α β).

        Creates a string for a given finmap and output, x₀ ↦ y₀, .. xₙ ↦ yₙ for each of the entries. The brackets are provided by the calling function.

        Instances For
          def SlimCheck.TotalFunction.repr {α : Type u} {β : Type v} [Repr α] [Repr β] :

          Produce a string for a given TotalFunction. The output is of the form [x₀ ↦ f x₀, .. xₙ ↦ f xₙ, _ ↦ y].

          Instances For
            def SlimCheck.TotalFunction.List.toFinmap' {α : Type u} {β : Type v} (xs : List (α × β)) :
            List ((_ : α) × β)

            Create a finmap from a list of pairs.

            Instances For

              Shrink a total function by shrinking the lists that represent it.

              Instances For

                Map a total_function to one whose default value is zero so that it represents a finsupp.

                Instances For

                  The support of a zero default total_function.

                  Instances For

                    Create a finitely supported function from a total function by taking the default value to zero.

                    Instances For
                      instance SlimCheck.TotalFunction.PiUncurry.sampleableExt {α : Type u} {β : Type v} {γ : Sort w} [SlimCheck.SampleableExt (α × βγ)] :
                      SlimCheck.SampleableExt (αβγ)
                      inductive SlimCheck.InjectiveFunction (α : Type u) :

                      Data structure specifying a total function using a list of pairs and a default value returned when the input is not in the domain of the partial function.

                      map_to_self f encodes x ↦ f x when x ∈ f and x ↦ x, i.e. x to itself, otherwise.

                      We use Σ to encode mappings instead of × because we rely on the association list API defined in data.list.sigma.

                      Instances For

                        Apply a total function to an argument.

                        Instances For

                          Produce a string for a given total_function. The output is of the form [x₀ ↦ f x₀, .. xₙ ↦ f xₙ, x ↦ x]. Unlike for total_function, the default value is not a constant but the identity function.

                          Instances For
                            def SlimCheck.InjectiveFunction.List.applyId {α : Type u} [DecidableEq α] (xs : List (α × α)) (x : α) :
                            α

                            Interpret a list of pairs as a total function, defaulting to the identity function when no entries are found for a given function

                            Instances For
                              @[simp]
                              theorem SlimCheck.InjectiveFunction.List.applyId_cons {α : Type u} [DecidableEq α] (xs : List (α × α)) (x : α) (y : α) (z : α) :
                              theorem SlimCheck.InjectiveFunction.List.applyId_zip_eq {α : Type u} [DecidableEq α] {xs : List α} {ys : List α} (h₀ : List.Nodup xs) (h₁ : List.length xs = List.length ys) (x : α) (y : α) (i : ) (h₂ : List.get? xs i = some x) :
                              theorem SlimCheck.InjectiveFunction.applyId_mem_iff {α : Type u} [DecidableEq α] {xs : List α} {ys : List α} (h₀ : List.Nodup xs) (h₁ : xs ~ ys) (x : α) :
                              def SlimCheck.InjectiveFunction.Perm.slice {α : Type u} [DecidableEq α] (n : ) (m : ) :
                              (xs : List α) ×' (ys : List α) ×' xs ~ ys List.Nodup ys(xs : List α) ×' (ys : List α) ×' xs ~ ys List.Nodup ys

                              Remove a slice of length m at index n in a list and a permutation, maintaining the property that it is a permutation.

                              Instances For

                                A lazy list, in decreasing order, of sizes that should be sliced off a list of length n

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  def SlimCheck.InjectiveFunction.shrinkPerm {α : Type} [DecidableEq α] :
                                  (xs : List α) ×' (ys : List α) ×' xs ~ ys List.Nodup ysList ((xs : List α) ×' (ys : List α) ×' xs ~ ys List.Nodup ys)

                                  Shrink a permutation of a list, slicing a segment in the middle.

                                  The sizes of the slice being removed start at n (with n the length of the list) and then n / 2, then n / 4, etc down to 1. The slices will be taken at index 0, n / k, 2n / k, 3n / k, etc.

                                  Instances For

                                    Shrink an injective function slicing a segment in the middle of the domain and removing the corresponding elements in the codomain, hence maintaining the property that one is a permutation of the other.

                                    Instances For
                                      def SlimCheck.InjectiveFunction.mk {α : Type u} (xs : List α) (ys : List α) (h : xs ~ ys) (h' : List.Nodup ys) :

                                      Create an injective function from one list and a permutation of that list.

                                      Instances For
                                        instance SlimCheck.Injective.testable {α : Type u} {β : Type v} (f : αβ) [I : SlimCheck.Testable (SlimCheck.NamedBinder "x" (∀ (x : α), SlimCheck.NamedBinder "y" (∀ (y : α), SlimCheck.NamedBinder "H" (f x = f yx = y))))] :
                                        instance SlimCheck.Monotone.testable {α : Type u} {β : Type v} [Preorder α] [Preorder β] (f : αβ) [I : SlimCheck.Testable (SlimCheck.NamedBinder "x" (∀ (x : α), SlimCheck.NamedBinder "y" (∀ (y : α), SlimCheck.NamedBinder "H" (x yf x f y))))] :
                                        instance SlimCheck.Antitone.testable {α : Type u} {β : Type v} [Preorder α] [Preorder β] (f : αβ) [I : SlimCheck.Testable (SlimCheck.NamedBinder "x" (∀ (x : α), SlimCheck.NamedBinder "y" (∀ (y : α), SlimCheck.NamedBinder "H" (x yf y f x))))] :