Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 92feaf9

Browse files
committed
feat(computability/primrec): list definitions are primrec
1 parent e017f0f commit 92feaf9

File tree

8 files changed

+352
-70
lines changed

8 files changed

+352
-70
lines changed

data/computability/primrec.lean

Lines changed: 271 additions & 42 deletions
Large diffs are not rendered by default.

data/denumerable.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ end
117117

118118
instance denumerable_list : denumerable (list α) := ⟨denumerable_list_aux⟩
119119

120+
@[simp] theorem list_of_nat_zero : of_nat (list α) 0 = [] := rfl
121+
122+
@[simp] theorem list_of_nat_succ (v : ℕ) :
123+
of_nat (list α) (succ v) =
124+
of_nat α v.unpair.2 :: of_nat (list α) v.unpair.1 :=
125+
of_nat_of_decode $ show decode_list (succ v) = _,
126+
begin
127+
cases e : unpair v with v₂ v₁,
128+
simp [decode_list, e],
129+
rw [show decode_list v₂ = decode (list α) v₂,
130+
from rfl, decode_eq_of_nat]; refl
131+
end
132+
120133
section multiset
121134

122135
def lower : list ℕ → ℕ → list ℕ

data/encodable.lean

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ instance sum : encodable (α ⊕ β) :=
9393
⟨encode_sum, decode_sum, λ s,
9494
by cases s; simp [encode_sum, decode_sum];
9595
rw [bodd_bit, div2_bit, decode_sum, encodek]; refl⟩
96+
97+
@[simp] theorem encode_inl (a : α) :
98+
@encode (α ⊕ β) _ (sum.inl a) = bit ff (encode a) := rfl
99+
@[simp] theorem encode_inr (b : β) :
100+
@encode (α ⊕ β) _ (sum.inr b) = bit tt (encode b) := rfl
101+
@[simp] theorem decode_sum_val (n : ℕ) :
102+
decode (α ⊕ β) n = decode_sum n := rfl
103+
96104
end sum
97105

98106
instance bool : encodable bool :=
@@ -173,6 +181,26 @@ def decode_list : ℕ → option (list α)
173181
instance list : encodable (list α) :=
174182
⟨encode_list, decode_list, λ l,
175183
by induction l with a l IH; simp [encode_list, decode_list, unpair_mkpair, encodek, *]⟩
184+
185+
@[simp] theorem encode_list_nil : encode (@nil α) = 0 := rfl
186+
@[simp] theorem encode_list_cons (a : α) (l : list α) :
187+
encode (a :: l) = succ (mkpair (encode l) (encode a)) := rfl
188+
189+
@[simp] theorem decode_list_zero : decode (list α) 0 = some [] := rfl
190+
191+
@[simp] theorem decode_list_succ (v : ℕ) :
192+
decode (list α) (succ v) =
193+
(::) <$> decode α v.unpair.2 <*> decode (list α) v.unpair.1 :=
194+
show decode_list (succ v) = _, begin
195+
cases e : unpair v with v₂ v₁,
196+
simp [decode_list, e], refl
197+
end
198+
199+
theorem length_le_encode : ∀ (l : list α), length l ≤ encode l
200+
| [] := zero_le _
201+
| (a :: l) := succ_le_succ $
202+
le_trans (length_le_encode l) (le_mkpair_left _ _)
203+
176204
end list
177205

178206
section finset

data/list/basic.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ theorem take_take : ∀ (n m) (l : list α), take n (take m l) = take (min n m)
862862
| (succ n) (succ m) nil := by simp
863863
| (succ n) (succ m) (a::l) := by simp [min_succ_succ, take_take]
864864

865+
@[simp] theorem drop_nil : ∀ n, drop n [] = ([] : list α)
866+
| 0 := rfl
867+
| (n+1) := rfl
868+
865869
theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
866870
drop n l = nth_le l n h :: drop (n+1) l
867871
| 0 (a::l) h := rfl

