refactor(library/data/list/basic): cleanup
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
1e5ba9bd75
commit
746f5bff0d
1 changed files with 61 additions and 101 deletions
|
@ -23,9 +23,6 @@ nil {} : list T,
|
|||
cons : T → list T → list T
|
||||
|
||||
namespace list
|
||||
|
||||
-- Type
|
||||
-- ----
|
||||
infix `::` := cons
|
||||
|
||||
section
|
||||
|
@ -57,26 +54,18 @@ infixl `++` : 65 := append
|
|||
|
||||
theorem nil_append {t : list T} : nil ++ t = t
|
||||
|
||||
theorem cons_append {x : T} {s t : list T} : (x :: s) ++ t = x :: (s ++ t)
|
||||
theorem cons_append {x : T} {s t : list T} : x::s ++ t = x::(s ++ t)
|
||||
|
||||
theorem append_nil {t : list T} : t ++ nil = t :=
|
||||
induction_on t rfl
|
||||
(take (x : T) (l : list T) (H : append l nil = l),
|
||||
H ▸ rfl)
|
||||
induction_on t rfl (λx l H, H ▸ rfl)
|
||||
|
||||
theorem append_assoc {s t u : list T} : s ++ t ++ u = s ++ (t ++ u) :=
|
||||
induction_on s
|
||||
rfl
|
||||
(take x l, assume H : (l ++ t) ++ u = l ++ (t ++ u),
|
||||
calc
|
||||
(x :: l) ++ t ++ u = x :: (l ++ t ++ u) : rfl
|
||||
... = x :: (l ++ (t ++ u)) : {H}
|
||||
... = (x :: l) ++ (t ++ u) : rfl)
|
||||
induction_on s rfl (λx l H, H ▸ rfl)
|
||||
|
||||
-- Length
|
||||
-- ------
|
||||
|
||||
definition length : list T → ℕ :=
|
||||
definition length : list T → nat :=
|
||||
rec 0 (λx l m, succ m)
|
||||
|
||||
theorem length_nil : length (@nil T) = 0
|
||||
|
@ -84,18 +73,7 @@ theorem length_nil : length (@nil T) = 0
|
|||
theorem length_cons {x : T} {t : list T} : length (x::t) = succ (length t)
|
||||
|
||||
theorem length_append {s t : list T} : length (s ++ t) = length s + length t :=
|
||||
induction_on s
|
||||
(calc
|
||||
length (nil ++ t) = length t : rfl
|
||||
... = 0 + length t : {add_zero_left⁻¹}
|
||||
... = length nil + length t : rfl)
|
||||
(take x s,
|
||||
assume H : length (s ++ t) = length s + length t,
|
||||
calc
|
||||
length ((x :: s) ++ t ) = succ (length (s ++ t)) : rfl
|
||||
... = succ (length s + length t) : {H}
|
||||
... = succ (length s) + length t : {add_succ_left⁻¹}
|
||||
... = length (x :: s) + length t : rfl)
|
||||
induction_on s (add_zero_left⁻¹) (λx s H, add_succ_left⁻¹ ▸ H ▸ rfl)
|
||||
|
||||
-- add_rewrite length_nil length_cons
|
||||
|
||||
|
@ -126,37 +104,19 @@ theorem reverse_cons {x : T} {l : list T} : reverse (x :: l) = concat x (reverse
|
|||
theorem reverse_singleton {x : T} : reverse [x] = [x]
|
||||
|
||||
theorem reverse_append {s t : list T} : reverse (s ++ t) = (reverse t) ++ (reverse s) :=
|
||||
induction_on s
|
||||
(append_nil⁻¹)
|
||||
(take x s, assume IH : reverse (s ++ t) = (reverse t) ++ (reverse s),
|
||||
calc
|
||||
reverse ((x :: s) ++ t) = reverse (s ++ t) ++ [x] : rfl
|
||||
... = reverse t ++ reverse s ++ [x] : {IH}
|
||||
... = reverse t ++ (reverse s ++ [x]) : append_assoc
|
||||
... = reverse t ++ (reverse (x :: s)) : rfl)
|
||||
induction_on s (append_nil⁻¹)
|
||||
(λx s H, calc
|
||||
reverse (x::s ++ t) = reverse t ++ reverse s ++ [x] : {H}
|
||||
... = reverse t ++ (reverse s ++ [x]) : append_assoc)
|
||||
|
||||
theorem reverse_reverse {l : list T} : reverse (reverse l) = l :=
|
||||
induction_on l
|
||||
rfl
|
||||
(take x l',
|
||||
assume H: reverse (reverse l') = l',
|
||||
show reverse (reverse (x :: l')) = x :: l', from
|
||||
calc
|
||||
reverse (reverse (x :: l')) = reverse (reverse l' ++ [x]) : rfl
|
||||
... = reverse [x] ++ reverse (reverse l') : reverse_append
|
||||
... = [x] ++ l' : {H}
|
||||
... = x :: l' : rfl)
|
||||
induction_on l rfl (λx l' H, H ▸ reverse_append)
|
||||
|
||||
theorem concat_eq_reverse_cons {x : T} {l : list T} : concat x l = reverse (x :: reverse l) :=
|
||||
induction_on l
|
||||
rfl
|
||||
(take y l',
|
||||
assume H : concat x l' = reverse (x :: reverse l'),
|
||||
calc
|
||||
induction_on l rfl
|
||||
(λy l' H, calc
|
||||
concat x (y::l') = (y::l') ++ [x] : concat_eq_append
|
||||
... = reverse (reverse (y :: l')) ++ [x] : {reverse_reverse⁻¹}
|
||||
... = reverse (x :: (reverse (y :: l'))) : rfl)
|
||||
|
||||
... = reverse (reverse (y::l')) ++ [x] : {reverse_reverse⁻¹})
|
||||
|
||||
-- Head and tail
|
||||
-- -------------
|
||||
|
@ -173,7 +133,7 @@ cases_on s
|
|||
(take H : nil ≠ nil, absurd rfl H)
|
||||
(take x s, take H : x::s ≠ nil,
|
||||
calc
|
||||
head x ((x :: s) ++ t) = head x (x :: (s ++ t)) : {cons_append}
|
||||
head x (x::s ++ t) = head x (x::(s ++ t)) : {cons_append}
|
||||
... = x : {head_cons}
|
||||
... = head x (x::s) : {head_cons⁻¹})
|
||||
|
||||
|
@ -200,14 +160,14 @@ infix `∈` := mem
|
|||
theorem mem_nil {x : T} : x ∈ nil ↔ false :=
|
||||
iff.rfl
|
||||
|
||||
theorem mem_cons {x y : T} {l : list T} : mem x (y :: l) ↔ (x = y ∨ mem x l) :=
|
||||
theorem mem_cons {x y : T} {l : list T} : x ∈ y::l ↔ (x = y ∨ x ∈ l) :=
|
||||
iff.rfl
|
||||
|
||||
theorem mem_concat_imp_or {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t :=
|
||||
induction_on s or.inr
|
||||
(take y s,
|
||||
assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t,
|
||||
assume H1 : x ∈ (y :: s) ++ t,
|
||||
assume H1 : x ∈ y::s ++ t,
|
||||
have H2 : x = y ∨ x ∈ s ++ t, from H1,
|
||||
have H3 : x = y ∨ x ∈ s ∨ x ∈ t, from or.imp_or_right H2 IH,
|
||||
iff.elim_right or.assoc H3)
|
||||
|
@ -244,28 +204,28 @@ induction_on l
|
|||
from H3 ▸ rfl,
|
||||
exists_intro _ (exists_intro _ H4)))
|
||||
|
||||
theorem mem_is_decidable [instance] {H : decidable_eq T} {x : T} {l : list T} : decidable (mem x l) :=
|
||||
theorem mem_is_decidable [instance] {H : decidable_eq T} {x : T} {l : list T} : decidable (x ∈ l) :=
|
||||
rec_on l
|
||||
(decidable.inr (iff.false_elim mem_nil))
|
||||
(λ (h : T) (l : list T) (iH : decidable (mem x l)),
|
||||
show decidable (mem x (h :: l)), from
|
||||
(λ (h : T) (l : list T) (iH : decidable (x ∈ l)),
|
||||
show decidable (x ∈ h::l), from
|
||||
decidable.rec_on iH
|
||||
(assume Hp : mem x l,
|
||||
(assume Hp : x ∈ l,
|
||||
decidable.rec_on (H x h)
|
||||
(assume Heq : x = h,
|
||||
decidable.inl (or.inl Heq))
|
||||
(assume Hne : x ≠ h,
|
||||
decidable.inl (or.inr Hp)))
|
||||
(assume Hn : ¬mem x l,
|
||||
(assume Hn : ¬x ∈ l,
|
||||
decidable.rec_on (H x h)
|
||||
(assume Heq : x = h,
|
||||
decidable.inl (or.inl Heq))
|
||||
(assume Hne : x ≠ h,
|
||||
have H1 : ¬(x = h ∨ mem x l), from
|
||||
assume H2 : x = h ∨ mem x l, or.elim H2
|
||||
have H1 : ¬(x = h ∨ x ∈ l), from
|
||||
assume H2 : x = h ∨ x ∈ l, or.elim H2
|
||||
(assume Heq, absurd Heq Hne)
|
||||
(assume Hp, absurd Hp Hn),
|
||||
have H2 : ¬mem x (h :: l), from
|
||||
have H2 : ¬x ∈ h::l, from
|
||||
iff.elim_right (iff.flip_sign mem_cons) H1,
|
||||
decidable.inr H2)))
|
||||
|
||||
|
@ -281,14 +241,14 @@ theorem find_cons {H : decidable_eq T} {x y : T} {l : list T} :
|
|||
find x (y::l) = if x = y then 0 else succ (find x l)
|
||||
|
||||
theorem not_mem_find {H : decidable_eq T} {l : list T} {x : T} :
|
||||
¬mem x l → find x l = length l :=
|
||||
¬x ∈ l → find x l = length l :=
|
||||
rec_on l
|
||||
(assume P₁ : ¬mem x nil, rfl)
|
||||
(assume P₁ : ¬x ∈ nil, rfl)
|
||||
(take y l,
|
||||
assume iH : ¬mem x l → find x l = length l,
|
||||
assume P₁ : ¬mem x (y :: l),
|
||||
have P₂ : ¬(x = y ∨ mem x l), from iff.elim_right (iff.flip_sign mem_cons) P₁,
|
||||
have P₃ : ¬x = y ∧ ¬mem x l, from (iff.elim_left not_or P₂),
|
||||
assume iH : ¬x ∈ l → find x l = length l,
|
||||
assume P₁ : ¬x ∈ y::l,
|
||||
have P₂ : ¬(x = y ∨ x ∈ l), from iff.elim_right (iff.flip_sign mem_cons) P₁,
|
||||
have P₃ : ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or P₂),
|
||||
calc
|
||||
find x (y::l) = if x = y then 0 else succ (find x l) : find_cons
|
||||
... = succ (find x l) : if_neg (and.elim_left P₃)
|
||||
|
@ -298,11 +258,11 @@ rec_on l
|
|||
-- nth element
|
||||
-- -----------
|
||||
|
||||
definition nth (x : T) (l : list T) (n : ℕ) : T :=
|
||||
definition nth (x : T) (l : list T) (n : nat) : T :=
|
||||
nat.rec (λl, head x l) (λm f l, f (tail l)) n l
|
||||
|
||||
theorem nth_zero {x : T} {l : list T} : nth x l 0 = head x l
|
||||
|
||||
theorem nth_succ {x : T} {l : list T} {n : ℕ} : nth x l (succ n) = nth x (tail l) n
|
||||
theorem nth_succ {x : T} {l : list T} {n : nat} : nth x l (succ n) = nth x (tail l) n
|
||||
end
|
||||
end list
|
||||
|
|
Loading…
Reference in a new issue