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

Commit aa78466

Browse files
committed
refactor(*): move in some nat lemmas
and other lemmas from init
1 parent 1b6322f commit aa78466

File tree

15 files changed

+2240
-178
lines changed

15 files changed

+2240
-178
lines changed

data/array/lemmas.lean

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/-
2+
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Leonardo de Moura, Mario Carneiro
5+
-/
6+
import data.list.basic data.buffer
7+
8+
universes u w
9+
10+
namespace array
11+
variables {α : Type u} {β : Type w} {n : nat}
12+
13+
protected def ext : ∀ {a b : array α n} (h : ∀ i, read a i = read b i), a = b
14+
| ⟨f⟩ ⟨g⟩ h := congr_arg array.mk (funext h)
15+
16+
lemma read_write (a : array α n) (i j : fin n) (v : α) :
17+
read (write a i v) j = if i = j then v else a.read j := rfl
18+
19+
lemma read_write_eq (a : array α n) (i : fin n) (v : α) :
20+
read (write a i v) i = v :=
21+
by rw [read_write, if_pos rfl]
22+
23+
lemma read_write_ne (a : array α n) (i j : fin n) (v : α) (h : i ≠ j) :
24+
read (write a i v) j = read a j :=
25+
by rw [read_write, if_neg h]
26+
27+
theorem rev_list_reverse_core (a : array α n) : Π i (h : i ≤ n) (t : list α),
28+
(a.iterate_aux (λ _ v l, v :: l) i h []).reverse_core t = a.rev_iterate_aux (λ _ v l, v :: l) i h t
29+
| 0 h t := rfl
30+
| (i+1) h t := rev_list_reverse_core i _ _
31+
32+
theorem rev_list_reverse (a : array α n) : a.rev_list.reverse = a.to_list :=
33+
rev_list_reverse_core a _ _ _
34+
35+
theorem to_list_reverse (a : array α n) : a.to_list.reverse = a.rev_list :=
36+
by rw [← rev_list_reverse, list.reverse_reverse]
37+
38+
theorem rev_list_length_aux (a : array α n) (i h) : (a.iterate_aux (λ _ v l, v :: l) i h []).length = i :=
39+
by induction i; simp [*, iterate_aux]
40+
41+
theorem rev_list_length (a : array α n) : a.rev_list.length = n :=
42+
rev_list_length_aux a _ _
43+
44+
theorem to_list_length (a : array α n) : a.to_list.length = n :=
45+
by rw[← rev_list_reverse, list.length_reverse, rev_list_length]
46+
47+
theorem to_list_nth_core (a : array α n) (i : ℕ) (ih : i < n) : Π (j) {jh t h'},
48+
(∀k tl, j + k = i → list.nth_le t k tl = a.read ⟨i, ih⟩) → (a.rev_iterate_aux (λ _ v l, v :: l) j jh t).nth_le i h' = a.read ⟨i, ih⟩
49+
| 0 _ _ _ al := al i _ $ zero_add _
50+
| (j+1) jh t h' al := to_list_nth_core j $ λk tl hjk,
51+
show list.nth_le (a.read ⟨j, jh⟩ :: t) k tl = a.read ⟨i, ih⟩, from
52+
match k, hjk, tl with
53+
| 0, e, tl := match i, e, ih with ._, rfl, _ := rfl end
54+
| k'+1, _, tl := by simp[list.nth_le]; exact al _ _ (by simp [*])
55+
end
56+
57+
theorem to_list_nth (a : array α n) (i : ℕ) (h h') : list.nth_le a.to_list i h' = a.read ⟨i, h⟩ :=
58+
to_list_nth_core _ _ _ _ (λk tl, absurd tl $ nat.not_lt_zero _)
59+
60+
theorem mem_iff_rev_list_mem_core (a : array α n) (v : α) : Π i (h : i ≤ n),
61+
(∃ (j : fin n), j.1 < i ∧ read a j = v) ↔ v ∈ a.iterate_aux (λ _ v l, v :: l) i h []
62+
| 0 _ := ⟨λ⟨_, n, _⟩, absurd n $ nat.not_lt_zero _, false.elim⟩
63+
| (i+1) h := let IH := mem_iff_rev_list_mem_core i (le_of_lt h) in
64+
⟨λ⟨j, ji1, e⟩, or.elim (lt_or_eq_of_le $ nat.le_of_succ_le_succ ji1)
65+
(λji, list.mem_cons_of_mem _ $ IH.1 ⟨j, ji, e⟩)
66+
(λje, by simp[iterate_aux]; apply or.inl; have H : j = ⟨i, h⟩ := fin.eq_of_veq je; rwa [← H, e]),
67+
λm, begin
68+
simp[iterate_aux, list.mem] at m,
69+
cases m with e m',
70+
exact ⟨⟨i, h⟩, nat.lt_succ_self _, eq.symm e⟩,
71+
exact let ⟨j, ji, e⟩ := IH.2 m' in
72+
⟨j, nat.le_succ_of_le ji, e⟩
73+
end
74+
75+
theorem mem_iff_rev_list_mem (a : array α n) (v : α) : v ∈ a ↔ v ∈ a.rev_list :=
76+
iff.trans
77+
(exists_congr $ λj, iff.symm $ show j.1 < n ∧ read a j = v ↔ read a j = v, from and_iff_right j.2)
78+
(mem_iff_rev_list_mem_core a v _ _)
79+
80+
theorem mem_iff_list_mem (a : array α n) (v : α) : v ∈ a ↔ v ∈ a.to_list :=
81+
by rw [← rev_list_reverse]; simp[mem_iff_rev_list_mem]
82+
83+
@[simp] theorem to_list_to_array (a : array α n) : a.to_list.to_array == a :=
84+
have array.mk (λ (v : fin n), list.nth_le (to_list a) (v.val) (eq.rec_on (eq.symm (to_list_length a)) (v.is_lt))) = a, from
85+
match a with ⟨f⟩ := congr_arg array.mk $ funext $ λ⟨i, h⟩, to_list_nth ⟨f⟩ i h _ end,
86+
heq_of_heq_of_eq
87+
(@eq.drec_on _ _ (λm (e : a.to_list.length = m), (array.mk (λv, a.to_list.nth_le v.1 v.2)) ==
88+
(@array.mk α m $ λv, a.to_list.nth_le v.1 (eq.rec_on (eq.symm e) v.2))) _ a.to_list_length (heq.refl _)) this
89+
90+
@[simp] theorem to_array_to_list (l : list α) : l.to_array.to_list = l :=
91+
list.ext_le (to_list_length _) $ λn h1 h2, to_list_nth _ _ _ _
92+
93+
lemma push_back_rev_list_core (a : array α n) (v : α) :
94+
∀ i h h',
95+
iterate_aux (a.push_back v) (λ_, list.cons) i h [] =
96+
iterate_aux a (λ_, list.cons) i h' []
97+
| 0 h h' := rfl
98+
| (i+1) h h' := begin
99+
simp [iterate_aux]; rw push_back_rev_list_core,
100+
apply congr_fun, apply congr_arg,
101+
dsimp [read, push_back],
102+
rw [dif_neg], refl,
103+
exact ne_of_lt h'
104+
end
105+
106+
@[simp] theorem push_back_rev_list (a : array α n) (v : α) :
107+
(a.push_back v).rev_list = v :: a.rev_list :=
108+
begin
109+
unfold push_back rev_list foldl iterate, dsimp [iterate_aux, read, push_back],
110+
rw [dif_pos (eq.refl n)], apply congr_arg,
111+
apply push_back_rev_list_core
112+
end
113+
114+
@[simp] theorem push_back_to_list (a : array α n) (v : α) :
115+
(a.push_back v).to_list = a.to_list ++ [v] :=
116+
by rw [← rev_list_reverse, ← rev_list_reverse, push_back_rev_list,
117+
list.reverse_cons, list.concat_eq_append]
118+
119+
def read_foreach_aux (f : fin n → α → α) (ai : array α n) :
120+
∀ i h (a : array α n) (j : fin n), j.1 < i →
121+
(iterate_aux ai (λ i v a', write a' i (f i v)) i h a).read j = f j (ai.read j)
122+
| 0 hi a ⟨j, hj⟩ ji := absurd ji (nat.not_lt_zero _)
123+
| (i+1) hi a ⟨j, hj⟩ ji := begin
124+
dsimp [iterate_aux], dsimp at ji,
125+
change ite _ _ _ = _,
126+
by_cases (⟨i, hi⟩ : fin _) = ⟨j, hj⟩ with e; simp [e],
127+
rw [read_foreach_aux _ _ _ ⟨j, hj⟩],
128+
exact (lt_or_eq_of_le (nat.le_of_lt_succ ji)).resolve_right
129+
(ne.symm $ mt (@fin.eq_of_veq _ ⟨i, hi⟩ ⟨j, hj⟩) e)
130+
end
131+
132+
def read_foreach (a : array α n) (f : fin n → α → α) (i : fin n) :
133+
(foreach a f).read i = f i (a.read i) :=
134+
read_foreach_aux _ _ _ _ _ _ i.2
135+
136+
def read_map (f : α → α) (a : array α n) (i : fin n) :
137+
(map f a).read i = f (a.read i) :=
138+
read_foreach _ _ _
139+
140+
def read_map₂ (f : α → α → α) (a b : array α n) (i : fin n) :
141+
(map₂ f a b).read i = f (a.read i) (b.read i) :=
142+
read_foreach _ _ _
143+
144+
instance [decidable_eq α] : decidable_eq (array α n) := λ a b,
145+
suffices to_list a = to_list b → a = b, from
146+
decidable_of_decidable_of_iff (by apply_instance) ⟨this, congr_arg to_list⟩,
147+
λ h, eq_of_heq $ a.to_list_to_array.symm.trans $
148+
match to_list a, h with ._, rfl := b.to_list_to_array end
149+
150+
end array
151+
152+
instance (α) [decidable_eq α] : decidable_eq (buffer α) :=
153+
by tactic.mk_dec_eq_instance

data/bitvec.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Basic operations on bitvectors.
77
88
This is a work-in-progress, and contains additions to other theories.
99
-/
10-
import data.vector
10+
import data.nat.basic data.vector
1111

1212
@[reducible] def bitvec (n : ℕ) := vector bool n
1313

@@ -45,7 +45,7 @@ section shift
4545
{ have h₁ := sub_le n i,
4646
rw [min_eq_right h], rw [min_eq_left h₁, ← nat.add_sub_assoc h, add_comm, nat.add_sub_cancel] },
4747
{ have h₁ := le_of_not_ge h,
48-
rw [min_eq_left h₁, sub_eq_zero_of_le h₁, min_zero_left, add_zero] }
48+
rw [min_eq_left h₁, sub_eq_zero_of_le h₁, zero_min, add_zero] }
4949
end $
5050
repeat fill (min n i) ++ₜ take (n-i) x
5151

data/fin.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
open fin nat
1+
import data.nat.basic
22

3-
theorem lt_succ_of_lt {a b : nat} (h : a < b) : a < b + 1 := lt_add_of_lt_of_pos h one_pos
3+
open fin nat
44

55
def raise_fin {n : ℕ} (k : fin n) : fin (n + 1) := ⟨val k, lt_succ_of_lt (is_lt k)⟩
66

data/hash_map.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2017 Microsoft Corporation. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Leonardo de Moura, Mario Carneiro
55
-/
6-
import data.list.set data.list.comb init.data.array data.pnat
6+
import data.list.set data.list.comb data.pnat data.array.lemmas
77

88
universes u v w
99

0 commit comments

Comments
 (0)