data/nat/basic.lean

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,23 @@ by rw [← nat.add_sub_cancel' h, pow_add]; apply dvd_mul_right
373373
@[simp] theorem bodd_div2_eq (n : ℕ) : bodd_div2 n = (bodd n, div2 n) :=
374374
by unfold bodd div2; cases bodd_div2 n; refl
375375

376-
/- foldl & foldr -/
377-
378-
/-- `foldl op n a` is the `n`-times iterate of `op` on `a`. -/
379-
@[simp] def foldl {α : Sort*} (op : α → α) : ℕ → α → α
380-
| 0 a := a
381-
| (succ k) a := foldl k (op a)
382-
383-
/-- `foldr op n a` is the `n`-times iterate of `op` on `a`.
384-
It is provably the same as `foldl` but has different
385-
definitional equalities. -/
386-
@[simp] def foldr {α : Sort*} (op : α → α) (a : α) : ℕ → α
387-
| 0 := a
388-
| (succ k) := op (foldr k)
376+
/- iterate -/
377+
378+
section
379+
variables {α : Sort*} (op : α → α)
380+
381+
@[simp] theorem iterate_zero (a : α) : op^[0] a = a := rfl
382+
383+
@[simp] theorem iterate_succ (n : ℕ) (a : α) : op^[succ n] a = (op^[n]) (op a) := rfl
384+
385+
theorem iterate_add : ∀ (m n : ℕ) (a : α), op^[m + n] a = (op^[m]) (op^[n] a)
386+
| m 0 a := rfl
387+
| m (succ n) a := iterate_add m n _
388+
389+
theorem iterate_succ' (n : ℕ) (a : α) : op^[succ n] a = op (op^[n] a) :=
390+
by rw [← one_add, iterate_add]; refl
391+
392+
end
389393

390394
/- size and shift -/
391395

data/nat/pairing.lean

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ open prod decidable
1111
namespace nat
1212

1313
/-- Pairing function for the natural numbers. -/
14-
def mkpair (a b : nat) : nat :=
14+
def mkpair (a b : ) : :=
1515
if a < b then b*b + a else a*a + a + b
1616

1717
/-- Unpairing function for the natural numbers. -/
18-
def unpair (n : nat) : nat × nat :=
18+
def unpair (n : ) : × :=
1919
let s := sqrt n in
2020
if n - s*s < s then (n - s*s, s) else (s, n - s*s - s)
2121

22-
@[simp] theorem mkpair_unpair (n : nat) : mkpair (unpair n).1 (unpair n).2 = n :=
22+
@[simp] theorem mkpair_unpair (n : ) : mkpair (unpair n).1 (unpair n).2 = n :=
2323
let s := sqrt n in begin
2424
dsimp [unpair], change sqrt n with s,
2525
have sm : s * s + (n - s * s) = n := nat.add_sub_cancel' (sqrt_le _),
@@ -35,7 +35,7 @@ end
3535
theorem mkpair_unpair' {n a b} (H : unpair n = (a, b)) : mkpair a b = n :=
3636
by simpa [H] using mkpair_unpair n
3737

38-
@[simp] theorem unpair_mkpair (a b : nat) : unpair (mkpair a b) = (a, b) :=
38+
@[simp] theorem unpair_mkpair (a b : ) : unpair (mkpair a b) = (a, b) :=
3939
begin
4040
by_cases a < b; simp [h, mkpair],
4141
{ show unpair (a + b * b) = (a, b),
@@ -55,7 +55,7 @@ begin
5555
nat.add_sub_cancel, nat.add_sub_cancel_left] }
5656
end
5757

58-
theorem unpair_lt {n : nat} (n1 : n ≥ 1) : (unpair n).1 < n :=
58+
theorem unpair_lt {n : } (n1 : n ≥ 1) : (unpair n).1 < n :=
5959
let s := sqrt n in begin
6060
simp [unpair], change sqrt n with s,
6161
by_cases h : n - s * s < s; simp [h],
@@ -65,8 +65,11 @@ let s := sqrt n in begin
6565
exact lt_of_le_of_lt h (nat.sub_lt_self n1 (mul_pos s0 s0)) }
6666
end
6767

68-
theorem unpair_le : ∀ (n : nat), (unpair n).1 ≤ n
68+
theorem unpair_le : ∀ (n : ), (unpair n).1 ≤ n
6969
| 0 := dec_trivial
7070
| (n+1) := le_of_lt (unpair_lt (nat.succ_pos _))
7171

72+
theorem le_mkpair_left (a b : ℕ) : a ≤ mkpair a b :=
73+
by simpa using unpair_le (mkpair a b)
74+
7275
end nat

