Documentation

Cslib.Languages.CombinatoryLogic.Recursion

General recursion in the SKI calculus #

In this file we implement general recursion functions (on the natural numbers), inspired by the formalisation of Mathlib.Computability.Partrec. Since composition (B-combinator) and pairs (MkPair, Fst, Snd) have been implemented in Cslib.Computability.CombinatoryLogic.Basic, what remains are the following definitions and proofs of their correctness.

References #

TODO #

def Cslib.SKI.Church (n : ) (f x : SKI) :

Function form of the church numerals.

Equations
Instances For
    @[simp]
    theorem Cslib.SKI.Church_zero (f x : SKI) :
    Church 0 f x = x
    @[simp]
    theorem Cslib.SKI.Church_succ (n : ) (f x : SKI) :
    Church (n + 1) f x = f.app (Church n f x)
    theorem Cslib.SKI.church_red (n : ) (f f' x x' : SKI) (hf : Relation.ReflTransGen Red f f') (hx : Relation.ReflTransGen Red x x') :

    church commutes with reduction.

    def Cslib.SKI.IsChurch (n : ) (a : SKI) :

    The term a is βη-equivalent to a standard church numeral.

    Equations
    Instances For
      theorem Cslib.SKI.isChurch_trans (n : ) {a a' : SKI} (h : Relation.ReflTransGen Red a a') :
      IsChurch n a'IsChurch n a

      To show IsChurch n a it suffices to show the same for a reduct of a.

      Church numeral basics #

      Church zero := λ f x. x

      Equations
      Instances For

        Church one := λ f x. f x

        Equations
        Instances For

          Church succ := λ a f x. f (a f x) λ a f. B f (a f) λ a. S B a ~ S B

          Equations
          Instances For
            theorem Cslib.SKI.succ_correct (n : ) (a : SKI) (h : IsChurch n a) :

            Build the canonical SKI Church numeral for n.

            Equations
            Instances For
              @[simp]

              toChurch (n + 1) = SucctoChurch n.

              toChurch n correctly represents n.

              To define the predecessor, iterate the function PredAux ⟨i, j⟩ ↦ ⟨j, j+1⟩ on ⟨0,0⟩, then take the first component.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                A term representing PredAux

                Equations
                Instances For

                  Useful auxiliary definition expressing that p represents ns ∈ Nat × Nat.

                  Equations
                  Instances For
                    theorem Cslib.SKI.predAux_correct (p : SKI) (ns : × ) (h : IsChurchPair ns p) :
                    IsChurchPair (ns.2, ns.2 + 1) (PredAux.app p)

                    The stronger induction hypothesis necessary for the proof of pred_correct.

                    Predecessor := λ n. Fst ⬝ (n ⬝ PredAux ⬝ (MkPair ⬝ Zero ⬝ Zero))

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      A term representing Pred

                      Equations
                      Instances For
                        theorem Cslib.SKI.pred_correct (n : ) (a : SKI) (h : IsChurch n a) :

                        Primitive recursion #

                        A term representing IsZero

                        Equations
                        Instances For
                          theorem Cslib.SKI.isZero_correct (n : ) (a : SKI) (h : IsChurch n a) :
                          IsBool (decide (n = 0)) (IsZero.app a)

                          To define Rec x g n := if n==0 then x else (Rec x g (Pred n)), we obtain a fixed point of R ↦ λ x g n. Cond ⬝ (IsZero ⬝ n) ⬝ x ⬝ (g ⬝ a ⬝ (R ⬝ x ⬝ g ⬝ (Pred ⬝ n)))

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            A term representing RecAux

                            Equations
                            Instances For
                              theorem Cslib.SKI.recAux_def (R₀ x g a : SKI) :
                              Relation.ReflTransGen Red ((((RecAux.app R₀).app x).app g).app a) (((SKI.Cond.app x).app ((g.app a).app (((R₀.app x).app g).app (Pred.app a)))).app (IsZero.app a))

                              We define Rec so that Rec ⬝ x ⬝ g ⬝ a ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a)

                              Equations
                              Instances For
                                theorem Cslib.SKI.rec_def (x g a : SKI) :
                                Relation.ReflTransGen Red (((Rec.app x).app g).app a) (((SKI.Cond.app x).app ((g.app a).app (((Rec.app x).app g).app (Pred.app a)))).app (IsZero.app a))
                                theorem Cslib.SKI.rec_zero (x g a : SKI) (ha : IsChurch 0 a) :
                                theorem Cslib.SKI.rec_succ (n : ) (x g a : SKI) (ha : IsChurch (n + 1) a) :
                                Relation.ReflTransGen Red (((Rec.app x).app g).app a) ((g.app a).app (((Rec.app x).app g).app (Pred.app a)))

                                Root-finding (μ-recursion) #

                                First define an auxiliary function RFindAbove that looks for roots above a fixed number n, as a fixed point of R ↦ λ n f. if f n = 0 then n else R f (n+1) ~ λ n f. Cond ⬝ n (R f (Succ n)) (IsZero (f n))

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  A term representing RFindAboveAux

                                  Equations
                                  Instances For
                                    theorem Cslib.SKI.rfindAboveAux_def (R₀ f a : SKI) :
                                    Relation.ReflTransGen Red (((RFindAboveAux.app R₀).app a).app f) (((SKI.Cond.app a).app ((R₀.app (SKI.Succ.app a)).app f)).app (IsZero.app (f.app a)))
                                    theorem Cslib.SKI.rfindAboveAux_base (R₀ f a : SKI) (hfa : IsChurch 0 (f.app a)) :
                                    theorem Cslib.SKI.rfindAboveAux_step (R₀ f a : SKI) {m : } (hfa : IsChurch (m + 1) (f.app a)) :

                                    Find the minimal root of fNat above a number n

                                    Equations
                                    Instances For
                                      theorem Cslib.SKI.RFindAbove_correct (fNat : ) (f x : SKI) (hf : ∀ (i : ) (y : SKI), IsChurch i yIsChurch (fNat i) (f.app y)) (n m : ) (hx : IsChurch m x) (hroot : fNat (m + n) = 0) (hpos : ∀ (i : ), i < nfNat (m + i) 0) :
                                      IsChurch (m + n) ((RFindAbove.app x).app f)

                                      Ordinary root finding is root finding above zero

                                      Equations
                                      Instances For
                                        theorem Cslib.SKI.RFind_correct (fNat : ) (f : SKI) (hf : ∀ (i : ) (y : SKI), IsChurch i yIsChurch (fNat i) (f.app y)) (n : ) (hroot : fNat n = 0) (hpos : ∀ (i : ), i < nfNat i 0) :

                                        Further numeric operations #

                                        A term representing addition on church numerals

                                        Equations
                                        Instances For
                                          theorem Cslib.SKI.add_correct (n m : ) (a b : SKI) (ha : IsChurch n a) (hb : IsChurch m b) :
                                          IsChurch (n + m) ((SKI.Add.app a).app b)

                                          A term representing multiplication on church numerals

                                          Equations
                                          Instances For
                                            theorem Cslib.SKI.mul_correct {n m : } {a b : SKI} (ha : IsChurch n a) (hb : IsChurch m b) :
                                            IsChurch (n * m) ((SKI.Mul.app a).app b)

                                            A term representing subtraction on church numerals

                                            Equations
                                            Instances For
                                              theorem Cslib.SKI.sub_correct (n m : ) (a b : SKI) (ha : IsChurch n a) (hb : IsChurch m b) :
                                              IsChurch (n - m) ((SKI.Sub.app a).app b)

                                              A term representing comparison on church numerals

                                              Equations
                                              Instances For
                                                theorem Cslib.SKI.le_correct (n m : ) (a b : SKI) (ha : IsChurch n a) (hb : IsChurch m b) :
                                                IsBool (decide (n m)) ((SKI.LE.app a).app b)