Documentation

Cslib.Languages.CombinatoryLogic.List

Church-Encoded Lists in SKI Combinatory Logic #

Church-encoded lists for proving SKI ≃ TM equivalence. A list is encoded as λ c n. c a₀ (c a₁ (... (c aₖ n)...)) where each aᵢ is a Church numeral.

Church List Representation #

def Cslib.SKI.IsChurchList :
List SKIProp

A term correctly Church-encodes a list of natural numbers.

Equations
Instances For
    theorem Cslib.SKI.isChurchList_trans {ns : List } {cns cns' : SKI} (h : Relation.ReflTransGen Red cns cns') (hcns' : IsChurchList ns cns') :

    IsChurchList is preserved under multi-step reduction of the term.

    structure Cslib.SKI.IsChurchListPair (prev curr : List ) (p : SKI) :

    Both components of a pair are Church lists.

    Instances For
      theorem Cslib.SKI.isChurchListPair_trans {prev curr : List } {p p' : SKI} (hp : Relation.ReflTransGen Red p p') (hp' : IsChurchListPair prev curr p') :
      IsChurchListPair prev curr p

      IsChurchListPair is preserved under reduction.

      Nil: The empty list #

      The SKI term for the empty list.

      Equations
      Instances For
        theorem Cslib.SKI.List.nil_def (c n : SKI) :
        Relation.ReflTransGen Red ((Nil.app c).app n) n

        Reduction: Nil ⬝ c ⬝ n ↠ n.

        The empty list term correctly represents [].

        Cons: Consing an element onto a list #

        The SKI term for list cons.

        Equations
        Instances For
          theorem Cslib.SKI.List.cons_def (x xs c n : SKI) :
          Relation.ReflTransGen Red ((((Cons.app x).app xs).app c).app n) ((c.app x).app ((xs.app c).app n))

          Reduction: Cons ⬝ x ⬝ xs ⬝ c ⬝ n ↠ c ⬝ x ⬝ (xs ⬝ c ⬝ n).

          theorem Cslib.SKI.List.cons_correct {x : } {xs : List } {cx cxs : SKI} (hcx : IsChurch x cx) (hcxs : IsChurchList xs cxs) :
          IsChurchList (x :: xs) ((Cons.app cx).app cxs)

          Cons preserves Church list representation.

          theorem Cslib.SKI.List.singleton_correct {x : } {cx : SKI} (hcx : IsChurch x cx) :

          Singleton list correctness.

          def Cslib.SKI.List.toChurch :
          List SKI

          The canonical SKI term for a Church-encoded list.

          Equations
          Instances For
            @[simp]
            theorem Cslib.SKI.List.toChurch_cons (x : ) (xs : List ) :
            toChurch (x :: xs) = (Cons.app (SKI.toChurch x)).app (toChurch xs)

            toChurch (x :: xs) = Cons ⬝ SKI.toChurch x ⬝ toChurch xs.

            theorem Cslib.SKI.List.toChurch_correct (ns : List ) :

            toChurch ns correctly represents ns.

            Head: Extract the head of a list #

            The SKI term for list head with default.

            Equations
            Instances For
              theorem Cslib.SKI.List.headD_def (d xs : SKI) :
              Relation.ReflTransGen Red ((HeadD.app d).app xs) ((xs.app K).app d)

              Reduction: HeadD ⬝ d ⬝ xs ↠ xs ⬝ K ⬝ d.

              theorem Cslib.SKI.List.headD_correct {d : } {cd : SKI} (hcd : IsChurch d cd) {ns : List } {cns : SKI} (hcns : IsChurchList ns cns) :
              IsChurch (ns.headD d) ((HeadD.app cd).app cns)

              General head-with-default correctness.

              The SKI term for list head (default 0).

              Equations
              Instances For
                theorem Cslib.SKI.List.head_def (xs : SKI) :
                Relation.ReflTransGen Red (Head.app xs) ((xs.app K).app SKI.Zero)

                Reduction: Head ⬝ xs ↠ xs ⬝ K ⬝ Zero.

                theorem Cslib.SKI.List.head_correct (ns : List ) (cns : SKI) (hcns : IsChurchList ns cns) :
                IsChurch (ns.headD 0) (Head.app cns)

                Head correctness (default 0).

                Tail: Extract the tail of a list #

                Step function for tail: (prev, curr) → (curr, cons h curr)

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

                  The step function for computing list tail.

                  Equations
                  Instances For
                    theorem Cslib.SKI.List.tailStep_def (h p : SKI) :
                    Relation.ReflTransGen Red ((TailStep.app h).app p) ((MkPair.app (Snd.app p)).app ((Cons.app h).app (Snd.app p)))

                    Reduction of the tail step function.

                    tail xs = Fst (xs TailStep (MkPair Nil Nil))

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

                      The tail of a Church-encoded list.

                      Equations
                      Instances For
                        theorem Cslib.SKI.List.tail_def (xs : SKI) :
                        Relation.ReflTransGen Red (Tail.app xs) (Fst.app ((xs.app TailStep).app ((MkPair.app Nil).app Nil)))

                        Reduction: Tail ⬝ xs ↠ Fst ⬝ (xs ⬝ TailStep ⬝ (MkPair ⬝ Nil ⬝ Nil)).

                        @[simp]

                        The initial pair (nil, nil) satisfies the invariant.

                        theorem Cslib.SKI.List.tailStep_correct {x : } {xs : List } {cx p : SKI} (hcx : IsChurch x cx) (hp : IsChurchListPair xs.tail xs p) :
                        IsChurchListPair xs (x :: xs) ((TailStep.app cx).app p)

                        The step function preserves the tail-computing invariant.

                        theorem Cslib.SKI.List.tailFold_correct (ns : List ) (cns : SKI) (hcns : IsChurchList ns cns) :
                        ∃ (p : SKI), Relation.ReflTransGen Red ((cns.app TailStep).app ((MkPair.app Nil).app Nil)) p IsChurchListPair ns.tail ns p
                        theorem Cslib.SKI.List.tail_correct (ns : List ) (cns : SKI) (hcns : IsChurchList ns cns) :
                        IsChurchList ns.tail (Tail.app cns)

                        Tail correctness.

                        Prepending zero to a list (for Code.zero') #

                        theorem Cslib.SKI.List.prependZero_def (xs : SKI) :
                        Relation.ReflTransGen Red (PrependZero.app xs) ((Cons.app SKI.Zero).app xs)

                        Reduction: PrependZero ⬝ xs ↠ Cons ⬝ Zero ⬝ xs.

                        theorem Cslib.SKI.List.prependZero_correct {ns : List } {cns : SKI} (hcns : IsChurchList ns cns) :
                        IsChurchList (0 :: ns) (PrependZero.app cns)

                        Prepending zero preserves Church list representation.

                        Successor on list head (for Code.succ) #

                        theorem Cslib.SKI.List.succHead_correct (ns : List ) (cns : SKI) (hcns : IsChurchList ns cns) :
                        IsChurchList [ns.headD 0 + 1] (SuccHead.app cns)

                        SuccHead correctly computes a singleton containing succ(head ns).