order/order_iso.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ theorem well_founded_iff_no_descending_seq [is_strict_order α r] : well_founded
154154
show ∀ x : {a // ¬ acc r a}, ∃ y : {a // ¬ acc r a}, r y.1 x.1,
155155
from λ ⟨x, h⟩, classical.by_contradiction $ λ hn, h $
156156
⟨_, λ y h, classical.by_contradiction $ λ na, hn ⟨⟨y, na⟩, h⟩⟩ in
157-
N ⟨nat_gt (λ n, (n.foldr f ⟨a, na⟩).1) $ λ n, h _⟩⟩⟩
157+
N ⟨nat_gt (λ n, (f^[n] ⟨a, na⟩).1) $ λ n,
158+
by rw nat.iterate_succ'; apply h⟩⟩⟩
158159

159160
end order_embedding
160161

set_theory/ordinal.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,20 +2556,20 @@ by rw [list.sorted, list.pairwise_map]; exact CNF_pairwise b o
25562556
/-- The next fixed point function, the least fixed point of the
25572557
normal function `f` above `a`. -/
25582558
def nfp (f : ordinal → ordinal) (a : ordinal) :=
2559-
sup (λ n : ℕ, n.foldr f a)
2559+
sup (λ n : ℕ, f^[n] a)
25602560

2561-
theorem foldr_le_nfp (f a n) : nat.foldr f a n ≤ nfp f a :=
2561+
theorem iterate_le_nfp (f a n) : f^[n] a ≤ nfp f a :=
25622562
le_sup _ n
25632563

25642564
theorem le_nfp_self (f a) : a ≤ nfp f a :=
2565-
foldr_le_nfp f a 0
2565+
iterate_le_nfp f a 0
25662566

25672567
theorem is_normal.lt_nfp {f} (H : is_normal f) {a b} :
25682568
f b < nfp f a ↔ b < nfp f a :=
25692569
lt_sup.trans $ iff.trans
25702570
(by exact
25712571
⟨λ ⟨n, h⟩, ⟨n, lt_of_le_of_lt (H.le_self _) h⟩,
2572-
λ ⟨n, h⟩, ⟨n+1, H.lt_iff.2 h⟩⟩)
2572+
λ ⟨n, h⟩, ⟨n+1, by rw nat.iterate_succ'; exact H.lt_iff.2 h⟩⟩)
25732573
lt_sup.symm
25742574

25752575
theorem is_normal.nfp_le {f} (H : is_normal f) {a b} :
@@ -2579,8 +2579,8 @@ le_iff_le_iff_lt_iff_lt.2 H.lt_nfp
25792579
theorem is_normal.nfp_le_fp {f} (H : is_normal f) {a b}
25802580
(ab : a ≤ b) (h : f b ≤ b) : nfp f a ≤ b :=
25812581
sup_le.2 $ λ i, begin
2582-
induction i with i IH, {exact ab},
2583-
exact le_trans (H.le_iff.2 IH) h
2582+
induction i with i IH generalizing a, {exact ab},
2583+
exact IH (le_trans (H.le_iff.2 ab) h),
25842584
end
25852585

25862586
theorem is_normal.nfp_fp {f} (H : is_normal f) (a) : f (nfp f a) = nfp f a :=
@@ -2589,13 +2589,13 @@ begin
25892589
cases le_or_lt (f a) a with aa aa,
25902590
{ rwa le_antisymm (H.nfp_le_fp (le_refl _) aa) (le_nfp_self _ _) },
25912591
rcases zero_or_succ_or_limit (nfp f a) with e|⟨b, e⟩|l,
2592-
{ refine @le_trans _ _ _ (f a) _ (H.le_iff.2 _) (foldr_le_nfp f a 1),
2592+
{ refine @le_trans _ _ _ (f a) _ (H.le_iff.2 _) (iterate_le_nfp f a 1),
25932593
simp [e, zero_le] },
25942594
{ have : f b < nfp f a := H.lt_nfp.2 (by simp [e, lt_succ_self]),
25952595
rw [e, lt_succ] at this,
25962596
have ab : a ≤ b,
25972597
{ rw [← lt_succ, ← e],
2598-
exact lt_of_lt_of_le aa (foldr_le_nfp f a 1) },
2598+
exact lt_of_lt_of_le aa (iterate_le_nfp f a 1) },
25992599
refine le_trans (H.le_iff.2 (H.nfp_le_fp ab this))
26002600
(le_trans this (le_of_lt _)),
26012601
simp [e, lt_succ_self] },

0 commit comments

Comments
 (0)