chars int64 40 24k | content_type stringclasses 9
values | id stringlengths 13 64 | source stringclasses 5
values | subset stringclasses 31
values | text stringlengths 40 24k |
|---|---|---|---|---|---|
89 | leanexplore_declaration | leanexplore:387855 | leanexplore_sqlite | leanexplore_declarations | instance : DecidablePred RedexFree := fun _ => decidable_of_iff' _ redexFree_iff_evalStep |
356 | leanexplore_declaration | leanexplore:387856 | leanexplore_sqlite | leanexplore_declarations | /-- A term is redex free iff its only many-step reduction is itself. -/
theorem redexFree_iff_mred_eq {x : SKI} : x.RedexFree ↔ ∀ y, (x ↠ y) ↔ x = y := by
constructor
case mp => grind [RedexFree.normal_red]
case mpr =>
intro h
rw [redexFree_iff]
intro ⟨y, hy⟩
specialize h y
exact Red.ne hy (h.... |
260 | leanexplore_declaration | leanexplore:387857 | leanexplore_sqlite | leanexplore_declarations | /-- If a term has a common reduct with a normal term, it in fact reduces to that term. -/
theorem mJoin_red_redexFree {x y : SKI} (hy : y.RedexFree) (h : MJoin Red x y) : x ↠ y :=
let ⟨w, hyw, hzw⟩ := h
(redexFree_iff_mred_eq.1 hy _ |>.1 hzw : y = w) ▸ hyw |
291 | leanexplore_declaration | leanexplore:387858 | leanexplore_sqlite | leanexplore_declarations | /-- If `x` reduces to both `y` and `z`, and `z` is not reducible, then `y` reduces to `z`. -/
lemma confluent_redexFree {x y z : SKI} (hxy : x ↠ y) (hxz : x ↠ z) (hz : RedexFree z) : y ↠ z :=
let ⟨w, hyw, hzw⟩ := MRed.diamond hxy hxz
(redexFree_iff_mred_eq.1 hz _ |>.1 hzw : z = w) ▸ hyw |
292 | leanexplore_declaration | leanexplore:387859 | leanexplore_sqlite | leanexplore_declarations | /--
If `x` reduces to both `y` and `z`, and both `y` and `z` are in normal form, then they are equal.
-/
lemma unique_normal_form {x y z : SKI}
(hxy : x ↠ y) (hxz : x ↠ z) (hy : y.RedexFree) (hz : RedexFree z) : y = z :=
(redexFree_iff_mred_eq.1 hy _).1 (confluent_redexFree hxy hxz hz) |
258 | leanexplore_declaration | leanexplore:387860 | leanexplore_sqlite | leanexplore_declarations | /-- If `x` and `y` are normal and have a common reduct, then they are equal. -/
lemma eq_of_mJoin_red_redexFree {x y : SKI} (h : MJoin Red x y)
(hx : x.RedexFree) (hy : y.RedexFree) : x = y :=
(redexFree_iff_mred_eq.1 hx _).1 (mJoin_red_redexFree hy h) |
245 | leanexplore_declaration | leanexplore:387861 | leanexplore_sqlite | leanexplore_declarations | lemma sk_nequiv : ¬ MJoin Red S K := by
intro ⟨z, hsz, hkz⟩
have hS : RedexFree S := by simp [RedexFree]
have hK : RedexFree K := by simp [RedexFree]
cases (redexFree_iff_mred_eq.1 hS z).1 hsz
cases (redexFree_iff_mred_eq.1 hK _).1 hkz |
593 | leanexplore_declaration | leanexplore:387862 | leanexplore_sqlite | leanexplore_declarations | /-- Injectivity for booleans. -/
theorem isBool_injective (x y : SKI) (u v : Bool) (hx : IsBool u x) (hy : IsBool v y)
(hxy : MJoin Red x y) : u = v := by
have h : MJoin Red (if u then S else K) (if v then S else K) := by
apply mJoin_red_equivalence.trans (y := x ⬝ S ⬝ K)
· apply mJoin_red_equivalence.sym... |
137 | leanexplore_declaration | leanexplore:387863 | leanexplore_sqlite | leanexplore_declarations | lemma TF_nequiv : ¬ MJoin Red TT FF := fun h =>
(Bool.eq_not_self true).mp <| isBool_injective TT FF true false TT_correct FF_correct h |
110 | leanexplore_declaration | leanexplore:387864 | leanexplore_sqlite | leanexplore_declarations | /-- A specialisation of `Church : Nat → SKI`. -/
def churchK : Nat → SKI
| 0 => K
| n+1 => K ⬝ (churchK n) |
127 | leanexplore_declaration | leanexplore:387865 | leanexplore_sqlite | leanexplore_declarations | lemma churchK_church : (n : Nat) → churchK n = Church n K K
| 0 => rfl
| n+1 => by simp [churchK, Church, churchK_church n] |
107 | leanexplore_declaration | leanexplore:387866 | leanexplore_sqlite | leanexplore_declarations | lemma churchK_redexFree : (n : Nat) → RedexFree (churchK n)
| 0 => trivial
| n+1 => churchK_redexFree n |
202 | leanexplore_declaration | leanexplore:387867 | leanexplore_sqlite | leanexplore_declarations | @[simp]
lemma churchK_size : (n : Nat) → (churchK n).size = n+1
| 0 => rfl
| n + 1 => by rw [churchK, size, size, churchK_size, Nat.add_comm]
lemma churchK_injective : Function.Injective churchK := |
105 | leanexplore_declaration | leanexplore:387868 | leanexplore_sqlite | leanexplore_declarations | lemma churchK_injective : Function.Injective churchK :=
fun n m h => by simpa using congrArg SKI.size h |
673 | leanexplore_declaration | leanexplore:387869 | leanexplore_sqlite | leanexplore_declarations | /-- Injectivity for Church numerals -/
theorem isChurch_injective (x y : SKI) (n m : Nat) (hx : IsChurch n x) (hy : IsChurch m y)
(hxy : MJoin Red x y) : n = m := by
suffices MJoin Red (churchK n) (churchK m) by
apply churchK_injective
exact eq_of_mJoin_red_redexFree this (churchK_redexFree n) (churchK_re... |
1,721 | leanexplore_declaration | leanexplore:387870 | leanexplore_sqlite | leanexplore_declarations | /--
**Rice's theorem**: no SKI term is a non-trivial predicate.
More specifically, say a term `P` is a *predicate* if, for every term `x`, `P · x` reduces to either
`TT` or `FF`. A predicate `P` is *trivial* if either it always reduces to true, or always to false.
This version of Rice's theorem derives a contradiction... |
504 | leanexplore_declaration | leanexplore:387871 | leanexplore_sqlite | leanexplore_declarations | /-- **Rice's theorem**: any SKI predicate is trivial.
This version of Rice's theorem proves (classically) that any SKI predicate `P` either is constantly
true (ie `P · x ↠ TT` for every `x`) or is constantly false (`P · x ↠ FF` for every `x`).
-/
theorem rice' {P : SKI} (hP : ∀ x : SKI, ((P ⬝ x) ↠ TT) ∨ (P ⬝ x) ↠ FF) ... |
290 | leanexplore_declaration | leanexplore:387872 | leanexplore_sqlite | leanexplore_declarations | /-- A term correctly Church-encodes a list of natural numbers. -/
def IsChurchList : List ℕ → SKI → Prop
| [], cns => ∀ c n : SKI, (cns ⬝ c ⬝ n) ↠ n
| x :: xs, cns => ∀ c n : SKI,
∃ cx cxs : SKI, IsChurch x cx ∧ IsChurchList xs cxs ∧
(cns ⬝ c ⬝ n) ↠ c ⬝ cx ⬝ (cxs ⬝ c ⬝ n) |
491 | leanexplore_declaration | leanexplore:387873 | leanexplore_sqlite | leanexplore_declarations | /-- `IsChurchList` is preserved under multi-step reduction of the term. -/
theorem isChurchList_trans {ns : List ℕ} {cns cns' : SKI} (h : cns ↠ cns')
(hcns' : IsChurchList ns cns') : IsChurchList ns cns := by
match ns with
| [] =>
intro c n
exact Trans.trans (parallel_mRed (MRed.head c h) .refl) (hcns' ... |
193 | leanexplore_declaration | leanexplore:387874 | leanexplore_sqlite | leanexplore_declarations | /-- Both components of a pair are Church lists. -/
structure IsChurchListPair (prev curr : List ℕ) (p : SKI) : Prop where
fst : IsChurchList prev (Fst ⬝ p)
snd : IsChurchList curr (Snd ⬝ p) |
371 | leanexplore_declaration | leanexplore:387875 | leanexplore_sqlite | leanexplore_declarations | /-- IsChurchListPair is preserved under reduction. -/
@[scoped grind →]
theorem isChurchListPair_trans {prev curr : List ℕ} {p p' : SKI} (hp : p ↠ p')
(hp' : IsChurchListPair prev curr p') : IsChurchListPair prev curr p := by
constructor
· apply isChurchList_trans (MRed.tail Fst hp)
exact hp'.1
· apply is... |
58 | leanexplore_declaration | leanexplore:387876 | leanexplore_sqlite | leanexplore_declarations | /-- nil = λ c n. n -/
def NilPoly : SKI.Polynomial 2 := &1 |
70 | leanexplore_declaration | leanexplore:387877 | leanexplore_sqlite | leanexplore_declarations | /-- The SKI term for the empty list. -/
def Nil : SKI := NilPoly.toSKI |
128 | leanexplore_declaration | leanexplore:387878 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `Nil ⬝ c ⬝ n ↠ n`. -/
theorem nil_def (c n : SKI) : (Nil ⬝ c ⬝ n) ↠ n :=
NilPoly.toSKI_correct [c, n] (by simp) |
106 | leanexplore_declaration | leanexplore:387879 | leanexplore_sqlite | leanexplore_declarations | /-- The empty list term correctly represents `[]`. -/
theorem nil_correct : IsChurchList [] Nil := nil_def |
102 | leanexplore_declaration | leanexplore:387880 | leanexplore_sqlite | leanexplore_declarations | /-- cons = λ x xs c n. c x (xs c n) -/
def ConsPoly : SKI.Polynomial 4 := &2 ⬝' &0 ⬝' (&1 ⬝' &2 ⬝' &3) |
67 | leanexplore_declaration | leanexplore:387881 | leanexplore_sqlite | leanexplore_declarations | /-- The SKI term for list cons. -/
def Cons : SKI := ConsPoly.toSKI |
204 | leanexplore_declaration | leanexplore:387882 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `Cons ⬝ x ⬝ xs ⬝ c ⬝ n ↠ c ⬝ x ⬝ (xs ⬝ c ⬝ n)`. -/
theorem cons_def (x xs c n : SKI) :
(Cons ⬝ x ⬝ xs ⬝ c ⬝ n) ↠ c ⬝ x ⬝ (xs ⬝ c ⬝ n) :=
ConsPoly.toSKI_correct [x, xs, c, n] (by simp) |
280 | leanexplore_declaration | leanexplore:387883 | leanexplore_sqlite | leanexplore_declarations | /-- Cons preserves Church list representation. -/
theorem cons_correct {x : ℕ} {xs : List ℕ} {cx cxs : SKI}
(hcx : IsChurch x cx) (hcxs : IsChurchList xs cxs) :
IsChurchList (x :: xs) (Cons ⬝ cx ⬝ cxs) := by
intro c n
use cx, cxs, hcx, hcxs
exact cons_def cx cxs c n |
176 | leanexplore_declaration | leanexplore:387884 | leanexplore_sqlite | leanexplore_declarations | /-- Singleton list correctness. -/
theorem singleton_correct {x : ℕ} {cx : SKI} (hcx : IsChurch x cx) :
IsChurchList [x] (Cons ⬝ cx ⬝ Nil) :=
cons_correct hcx nil_correct |
153 | leanexplore_declaration | leanexplore:387885 | leanexplore_sqlite | leanexplore_declarations | /-- The canonical SKI term for a Church-encoded list. -/
def toChurch : List ℕ → SKI
| [] => Nil
| x :: xs => Cons ⬝ (SKI.toChurch x) ⬝ (toChurch xs) |
81 | leanexplore_declaration | leanexplore:387886 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch [] = Nil`. -/
@[simp]
lemma toChurch_nil : toChurch [] = Nil := rfl |
190 | leanexplore_declaration | leanexplore:387887 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch (x :: xs) = Cons ⬝ SKI.toChurch x ⬝ toChurch xs`. -/
@[simp]
lemma toChurch_cons (x : ℕ) (xs : List ℕ) :
toChurch (x :: xs) = Cons ⬝ (SKI.toChurch x) ⬝ (toChurch xs) := rfl |
240 | leanexplore_declaration | leanexplore:387888 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch ns` correctly represents `ns`. -/
theorem toChurch_correct (ns : List ℕ) : IsChurchList ns (toChurch ns) := by
induction ns with
| nil => exact nil_correct
| cons x xs ih => exact cons_correct (SKI.toChurch_correct x) ih |
103 | leanexplore_declaration | leanexplore:387889 | leanexplore_sqlite | leanexplore_declarations | /-- headD d xs = xs K d (returns d for empty list) -/
def HeadDPoly : SKI.Polynomial 2 := &1 ⬝' K ⬝' &0 |
82 | leanexplore_declaration | leanexplore:387890 | leanexplore_sqlite | leanexplore_declarations | /-- The SKI term for list head with default. -/
def HeadD : SKI := HeadDPoly.toSKI |
158 | leanexplore_declaration | leanexplore:387891 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `HeadD ⬝ d ⬝ xs ↠ xs ⬝ K ⬝ d`. -/
theorem headD_def (d xs : SKI) : (HeadD ⬝ d ⬝ xs) ↠ xs ⬝ K ⬝ d :=
HeadDPoly.toSKI_correct [d, xs] (by simp) |
586 | leanexplore_declaration | leanexplore:387892 | leanexplore_sqlite | leanexplore_declarations | /-- General head-with-default correctness. -/
theorem headD_correct {d : ℕ} {cd : SKI} (hcd : IsChurch d cd)
{ns : List ℕ} {cns : SKI} (hcns : IsChurchList ns cns) :
IsChurch (ns.headD d) (HeadD ⬝ cd ⬝ cns) := by
match ns with
| [] =>
simp only [List.headD_nil]
apply isChurch_trans d (headD_def cd c... |
81 | leanexplore_declaration | leanexplore:387893 | leanexplore_sqlite | leanexplore_declarations | /-- The SKI term for list head (default 0). -/
def Head : SKI := HeadD ⬝ SKI.Zero |
135 | leanexplore_declaration | leanexplore:387894 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `Head ⬝ xs ↠ xs ⬝ K ⬝ Zero`. -/
theorem head_def (xs : SKI) : (Head ⬝ xs) ↠ xs ⬝ K ⬝ SKI.Zero :=
headD_def SKI.Zero xs |
190 | leanexplore_declaration | leanexplore:387895 | leanexplore_sqlite | leanexplore_declarations | /-- Head correctness (default 0). -/
theorem head_correct (ns : List ℕ) (cns : SKI) (hcns : IsChurchList ns cns) :
IsChurch (ns.headD 0) (Head ⬝ cns) :=
headD_correct zero_correct hcns |
159 | leanexplore_declaration | leanexplore:387896 | leanexplore_sqlite | leanexplore_declarations | /-- Step function for tail: (prev, curr) → (curr, cons h curr) -/
def TailStepPoly : SKI.Polynomial 2 :=
MkPair ⬝' (Snd ⬝' &1) ⬝' (Cons ⬝' &0 ⬝' (Snd ⬝' &1)) |
90 | leanexplore_declaration | leanexplore:387897 | leanexplore_sqlite | leanexplore_declarations | /-- The step function for computing list tail. -/
def TailStep : SKI := TailStepPoly.toSKI |
196 | leanexplore_declaration | leanexplore:387898 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction of the tail step function. -/
theorem tailStep_def (h p : SKI) :
(TailStep ⬝ h ⬝ p) ↠ MkPair ⬝ (Snd ⬝ p) ⬝ (Cons ⬝ h ⬝ (Snd ⬝ p)) :=
TailStepPoly.toSKI_correct [h, p] (by simp) |
136 | leanexplore_declaration | leanexplore:387899 | leanexplore_sqlite | leanexplore_declarations | /-- tail xs = Fst (xs TailStep (MkPair Nil Nil)) -/
def TailPoly : SKI.Polynomial 1 :=
Fst ⬝' (&0 ⬝' TailStep ⬝' (MkPair ⬝ Nil ⬝ Nil)) |
74 | leanexplore_declaration | leanexplore:387900 | leanexplore_sqlite | leanexplore_declarations | /-- The tail of a Church-encoded list. -/
def Tail : SKI := TailPoly.toSKI |
213 | leanexplore_declaration | leanexplore:387901 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `Tail ⬝ xs ↠ Fst ⬝ (xs ⬝ TailStep ⬝ (MkPair ⬝ Nil ⬝ Nil))`. -/
theorem tail_def (xs : SKI) :
(Tail ⬝ xs) ↠ Fst ⬝ (xs ⬝ TailStep ⬝ (MkPair ⬝ Nil ⬝ Nil)) :=
TailPoly.toSKI_correct [xs] (by simp) |
283 | leanexplore_declaration | leanexplore:387902 | leanexplore_sqlite | leanexplore_declarations | /-- The initial pair (nil, nil) satisfies the invariant. -/
@[simp]
theorem tail_init : IsChurchListPair [] [] (MkPair ⬝ Nil ⬝ Nil) := by
constructor
· apply isChurchList_trans (fst_correct _ _); exact nil_correct
· apply isChurchList_trans (snd_correct _ _); exact nil_correct |
423 | leanexplore_declaration | leanexplore:387903 | leanexplore_sqlite | leanexplore_declarations | /-- The step function preserves the tail-computing invariant. -/
theorem tailStep_correct {x : ℕ} {xs : List ℕ} {cx p : SKI}
(hcx : IsChurch x cx) (hp : IsChurchListPair xs.tail xs p) :
IsChurchListPair xs (x :: xs) (TailStep ⬝ cx ⬝ p) := by
apply isChurchListPair_trans (tailStep_def cx p)
exact ⟨isChurchLi... |
1,170 | leanexplore_declaration | leanexplore:387904 | leanexplore_sqlite | leanexplore_declarations | theorem tailFold_correct (ns : List ℕ) (cns : SKI) (hcns : IsChurchList ns cns) :
∃ p, (cns ⬝ TailStep ⬝ (MkPair ⬝ Nil ⬝ Nil)) ↠ p ∧
IsChurchListPair ns.tail ns p := by
induction ns generalizing cns with
| nil =>
-- For empty list, the fold returns the initial pair
use MkPair ⬝ Nil ⬝ Nil
co... |
499 | leanexplore_declaration | leanexplore:387905 | leanexplore_sqlite | leanexplore_declarations | /-- Tail correctness. -/
theorem tail_correct (ns : List ℕ) (cns : SKI) (hcns : IsChurchList ns cns) :
IsChurchList ns.tail (Tail ⬝ cns) := by
-- Tail ⬝ cns ↠ Fst ⬝ (cns ⬝ TailStep ⬝ (MkPair ⬝ Nil ⬝ Nil))
apply isChurchList_trans (tail_def cns)
-- Get the fold result
obtain ⟨p, hp_red, hp_pair⟩ := tailFold_... |
117 | leanexplore_declaration | leanexplore:387906 | leanexplore_sqlite | leanexplore_declarations | /-- PrependZero xs = cons 0 xs = Cons ⬝ Zero ⬝ xs -/
def PrependZeroPoly : SKI.Polynomial 1 := Cons ⬝' SKI.Zero ⬝' &0 |
92 | leanexplore_declaration | leanexplore:387907 | leanexplore_sqlite | leanexplore_declarations | /-- Prepend zero to a Church-encoded list. -/
def PrependZero : SKI := PrependZeroPoly.toSKI |
185 | leanexplore_declaration | leanexplore:387908 | leanexplore_sqlite | leanexplore_declarations | /-- Reduction: `PrependZero ⬝ xs ↠ Cons ⬝ Zero ⬝ xs`. -/
theorem prependZero_def (xs : SKI) : (PrependZero ⬝ xs) ↠ Cons ⬝ SKI.Zero ⬝ xs :=
PrependZeroPoly.toSKI_correct [xs] (by simp) |
286 | leanexplore_declaration | leanexplore:387909 | leanexplore_sqlite | leanexplore_declarations | /-- Prepending zero preserves Church list representation. -/
theorem prependZero_correct {ns : List ℕ} {cns : SKI} (hcns : IsChurchList ns cns) :
IsChurchList (0 :: ns) (PrependZero ⬝ cns) := by
apply isChurchList_trans (prependZero_def cns)
exact cons_correct zero_correct hcns |
113 | leanexplore_declaration | leanexplore:387910 | leanexplore_sqlite | leanexplore_declarations | /-- SuccHead xs = cons (succ (head xs)) nil -/
def SuccHead : SKI := B ⬝ (C ⬝ Cons ⬝ Nil) ⬝ (B ⬝ SKI.Succ ⬝ Head) |
456 | leanexplore_declaration | leanexplore:387911 | leanexplore_sqlite | leanexplore_declarations | /-- `SuccHead` correctly computes a singleton containing `succ(head ns)`. -/
theorem succHead_correct (ns : List ℕ) (cns : SKI) (hcns : IsChurchList ns cns) :
IsChurchList [ns.headD 0 + 1] (SuccHead ⬝ cns) := by
have hhead := head_correct ns cns hcns
have hsucc := succ_correct (ns.headD 0) (Head ⬝ cns) hhead
... |
136 | leanexplore_declaration | leanexplore:387912 | leanexplore_sqlite | leanexplore_declarations | /-- Function form of the church numerals. -/
def Church (n : Nat) (f x : SKI) : SKI :=
match n with
| 0 => x
| n+1 => f ⬝ (Church n f x) |
156 | leanexplore_declaration | leanexplore:387913 | leanexplore_sqlite | leanexplore_declarations | @[simp] lemma Church_zero (f x : SKI) : Church 0 f x = x := rfl
@[simp] lemma Church_succ (n : Nat) (f x : SKI) : Church (n+1) f x = f ⬝ Church n f x := rfl |
92 | leanexplore_declaration | leanexplore:387914 | leanexplore_sqlite | leanexplore_declarations | @[simp] lemma Church_succ (n : Nat) (f x : SKI) : Church (n+1) f x = f ⬝ Church n f x := rfl |
238 | leanexplore_declaration | leanexplore:387915 | leanexplore_sqlite | leanexplore_declarations | /-- `church` commutes with reduction. -/
lemma church_red (n : Nat) (f f' x x' : SKI) (hf : f ↠ f') (hx : x ↠ x') :
Church n f x ↠ Church n f' x' := by
induction n with
| zero => exact hx
| succ n ih => exact parallel_mRed hf ih |
154 | leanexplore_declaration | leanexplore:387916 | leanexplore_sqlite | leanexplore_declarations | /-- The term `a` is βη-equivalent to a standard church numeral. -/
def IsChurch (n : Nat) (a : SKI) : Prop :=
∀ f x :SKI, (a ⬝ f ⬝ x) ↠ (Church n f x) |
323 | leanexplore_declaration | leanexplore:387917 | leanexplore_sqlite | leanexplore_declarations | /-- To show `IsChurch n a` it suffices to show the same for a reduct of `a`. -/
theorem isChurch_trans (n : Nat) {a a' : SKI} (h : a ↠ a') :
IsChurch n a' → IsChurch n a := by
simp_rw [IsChurch]
intro ha' f x
calc
_ ↠ a' ⬝ f ⬝ x := by apply MRed.head; apply MRed.head; exact h
_ ↠ Church n f x := by apply ... |
64 | leanexplore_declaration | leanexplore:387918 | leanexplore_sqlite | leanexplore_declarations | /-- Church zero := λ f x. x -/
protected def Zero : SKI := K ⬝ I |
227 | leanexplore_declaration | leanexplore:387919 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind .]
theorem zero_correct : IsChurch 0 SKI.Zero := by
unfold IsChurch SKI.Zero Church
intro f x
calc
_ ↠ I ⬝ x := by apply Relation.ReflTransGen.single; apply red_head; apply red_K
_ ⭢ x := by apply red_I |
60 | leanexplore_declaration | leanexplore:387920 | leanexplore_sqlite | leanexplore_declarations | /-- Church one := λ f x. f x -/
protected def One : SKI := I |
115 | leanexplore_declaration | leanexplore:387921 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind .]
theorem one_correct : IsChurch 1 SKI.One := by
intro f x
apply head
exact .single (red_I f) |
112 | leanexplore_declaration | leanexplore:387922 | leanexplore_sqlite | leanexplore_declarations | /-- Church succ := λ a f x. f (a f x) ~ λ a f. B f (a f) ~ λ a. S B a ~ S B -/
protected def Succ : SKI := S ⬝ B |
299 | leanexplore_declaration | leanexplore:387923 | leanexplore_sqlite | leanexplore_declarations | @[scoped grind →]
theorem succ_correct (n : Nat) (a : SKI) (h : IsChurch n a) :
IsChurch (n+1) (SKI.Succ ⬝ a) := by
intro f x
calc
_ ⭢ B ⬝ f ⬝ (a ⬝ f) ⬝ x := by apply red_head; apply red_S
_ ↠ f ⬝ (a ⬝ f ⬝ x) := by apply B_def
_ ↠ f ⬝ (Church n f x) := by apply MRed.tail; exact h f x |
132 | leanexplore_declaration | leanexplore:387924 | leanexplore_sqlite | leanexplore_declarations | /-- Build the canonical SKI Church numeral for `n`. -/
def toChurch : ℕ → SKI
| 0 => SKI.Zero
| n + 1 => SKI.Succ ⬝ (toChurch n) |
86 | leanexplore_declaration | leanexplore:387925 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch 0 = Zero`. -/
@[simp] lemma toChurch_zero : toChurch 0 = SKI.Zero := rfl |
134 | leanexplore_declaration | leanexplore:387926 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch (n + 1) = Succ ⬝ toChurch n`. -/
@[simp] lemma toChurch_succ (n : ℕ) : toChurch (n + 1) = SKI.Succ ⬝ (toChurch n) := rfl |
232 | leanexplore_declaration | leanexplore:387927 | leanexplore_sqlite | leanexplore_declarations | /-- `toChurch n` correctly represents `n`. -/
@[scoped grind .]
theorem toChurch_correct (n : ℕ) : IsChurch n (toChurch n) := by
induction n with
| zero => exact zero_correct
| succ n ih => exact succ_correct n (toChurch n) ih |
213 | leanexplore_declaration | leanexplore:387928 | leanexplore_sqlite | leanexplore_declarations | /--
To define the predecessor, iterate the function `PredAux` ⟨i, j⟩ ↦ ⟨j, j+1⟩ on ⟨0,0⟩, then take
the first component.
-/
def PredAuxPoly : SKI.Polynomial 1 := MkPair ⬝' (Snd ⬝' &0) ⬝' (SKI.Succ ⬝' (Snd ⬝' &0)) |
73 | leanexplore_declaration | leanexplore:387929 | leanexplore_sqlite | leanexplore_declarations | /-- A term representing PredAux -/
def PredAux : SKI := PredAuxPoly.toSKI |
137 | leanexplore_declaration | leanexplore:387930 | leanexplore_sqlite | leanexplore_declarations | theorem predAux_def (p : SKI) : (PredAux ⬝ p) ↠ MkPair ⬝ (Snd ⬝ p) ⬝ (SKI.Succ ⬝ (Snd ⬝ p)) :=
PredAuxPoly.toSKI_correct [p] (by simp) |
187 | leanexplore_declaration | leanexplore:387931 | leanexplore_sqlite | leanexplore_declarations | /-- Useful auxiliary definition expressing that `p` represents ns ∈ Nat × Nat. -/
def IsChurchPair (ns : Nat × Nat) (x : SKI) : Prop :=
IsChurch ns.1 (Fst ⬝ x) ∧ IsChurch ns.2 (Snd ⬝ x) |
355 | leanexplore_declaration | leanexplore:387932 | leanexplore_sqlite | leanexplore_declarations | theorem isChurchPair_trans (ns : Nat × Nat) (a a' : SKI) (h : a ↠ a') :
IsChurchPair ns a' → IsChurchPair ns a := by
simp_rw [IsChurchPair]
intro ⟨ha₁,ha₂⟩
constructor
· apply isChurch_trans (a' := Fst ⬝ a')
· apply MRed.tail; exact h
· exact ha₁
· apply isChurch_trans (a' := Snd ⬝ a')
· apply... |
389 | leanexplore_declaration | leanexplore:387933 | leanexplore_sqlite | leanexplore_declarations | theorem predAux_correct (p : SKI) (ns : Nat × Nat) (h : IsChurchPair ns p) :
IsChurchPair ⟨ns.2, ns.2+1⟩ (PredAux ⬝ p) := by
refine isChurchPair_trans _ _ (MkPair ⬝ (Snd ⬝ p) ⬝ (SKI.Succ ⬝ (Snd ⬝ p))) (predAux_def p) ?_
constructor
· exact isChurch_trans ns.2 (fst_correct _ _) h.2
· refine isChurch_trans (n... |
553 | leanexplore_declaration | leanexplore:387934 | leanexplore_sqlite | leanexplore_declarations | /-- The stronger induction hypothesis necessary for the proof of `pred_correct`. -/
theorem predAux_correct' (n : Nat) :
IsChurchPair (n.pred, n) <| Church n PredAux (MkPair ⬝ SKI.Zero ⬝ SKI.Zero) := by
induction n with
| zero =>
apply isChurchPair_trans ⟨0,0⟩ _ (MkPair ⬝ SKI.Zero ⬝ SKI.Zero)
(... |
163 | leanexplore_declaration | leanexplore:387935 | leanexplore_sqlite | leanexplore_declarations | /-- Predecessor := λ n. Fst ⬝ (n ⬝ PredAux ⬝ (MkPair ⬝ Zero ⬝ Zero)) -/
def PredPoly : SKI.Polynomial 1 := Fst ⬝' (&0 ⬝' PredAux ⬝' (MkPair ⬝ SKI.Zero ⬝ SKI.Zero)) |
64 | leanexplore_declaration | leanexplore:387936 | leanexplore_sqlite | leanexplore_declarations | /-- A term representing Pred -/
def Pred : SKI := PredPoly.toSKI |
136 | leanexplore_declaration | leanexplore:387937 | leanexplore_sqlite | leanexplore_declarations | theorem pred_def (a : SKI) : (Pred ⬝ a) ↠ Fst ⬝ (a ⬝ PredAux ⬝ (MkPair ⬝ SKI.Zero ⬝ SKI.Zero)) :=
PredPoly.toSKI_correct [a] (by simp) |
311 | leanexplore_declaration | leanexplore:387938 | leanexplore_sqlite | leanexplore_declarations | theorem pred_correct (n : Nat) (a : SKI) (h : IsChurch n a) : IsChurch n.pred (Pred ⬝ a) := by
refine isChurch_trans n.pred
(pred_def a) ?_
refine isChurch_trans _ (a' := Fst ⬝ (Church n PredAux (MkPair ⬝ SKI.Zero ⬝ SKI.Zero))) ?_ ?_
· apply MRed.tail
exact h _ _
· exact predAux_correct' n |>.1 |
91 | leanexplore_declaration | leanexplore:387939 | leanexplore_sqlite | leanexplore_declarations | /-- IsZero := λ n. n (K FF) TT -/
def IsZeroPoly : SKI.Polynomial 1 := &0 ⬝' (K ⬝ FF) ⬝' TT |
70 | leanexplore_declaration | leanexplore:387940 | leanexplore_sqlite | leanexplore_declarations | /-- A term representing IsZero -/
def IsZero : SKI := IsZeroPoly.toSKI |
107 | leanexplore_declaration | leanexplore:387941 | leanexplore_sqlite | leanexplore_declarations | theorem isZero_def (a : SKI) : (IsZero ⬝ a) ↠ a ⬝ (K ⬝ FF) ⬝ TT :=
IsZeroPoly.toSKI_correct [a] (by simp) |
520 | leanexplore_declaration | leanexplore:387942 | leanexplore_sqlite | leanexplore_declarations | theorem isZero_correct (n : Nat) (a : SKI) (h : IsChurch n a) :
IsBool (n = 0) (IsZero ⬝ a) := by
apply isBool_trans (a' := a ⬝ (K ⬝ FF) ⬝ TT) (h := isZero_def a)
by_cases n=0
case pos h0 =>
simp_rw [h0]
rw [h0] at h
apply isBool_trans (ha' := TT_correct)
exact h _ _
case neg h0 =>
simp_... |
294 | leanexplore_declaration | leanexplore:387943 | leanexplore_sqlite | leanexplore_declarations | /--
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)))
-/
def RecAuxPoly : SKI.Polynomial 4 :=
SKI.Cond ⬝' &1 ⬝' (&2 ⬝' &3 ⬝' (&0 ⬝' &1 ⬝' &2 ⬝' (Pred ⬝' &3))) ⬝' (IsZero ⬝' &3) |
70 | leanexplore_declaration | leanexplore:387944 | leanexplore_sqlite | leanexplore_declarations | /-- A term representing RecAux -/
def RecAux : SKI := RecAuxPoly.toSKI |
196 | leanexplore_declaration | leanexplore:387945 | leanexplore_sqlite | leanexplore_declarations | theorem recAux_def (R₀ x g a : SKI) :
(RecAux ⬝ R₀ ⬝ x ⬝ g ⬝ a) ↠
SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (R₀ ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a) :=
RecAuxPoly.toSKI_correct [R₀, x, g, a] (by simp) |
150 | leanexplore_declaration | leanexplore:387946 | leanexplore_sqlite | leanexplore_declarations | /--
We define Rec so that
`Rec ⬝ x ⬝ g ⬝ a ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a)`
-/
def Rec : SKI := fixedPoint RecAux |
351 | leanexplore_declaration | leanexplore:387947 | leanexplore_sqlite | leanexplore_declarations | theorem rec_def (x g a : SKI) :
(Rec ⬝ x ⬝ g ⬝ a) ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a) := calc
_ ↠ RecAux ⬝ Rec ⬝ x ⬝ g ⬝ a := by
apply MRed.head; apply MRed.head; apply MRed.head
apply fixedPoint_correct
_ ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZe... |
314 | leanexplore_declaration | leanexplore:387948 | leanexplore_sqlite | leanexplore_declarations | theorem rec_zero (x g a : SKI) (ha : IsChurch 0 a) : (Rec ⬝ x ⬝ g ⬝ a) ↠ x := by
calc
_ ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a) := rec_def _ _ _
_ ↠ if (Nat.beq 0 0) then x else (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) := by
apply cond_correct
exact isZero_correct 0 a ha |
375 | leanexplore_declaration | leanexplore:387949 | leanexplore_sqlite | leanexplore_declarations | theorem rec_succ (n : Nat) (x g a : SKI) (ha : IsChurch (n + 1) a) :
(Rec ⬝ x ⬝ g ⬝ a) ↠ g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a)) := by
calc
_ ↠ SKI.Cond ⬝ x ⬝ (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) ⬝ (IsZero ⬝ a) := rec_def _ _ _
_ ↠ if (Nat.beq (n+1) 0) then x else (g ⬝ a ⬝ (Rec ⬝ x ⬝ g ⬝ (Pred ⬝ a))) := by
a... |
351 | leanexplore_declaration | leanexplore:387950 | leanexplore_sqlite | leanexplore_declarations | /--
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))
-/
def RFindAboveAuxPoly : SKI.Polynomial 3 :=
SKI.Cond ⬝' &1 ⬝' (&0 ⬝' (SKI.Succ ⬝' &1) ⬝... |
91 | leanexplore_declaration | leanexplore:387951 | leanexplore_sqlite | leanexplore_declarations | /-- A term representing RFindAboveAux -/
def RFindAboveAux : SKI := RFindAboveAuxPoly.toSKI |
198 | leanexplore_declaration | leanexplore:387952 | leanexplore_sqlite | leanexplore_declarations | lemma rfindAboveAux_def (R₀ f a : SKI) :
(RFindAboveAux ⬝ R₀ ⬝ a ⬝ f) ↠ SKI.Cond ⬝ a ⬝ (R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f) ⬝ (IsZero ⬝ (f ⬝ a)) :=
RFindAboveAuxPoly.toSKI_correct [R₀, a, f] (by trivial) |
337 | leanexplore_declaration | leanexplore:387953 | leanexplore_sqlite | leanexplore_declarations | theorem rfindAboveAux_base (R₀ f a : SKI) (hfa : IsChurch 0 (f ⬝ a)) :
(RFindAboveAux ⬝ R₀ ⬝ a ⬝ f) ↠ a := calc
_ ↠ SKI.Cond ⬝ a ⬝ (R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f) ⬝ (IsZero ⬝ (f ⬝ a)) := rfindAboveAux_def _ _ _
_ ↠ if (Nat.beq 0 0) then a else (R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f) := by
apply cond_correct
apply isZer... |
379 | leanexplore_declaration | leanexplore:387954 | leanexplore_sqlite | leanexplore_declarations | theorem rfindAboveAux_step (R₀ f a : SKI) {m : Nat} (hfa : IsChurch (m + 1) (f ⬝ a)) :
(RFindAboveAux ⬝ R₀ ⬝ a ⬝ f) ↠ R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f := calc
_ ↠ SKI.Cond ⬝ a ⬝ (R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f) ⬝ (IsZero ⬝ (f ⬝ a)) := rfindAboveAux_def _ _ _
_ ↠ if (Nat.beq (m+1) 0) then a else (R₀ ⬝ (SKI.Succ ⬝ a) ⬝ f) := by
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.