Documentation

Cslib.Computability.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 SKI.Church (n : ) (f x : SKI) :

Function form of the church numerals.

Equations
Instances For
    theorem SKI.church_red (n : ) (f f' x x' : SKI) (hf : f.MRed f') (hx : x.MRed x') :
    (Church n f x).MRed (Church n f' x')

    church commutes with reduction.

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

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

    Equations
    Instances For
      theorem SKI.isChurch_trans (n : ) {a a' : SKI} (h : a.MRed 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 SKI.succ_correct (n : ) (a : SKI) (h : IsChurch n a) :

            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
                def SKI.IsChurchPair (ns : × ) (x : SKI) :

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

                Equations
                Instances For
                  theorem SKI.isChurchPair_trans (ns : × ) (a a' : SKI) (h : a.MRed a') :
                  IsChurchPair ns a'IsChurchPair ns a
                  theorem SKI.predAux_correct (p : SKI) (ns : × ) (h : IsChurchPair ns p) :

                  The stronger induction hypothesis necessary for the proof of pred_correct.

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

                  Equations
                  Instances For

                    A term representing Pred

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

                      Primitive recursion #

                      A term representing IsZero

                      Equations
                      Instances For
                        theorem SKI.isZero_def (a : SKI) :
                        (IsZero.app a).MRed ((a.app (K.app FF)).app TT)
                        theorem 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 SKI.recAux_def (R₀ x g a : SKI) :
                            ((((RecAux.app R₀).app x).app g).app a).MRed (((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 SKI.rec_def (x g a : SKI) :
                              (((Rec.app x).app g).app a).MRed (((SKI.Cond.app x).app ((g.app a).app (((Rec.app x).app g).app (Pred.app a)))).app (IsZero.app a))
                              theorem SKI.rec_zero (x g a : SKI) (ha : IsChurch 0 a) :
                              (((Rec.app x).app g).app a).MRed x
                              theorem SKI.rec_succ (n : ) (x g a : SKI) (ha : IsChurch (n + 1) a) :
                              (((Rec.app x).app g).app a).MRed ((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 SKI.rfindAboveAux_def (R₀ f a : SKI) :
                                  (((RFindAboveAux.app R₀).app a).app f).MRed (((SKI.Cond.app a).app ((R₀.app (SKI.Succ.app a)).app f)).app (IsZero.app (f.app a)))
                                  theorem SKI.rfindAboveAux_base (R₀ f a : SKI) (hfa : IsChurch 0 (f.app a)) :
                                  (((RFindAboveAux.app R₀).app a).app f).MRed a
                                  theorem SKI.rfindAboveAux_step (R₀ f a : SKI) {m : } (hfa : IsChurch (m + 1) (f.app a)) :
                                  (((RFindAboveAux.app R₀).app a).app f).MRed ((R₀.app (SKI.Succ.app a)).app f)

                                  Find the minimal root of fNat above a number n

                                  Equations
                                  Instances For
                                    theorem 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 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 #

                                      Addition: λ n m. n Succ m

                                      Equations
                                      Instances For

                                        A term representing addition on church numerals

                                        Equations
                                        Instances For
                                          theorem SKI.add_def (a b : SKI) :
                                          ((SKI.Add.app a).app b).MRed ((a.app SKI.Succ).app b)
                                          theorem 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)

                                          Multiplication: λ n m. n (Add m) Zero

                                          Equations
                                          Instances For

                                            A term representing multiplication on church numerals

                                            Equations
                                            Instances For
                                              theorem SKI.mul_def (a b : SKI) :
                                              theorem 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)

                                              Subtraction: λ n m. n Pred m

                                              Equations
                                              Instances For

                                                A term representing subtraction on church numerals

                                                Equations
                                                Instances For
                                                  theorem SKI.sub_def (a b : SKI) :
                                                  ((SKI.Sub.app a).app b).MRed ((b.app Pred).app a)
                                                  theorem 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)

                                                  Comparison: (. ≤ .) := λ n m. IsZero ⬝ (Sub ⬝ n ⬝ m)

                                                  Equations
                                                  Instances For
                                                    def SKI.LE :

                                                    A term representing comparison on church numerals

                                                    Equations
                                                    Instances For
                                                      theorem SKI.le_def (a b : SKI) :
                                                      ((SKI.LE.app a).app b).MRed (IsZero.app ((SKI.Sub.app a).app b))
                                                      theorem 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)