Documentation

Init.WF

inductive Acc {α : Sort u} (r : α → α → Prop) :
α → Prop

Acc is the accessibility predicate. Given some relation r (e.g. <) and a value x, Acc r x means that x is accessible through r:

x is accessible if there exists no infinite sequence ... < y₂ < y₁ < y₀ < x.

  • intro {α : Sort u} {r : α → α → Prop} (x : α) (h : ∀ (y : α), r y x → Acc r y) : Acc r x

    A value is accessible if for all y such that r y x, y is also accessible. Note that if there exists no y such that r y x, then x is accessible. Such an x is called a base case.

Instances For
    @[reducible, inline]
    noncomputable abbrev Acc.ndrec {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} (m : (x : α) → (∀ (y : α), r y x → Acc r y) → ((y : α) → r y x → C y) → C x) {a : α} (n : Acc r a) :
    C a
    Equations
    Instances For
      @[reducible, inline]
      noncomputable abbrev Acc.ndrecOn {α : Sort u2} {r : α → α → Prop} {C : α → Sort u1} {a : α} (n : Acc r a) (m : (x : α) → (∀ (y : α), r y x → Acc r y) → ((y : α) → r y x → C y) → C x) :
      C a
      Equations
      Instances For
        theorem Acc.inv {α : Sort u} {r : α → α → Prop} {x y : α} (h₁ : Acc r x) (h₂ : r y x) :
        Acc r y
        inductive WellFounded {α : Sort u} (r : α → α → Prop) :

        A relation r is WellFounded if all elements of α are accessible within r. If a relation is WellFounded, it does not allow for an infinite descent along the relation.

        If the arguments of the recursive calls in a function definition decrease according to a well founded relation, then the function terminates. Well-founded relations are sometimes called Artinian or said to satisfy the “descending chain condition”.

        • intro {α : Sort u} {r : α → α → Prop} (h : ∀ (a : α), Acc r a) : WellFounded r

          If all elements are accessible via r, then r is well-founded.

        Instances For
          class WellFoundedRelation (α : Sort u) :
          Sort (max 1 u)

          A type that has a standard well-founded relation.

          Instances are used to prove that functions terminate using well-founded recursion by showing that recursive calls reduce some measure according to a well-founded relation. This relation can combine well-founded relations on the recursive function's parameters.

          • rel : α → α → Prop

            A well-founded relation on α.

          • A proof that rel is, in fact, well-founded.

          Instances
            theorem WellFounded.apply {α : Sort u} {r : α → α → Prop} (wf : WellFounded r) (a : α) :
            Acc r a
            inductive WellFounded.FixGraph {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) :
            C x → Prop

            Graph of a well-founded fixpoint: FixGraph F x c holds when c : C x is the value obtained by unfolding F along the accessibility tree. This recursive Prop inductive is used to define WellFounded.fix without large elimination of Acc.

            • mk {α : Sort u} {r : α → α → Prop} {C : α → Sort v} {F : (x : α) → ((y : α) → r y x → C y) → C x} (x : α) (g : (y : α) → r y x → C y) (ih : ∀ (y : α) (h : r y x), FixGraph F y (g y h)) : FixGraph F x (F x g)
            Instances For
              theorem WellFounded.FixGraph.nonempty {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) {x : α} (acx : Acc r x) :
              Nonempty { c : C x // FixGraph F x c }
              theorem WellFounded.FixGraph.unique {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) {x : α} (acx : Acc r x) (c₁ c₂ : C x) :
              FixGraph F x c₁ → FixGraph F x c₂ → c₁ = c₂
              noncomputable def WellFounded.fixF {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) (a : Acc r x) :
              C x
              Equations
              Instances For
                theorem WellFounded.fixGraph_fixF {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) (acx : Acc r x) :
                FixGraph F x (fixF F x acx)
                noncomputable def WellFounded.recursion {α : Sort u} {r : α → α → Prop} (hwf : WellFounded r) {C : α → Sort v} (a : α) (h : (x : α) → ((y : α) → r y x → C y) → C x) :
                C a
                Equations
                Instances For
                  theorem WellFounded.induction {α : Sort u} {r : α → α → Prop} (hwf : WellFounded r) {C : α → Prop} (a : α) (h : ∀ (x : α), (∀ (y : α), r y x → C y) → C x) :
                  C a
                  theorem WellFounded.fixF_eq {α : Sort u} {r : α → α → Prop} {C : α → Sort v} (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) (acx : Acc r x) :
                  fixF F x acx = F x fun (y : α) (p : r y x) => fixF F y ⋯
                  def WellFounded.wrap {α : Sort u} {r : α → α → Prop} (h : WellFounded r) (x : α) :
                  { x : α // Acc r x }

                  Attaches to x the proof that x is accessible in the given well-founded relation. This can be used in recursive function definitions to explicitly use a different relation than the one inferred by default:

                  def otherWF : WellFounded Nat := …
                  def foo (n : Nat) := …
                  termination_by otherWF.wrap n
                  
                  Equations
                  Instances For
                    @[simp]
                    theorem WellFounded.val_wrap {α : Sort u} {r : α → α → Prop} (h : WellFounded r) (x : α) :
                    (h.wrap x).val = x
                    noncomputable def WellFounded.fix {α : Sort u} {C : α → Sort v} {r : α → α → Prop} (hwf : WellFounded r) (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) :
                    C x

                    A well-founded fixpoint. If satisfying the motive C for all values that are smaller according to a well-founded relation allows it to be satisfied for the current value, then it is satisfied for all values.

                    This function is used as part of the elaboration of well-founded recursion.

                    Equations
                    Instances For
                      theorem WellFounded.fix_eq {α : Sort u} {C : α → Sort v} {r : α → α → Prop} (hwf : WellFounded r) (F : (x : α) → ((y : α) → r y x → C y) → C x) (x : α) :
                      hwf.fix F x = F x fun (y : α) (x : r y x) => hwf.fix F y
                      @[irreducible]
                      noncomputable def Acc.rec' {α : Sort u'} {r : α → α → Prop} {motive : (a : α) → Acc r a → Sort v'} (intro : (x : α) → (h : ∀ (y : α), r y x → Acc r y) → ((y : α) → (hy : r y x) → motive y ⋯) → motive x ⋯) {a : α} (t : Acc r a) :
                      motive a t

                      A replacement for Acc.rec at motives that are not propositions.

                      Acc.rec' has the same signature as Acc.rec — any motive in Sort v, including one that mentions the accessibility proof — but obtains its result from Classical.choice rather than by recursion, and is @[irreducible]. Compiled code is unaffected: the @[csimp] lemma Acc.rec'_eq_recC rewrites it to a directly recursive implementation.

                      It is intended for use ahead of a planned change that restricts Acc.rec to Prop motives, so that the kernel never has to reduce a proof. Code that eliminates an accessibility proof into data can move to Acc.rec' today and keep working across that change.

                      What such a move costs is the iota rule: Acc.rec' intro (Acc.intro x h) is only propositionally equal to intro x h …, via Acc.rec'_intro. To port code that uses Acc.rec at a non-Prop motive:

                      Equations
                      • Acc.rec' intro t = WellFounded.fixF (fun (x : α) (ih : (y : α) → r y x → (h : Acc r y) → motive y h) (h : Acc r x) => intro x ⋯ fun (y : α) (hy : r y x) => ih y hy ⋯) a t t
                      Instances For
                        theorem Acc.rec'_intro {α : Sort u} {r : α → α → Prop} {motive : (a : α) → Acc r a → Sort v} (intro : (x : α) → (h : ∀ (y : α), r y x → Acc r y) → ((y : α) → (hy : r y x) → motive y ⋯) → motive x ⋯) (x : α) (h : ∀ (y : α), r y x → Acc r y) :
                        rec' intro ⋯ = intro x h fun (y : α) (hy : r y x) => rec' intro ⋯

                        The propositional replacement for the iota rule of Acc.rec.

                        theorem Acc.rec'_eq {α : Sort u} {r : α → α → Prop} {motive : (a : α) → Acc r a → Sort v} (intro : (x : α) → (h : ∀ (y : α), r y x → Acc r y) → ((y : α) → (hy : r y x) → motive y ⋯) → motive x ⋯) {a : α} (t : Acc r a) :
                        rec' intro t = intro a ⋯ fun (x : α) (hy : r x a) => rec' intro ⋯

                        Unfolds Acc.rec' at an arbitrary accessibility proof, which is what proofs that used to close an unfolding equation by rfl need. Unlike Acc.rec'_intro this matches any Acc.rec' intro t, since t need not be in Acc.intro _ _ form.

                        @[reducible]
                        noncomputable def Acc.recOn' {α : Sort u'} {r : α → α → Prop} {motive : (a : α) → Acc r a → Sort v'} {a : α} (t : Acc r a) (intro : (x : α) → (h : ∀ (y : α), r y x → Acc r y) → ((y : α) → (hy : r y x) → motive y ⋯) → motive x ⋯) :
                        motive a t

                        Acc.recOn at motives that are not propositions. See Acc.rec'.

                        Equations
                        Instances For
                          @[reducible]
                          noncomputable def Acc.ndrec' {α : Sort u'} {r : α → α → Prop} {C : α → Sort v'} (m : (x : α) → (∀ (y : α), r y x → Acc r y) → ((y : α) → r y x → C y) → C x) {a : α} (n : Acc r a) :
                          C a

                          Acc.ndrec at motives that are not propositions. See Acc.rec'.

                          Equations
                          Instances For
                            @[reducible]
                            noncomputable def Acc.ndrecOn' {α : Sort u'} {r : α → α → Prop} {C : α → Sort v'} {a : α} (n : Acc r a) (m : (x : α) → (∀ (y : α), r y x → Acc r y) → ((y : α) → r y x → C y) → C x) :
                            C a

                            Acc.ndrecOn at motives that are not propositions. See Acc.rec'.

                            Equations
                            Instances For
                              @[instance_reducible]
                              Equations
                              Instances For
                                theorem Subrelation.accessible {α : Sort u} {r q : α → α → Prop} {a : α} (h₁ : Subrelation q r) (ac : Acc r a) :
                                Acc q a
                                theorem Subrelation.wf {α : Sort u} {r q : α → α → Prop} (h₁ : Subrelation q r) (h₂ : WellFounded r) :
                                theorem InvImage.accAux {α : Sort u} {β : Sort v} {r : β → β → Prop} (f : α → β) {b : β} (ac : Acc r b) (x : α) :
                                f x = b → Acc (InvImage r f) x
                                theorem InvImage.accessible {α : Sort u} {β : Sort v} {r : β → β → Prop} {a : α} (f : α → β) (ac : Acc r (f a)) :
                                Acc (InvImage r f) a
                                theorem InvImage.wf {α : Sort u} {β : Sort v} {r : β → β → Prop} (f : α → β) (h : WellFounded r) :
                                @[reducible]
                                def invImage {α : Sort u_1} {β : Sort u_2} (f : α → β) (h : WellFoundedRelation β) :

                                The inverse image of a well-founded relation is well-founded.

                                Equations
                                Instances For
                                  theorem Acc.transGen {α✝ : Sort u_1} {r : α✝ → α✝ → Prop} {a : α✝} (h : Acc r a) :
                                  theorem acc_transGen_iff {α✝ : Sort u_1} {r : α✝ → α✝ → Prop} {a : α✝} :
                                  theorem Acc.inv_of_transGen {α : Sort u_1} {r : α → α → Prop} {x y : α} (h₁ : Acc r x) (h₂ : Relation.TransGen r y x) :
                                  Acc r y

                                  If Acc r x holds and y is transitively related to x, then Acc r y holds, too.

                                  theorem WellFounded.transGen {α✝ : Sort u_1} {r : α✝ → α✝ → Prop} (h : WellFounded r) :
                                  @[instance_reducible]
                                  Equations
                                  Instances For
                                    noncomputable def Nat.strongRecOn {motive : Nat → Sort u} (n : Nat) (ind : (n : Nat) → ((m : Nat) → m < n → motive m) → motive n) :
                                    motive n

                                    Strong induction on the natural numbers.

                                    The induction hypothesis is that all numbers less than a given number satisfy the motive, which should be demonstrated for the given number.

                                    Equations
                                    Instances For
                                      noncomputable def Nat.caseStrongRecOn {motive : Nat → Sort u} (a : Nat) (zero : motive 0) (ind : (n : Nat) → ((m : Nat) → m ≤ n → motive m) → motive n.succ) :
                                      motive a

                                      Case analysis based on strong induction for the natural numbers.

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[reducible, inline]
                                        abbrev measure {α : Sort u} (f : α → Nat) :
                                        Equations
                                        Instances For
                                          @[reducible, inline, instance 100]
                                          instance sizeOfWFRel {α : Sort u} [SizeOf α] :
                                          Equations
                                          inductive Prod.Lex {α : Type u} {β : Type v} (ra : α → α → Prop) (rb : β → β → Prop) :
                                          α × β → α × β → Prop

                                          A lexicographical order based on the orders ra and rb for the elements of pairs.

                                          • left {α : Type u} {β : Type v} {ra : α → α → Prop} {rb : β → β → Prop} {a₁ : α} (b₁ : β) {a₂ : α} (b₂ : β) (h : ra a₁ a₂) : Prod.Lex ra rb (a₁, b₁) (a₂, b₂)

                                            If the first projections of two pairs are ordered, then they are lexicographically ordered.

                                          • right {α : Type u} {β : Type v} {ra : α → α → Prop} {rb : β → β → Prop} (a : α) {b₁ b₂ : β} (h : rb b₁ b₂) : Prod.Lex ra rb (a, b₁) (a, b₂)

                                            If the first projections of two pairs are equal, then they are lexicographically ordered if the second projections are ordered.

                                          Instances For
                                            theorem Prod.lex_def {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} {p q : α × β} :
                                            Prod.Lex r s p q ↔ r p.fst q.fst ∨ p.fst = q.fst ∧ s p.snd q.snd
                                            @[instance_reducible]
                                            instance Prod.Lex.instDecidableRelOfDecidableEq {α : Type u} {β : Type v} [αeqDec : DecidableEq α] {r : α → α → Prop} [rDec : DecidableRel r] {s : β → β → Prop} [sDec : DecidableRel s] :
                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            theorem Prod.Lex.right' {β : Type v} (rb : β → β → Prop) {a₂ : Nat} {b₂ : β} {a₁ : Nat} {b₁ : β} (h₁ : a₁ ≤ a₂) (h₂ : rb b₁ b₂) :
                                            Prod.Lex Nat.lt rb (a₁, b₁) (a₂, b₂)
                                            inductive Prod.RProd {α : Type u} {β : Type v} (ra : α → α → Prop) (rb : β → β → Prop) :
                                            α × β → α × β → Prop
                                            • intro {α : Type u} {β : Type v} {ra : α → α → Prop} {rb : β → β → Prop} {a₁ : α} {b₁ : β} {a₂ : α} {b₂ : β} (h₁ : ra a₁ a₂) (h₂ : rb b₁ b₂) : RProd ra rb (a₁, b₁) (a₂, b₂)
                                            Instances For
                                              theorem Prod.lexAccessible {α : Type u} {β : Type v} {ra : α → α → Prop} {rb : β → β → Prop} {a : α} (aca : Acc ra a) (acb : ∀ (b : β), Acc rb b) (b : β) :
                                              Acc (Prod.Lex ra rb) (a, b)
                                              @[reducible]
                                              def Prod.lex {α : Type u} {β : Type v} (ha : WellFoundedRelation α) (hb : WellFoundedRelation β) :
                                              Equations
                                              Instances For
                                                @[instance_reducible]
                                                Equations
                                                theorem Prod.RProdSubLex {α : Type u} {β : Type v} {ra : α → α → Prop} {rb : β → β → Prop} (a b : α × β) (h : RProd ra rb a b) :
                                                Prod.Lex ra rb a b
                                                @[instance_reducible]
                                                def Prod.rprod {α : Type u} {β : Type v} (ha : WellFoundedRelation α) (hb : WellFoundedRelation β) :
                                                Equations
                                                Instances For
                                                  inductive PSigma.Lex {α : Sort u} {β : α → Sort v} (r : α → α → Prop) (s : (a : α) → β a → β a → Prop) :
                                                  PSigma β → PSigma β → Prop
                                                  • left {α : Sort u} {β : α → Sort v} {r : α → α → Prop} {s : (a : α) → β a → β a → Prop} {a₁ : α} (b₁ : β a₁) {a₂ : α} (b₂ : β a₂) : r a₁ a₂ → Lex r s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
                                                  • right {α : Sort u} {β : α → Sort v} {r : α → α → Prop} {s : (a : α) → β a → β a → Prop} (a : α) {b₁ b₂ : β a} : s a b₁ b₂ → Lex r s ⟨a, b₁⟩ ⟨a, b₂⟩
                                                  Instances For
                                                    theorem PSigma.lexAccessible {α : Sort u} {β : α → Sort v} {r : α → α → Prop} {s : (a : α) → β a → β a → Prop} {a : α} (aca : Acc r a) (acb : ∀ (a : α), WellFounded (s a)) (b : β a) :
                                                    Acc (Lex r s) ⟨a, b⟩
                                                    @[reducible]
                                                    def PSigma.lex {α : Sort u} {β : α → Sort v} (ha : WellFoundedRelation α) (hb : (a : α) → WellFoundedRelation (β a)) :
                                                    Equations
                                                    Instances For
                                                      @[instance_reducible]
                                                      instance PSigma.instWellFoundedRelation {α : Sort u} {β : α → Sort v} [ha : WellFoundedRelation α] [hb : (a : α) → WellFoundedRelation (β a)] :
                                                      Equations
                                                      def PSigma.lexNdep {α : Sort u} {β : Sort v} (r : α → α → Prop) (s : β → β → Prop) :
                                                      (_ : α) ×' β → (_ : α) ×' β → Prop
                                                      Equations
                                                      Instances For
                                                        theorem PSigma.lexNdepWf {α : Sort u} {β : Sort v} {r : α → α → Prop} {s : β → β → Prop} (ha : WellFounded r) (hb : WellFounded s) :
                                                        inductive PSigma.RevLex {α : Sort u} {β : Sort v} (r : α → α → Prop) (s : β → β → Prop) :
                                                        (_ : α) ×' β → (_ : α) ×' β → Prop
                                                        • left {α : Sort u} {β : Sort v} {r : α → α → Prop} {s : β → β → Prop} {a₁ a₂ : α} (b : β) : r a₁ a₂ → RevLex r s ⟨a₁, b⟩ ⟨a₂, b⟩
                                                        • right {α : Sort u} {β : Sort v} {r : α → α → Prop} {s : β → β → Prop} (a₁ : α) {b₁ : β} (a₂ : α) {b₂ : β} : s b₁ b₂ → RevLex r s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
                                                        Instances For
                                                          theorem PSigma.revLexAccessible {α : Sort u} {β : Sort v} {r : α → α → Prop} {s : β → β → Prop} {b : β} (acb : Acc s b) (aca : ∀ (a : α), Acc r a) (a : α) :
                                                          Acc (RevLex r s) ⟨a, b⟩
                                                          theorem PSigma.revLex {α : Sort u} {β : Sort v} {r : α → α → Prop} {s : β → β → Prop} (ha : WellFounded r) (hb : WellFounded s) :
                                                          def PSigma.SkipLeft (α : Type u) {β : Type v} (s : β → β → Prop) :
                                                          (_ : α) ×' β → (_ : α) ×' β → Prop
                                                          Equations
                                                          Instances For
                                                            @[instance_reducible]
                                                            def PSigma.skipLeft (α : Type u) {β : Type v} (hb : WellFoundedRelation β) :
                                                            WellFoundedRelation ((_ : α) ×' β)
                                                            Equations
                                                            Instances For
                                                              theorem PSigma.mkSkipLeft {α : Type u} {β : Type v} {b₁ b₂ : β} {s : β → β → Prop} (a₁ a₂ : α) (h : s b₁ b₂) :
                                                              SkipLeft α s ⟨a₁, b₁⟩ ⟨a₂, b₂⟩
                                                              @[implicit_reducible]

                                                              Helper gadget that prevents reduction of Nat.eager n unless n evaluates to a ground term.

                                                              Equations
                                                              Instances For
                                                                @[implicit_reducible]
                                                                def WellFounded.Nat.fix {α : Sort u} {motive : α → Sort v} (h : α → Nat) (F : (x : α) → ((y : α) → InvImage (fun (x1 x2 : Nat) => x1 < x2) h y x → motive y) → motive x) (x : α) :
                                                                motive x

                                                                A well-founded fixpoint operator specialized for Nat-valued measures. Given a measure h, it expects its higher order function argument F to invoke its argument only on values y that are smaller than x with regard to h.

                                                                In contrast to WellFounded.fix, this fixpoint operator reduces on closed terms. (More precisely: when h x evaluates to a ground value)

                                                                Equations
                                                                Instances For
                                                                  @[implicit_reducible]
                                                                  def WellFounded.Nat.fix.go {α : Sort u} {motive : α → Sort v} (h : α → Nat) (F : (x : α) → ((y : α) → InvImage (fun (x1 x2 : Nat) => x1 < x2) h y x → motive y) → motive x) (fuel : Nat) (x : α) :
                                                                  h x < fuel → motive x
                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For
                                                                    theorem WellFounded.Nat.fix.go_congr {α : Sort u} {motive : α → Sort v} (h : α → Nat) (F : (x : α) → ((y : α) → InvImage (fun (x1 x2 : Nat) => x1 < x2) h y x → motive y) → motive x) (x : α) (fuel₁ fuel₂ : Nat) (h₁ : h x < fuel₁) (h₂ : h x < fuel₂) :
                                                                    go h F fuel₁ x h₁ = go h F fuel₂ x h₂
                                                                    theorem WellFounded.Nat.fix_eq {α : Sort u} {motive : α → Sort v} (h : α → Nat) (F : (x : α) → ((y : α) → InvImage (fun (x1 x2 : Nat) => x1 < x2) h y x → motive y) → motive x) (x : α) :
                                                                    fix h F x = F x fun (y : α) (x : InvImage (fun (x1 x2 : Nat) => x1 < x2) h y x) => fix h F y
                                                                    @[implicit_reducible]
                                                                    def wfParam {α : Sort u} (a : α) :
                                                                    α

                                                                    The wfParam gadget is used internally during the construction of recursive functions by wellfounded recursion, to keep track of the parameter for which the automatic introduction of List.attach (or similar) is plausible.

                                                                    Equations
                                                                    Instances For
                                                                      theorem ite_eq_dite {P : Prop} {α✝ : Sort u_1} {a b : α✝} [Decidable P] :
                                                                      (if P then a else b) = if h : P then binderNameHint h () a else binderNameHint h () b

                                                                      Reverse direction of dite_eq_ite. Used by the well-founded definition preprocessor to extend the context of a termination proof inside if-then-else with the condition.