2014-12-23 22:34:16 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2014 Parikshit Khanna. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
|
|
Module: data.list.basic
|
|
|
|
|
Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura
|
|
|
|
|
|
|
|
|
|
Basic properties of lists.
|
|
|
|
|
-/
|
|
|
|
|
|
2014-12-01 04:34:12 +00:00
|
|
|
|
import logic tools.helper_tactics data.nat.basic
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-10-05 20:09:56 +00:00
|
|
|
|
open eq.ops helper_tactics nat
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
|
|
|
|
inductive list (T : Type) : Type :=
|
2014-08-22 22:46:10 +00:00
|
|
|
|
nil {} : list T,
|
|
|
|
|
cons : T → list T → list T
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-09-04 22:03:59 +00:00
|
|
|
|
namespace list
|
2014-10-21 21:08:07 +00:00
|
|
|
|
notation h :: t := cons h t
|
2014-10-19 18:13:36 +00:00
|
|
|
|
notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-10-09 14:13:06 +00:00
|
|
|
|
variable {T : Type}
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- append -/
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition append : list T → list T → list T,
|
|
|
|
|
append nil l := l,
|
|
|
|
|
append (h :: s) t := h :: (append s t)
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-10-21 21:08:07 +00:00
|
|
|
|
notation l₁ ++ l₂ := append l₁ l₂
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem append_nil_left (t : list T) : nil ++ t = t
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem append_cons (x : T) (s t : list T) : (x::s) ++ t = x::(s ++ t)
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem append_nil_right : ∀ (t : list T), t ++ nil = t,
|
|
|
|
|
append_nil_right nil := rfl,
|
|
|
|
|
append_nil_right (a :: l) := calc
|
|
|
|
|
(a :: l) ++ nil = a :: (l ++ nil) : rfl
|
|
|
|
|
... = a :: l : append_nil_right l
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-09 02:47:44 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem append.assoc : ∀ (s t u : list T), s ++ t ++ u = s ++ (t ++ u),
|
|
|
|
|
append.assoc nil t u := rfl,
|
|
|
|
|
append.assoc (a :: l) t u := calc
|
|
|
|
|
(a :: l) ++ t ++ u = a :: (l ++ t ++ u) : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = a :: (l ++ (t ++ u)) : append.assoc
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = (a :: l) ++ (t ++ u) : rfl
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- length -/
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition length : list T → nat,
|
|
|
|
|
length nil := 0,
|
|
|
|
|
length (a :: l) := length l + 1
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem length_nil : length (@nil T) = 0
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem length_cons (x : T) (t : list T) : length (x::t) = length t + 1
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem length_append : ∀ (s t : list T), length (s ++ t) = length s + length t,
|
|
|
|
|
length_append nil t := calc
|
|
|
|
|
length (nil ++ t) = length t : rfl
|
|
|
|
|
... = length nil + length t : zero_add,
|
|
|
|
|
length_append (a :: s) t := calc
|
|
|
|
|
length (a :: s ++ t) = length (s ++ t) + 1 : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = length s + length t + 1 : length_append
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = (length s + 1) + length t : add.succ_left
|
|
|
|
|
... = length (a :: s) + length t : rfl
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-07-31 20:33:35 +00:00
|
|
|
|
-- add_rewrite length_nil length_cons
|
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- concat -/
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition concat : Π (x : T), list T → list T,
|
|
|
|
|
concat a nil := [a],
|
|
|
|
|
concat a (b :: l) := b :: concat a l
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem concat_nil (x : T) : concat x nil = [x]
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem concat_cons (x y : T) (l : list T) : concat x (y::l) = y::(concat x l)
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem concat_eq_append (a : T) : ∀ (l : list T), concat a l = l ++ [a],
|
|
|
|
|
concat_eq_append nil := rfl,
|
|
|
|
|
concat_eq_append (b :: l) := calc
|
|
|
|
|
concat a (b :: l) = b :: (concat a l) : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = b :: (l ++ [a]) : concat_eq_append
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = (b :: l) ++ [a] : rfl
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
|
|
|
|
-- add_rewrite append_nil append_cons
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- reverse -/
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition reverse : list T → list T,
|
|
|
|
|
reverse nil := nil,
|
|
|
|
|
reverse (a :: l) := concat a (reverse l)
|
|
|
|
|
|
|
|
|
|
theorem reverse_nil : reverse (@nil T) = nil
|
|
|
|
|
|
|
|
|
|
theorem reverse_cons (x : T) (l : list T) : reverse (x::l) = concat x (reverse l)
|
|
|
|
|
|
|
|
|
|
theorem reverse_singleton (x : T) : reverse [x] = [x]
|
|
|
|
|
|
|
|
|
|
theorem reverse_append : ∀ (s t : list T), reverse (s ++ t) = (reverse t) ++ (reverse s),
|
|
|
|
|
reverse_append nil t2 := calc
|
|
|
|
|
reverse (nil ++ t2) = reverse t2 : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = (reverse t2) ++ nil : append_nil_right
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = (reverse t2) ++ (reverse nil) : {reverse_nil⁻¹},
|
|
|
|
|
reverse_append (a2 :: s2) t2 := calc
|
|
|
|
|
reverse ((a2 :: s2) ++ t2) = concat a2 (reverse (s2 ++ t2)) : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = concat a2 (reverse t2 ++ reverse s2) : reverse_append
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = (reverse t2 ++ reverse s2) ++ [a2] : concat_eq_append
|
|
|
|
|
... = reverse t2 ++ (reverse s2 ++ [a2]) : append.assoc
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = reverse t2 ++ concat a2 (reverse s2) : concat_eq_append
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = reverse t2 ++ reverse (a2 :: s2) : rfl
|
|
|
|
|
|
|
|
|
|
theorem reverse_reverse : ∀ (l : list T), reverse (reverse l) = l,
|
|
|
|
|
reverse_reverse nil := rfl,
|
|
|
|
|
reverse_reverse (a :: l) := calc
|
|
|
|
|
reverse (reverse (a :: l)) = reverse (concat a (reverse l)) : rfl
|
2015-01-09 02:47:44 +00:00
|
|
|
|
... = reverse (reverse l ++ [a]) : concat_eq_append
|
|
|
|
|
... = reverse [a] ++ reverse (reverse l) : reverse_append
|
|
|
|
|
... = reverse [a] ++ l : reverse_reverse
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = a :: l : rfl
|
|
|
|
|
|
|
|
|
|
theorem concat_eq_reverse_cons (x : T) (l : list T) : concat x l = reverse (x :: reverse l) :=
|
|
|
|
|
calc
|
2015-01-09 02:47:44 +00:00
|
|
|
|
concat x l = concat x (reverse (reverse l)) : reverse_reverse
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = reverse (x :: reverse l) : rfl
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- head and tail -/
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition head [h : inhabited T] : list T → T,
|
|
|
|
|
head nil := arbitrary T,
|
|
|
|
|
head (a :: l) := a
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem head_cons [h : inhabited T] (a : T) (l : list T) : head (a::l) = a
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem head_concat [h : inhabited T] {s : list T} (t : list T) : s ≠ nil → head (s ++ t) = head s :=
|
2014-09-03 22:13:03 +00:00
|
|
|
|
cases_on s
|
2014-09-02 02:44:04 +00:00
|
|
|
|
(take H : nil ≠ nil, absurd rfl H)
|
2015-01-09 02:47:44 +00:00
|
|
|
|
(take (x : T) (s : list T), take H : x::s ≠ nil,
|
2014-07-30 00:04:25 +00:00
|
|
|
|
calc
|
2015-01-09 02:47:44 +00:00
|
|
|
|
head ((x::s) ++ t) = head (x::(s ++ t)) : rfl
|
|
|
|
|
... = x : head_cons
|
|
|
|
|
... = head (x::s) : rfl)
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition tail : list T → list T,
|
|
|
|
|
tail nil := nil,
|
|
|
|
|
tail (a :: l) := l
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem tail_nil : tail (@nil T) = nil
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem tail_cons (a : T) (l : list T) : tail (a::l) = l
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem cons_head_tail [h : inhabited T] {l : list T} : l ≠ nil → (head l)::(tail l) = l :=
|
2014-09-03 22:13:03 +00:00
|
|
|
|
cases_on l
|
2014-09-02 02:44:04 +00:00
|
|
|
|
(assume H : nil ≠ nil, absurd rfl H)
|
2014-09-10 23:42:27 +00:00
|
|
|
|
(take x l, assume H : x::l ≠ nil, rfl)
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- list membership -/
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition mem : T → list T → Prop,
|
|
|
|
|
mem a nil := false,
|
|
|
|
|
mem a (b :: l) := a = b ∨ mem a l
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-10-21 21:08:07 +00:00
|
|
|
|
notation e ∈ s := mem e s
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_nil (x : T) : x ∈ nil ↔ false :=
|
2014-09-10 14:55:32 +00:00
|
|
|
|
iff.rfl
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_cons (x y : T) (l : list T) : x ∈ y::l ↔ (x = y ∨ x ∈ l) :=
|
2014-09-10 14:55:32 +00:00
|
|
|
|
iff.rfl
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_concat_imp_or {x : T} {s t : list T} : x ∈ s ++ t → x ∈ s ∨ x ∈ t :=
|
2014-09-05 04:25:21 +00:00
|
|
|
|
induction_on s or.inr
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(take y s,
|
|
|
|
|
assume IH : x ∈ s ++ t → x ∈ s ∨ x ∈ t,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
assume H1 : x ∈ y::s ++ t,
|
2014-07-31 20:33:35 +00:00
|
|
|
|
have H2 : x = y ∨ x ∈ s ++ t, from H1,
|
2014-12-15 20:05:44 +00:00
|
|
|
|
have H3 : x = y ∨ x ∈ s ∨ x ∈ t, from or_of_or_of_imp_right H2 IH,
|
2014-09-05 04:25:21 +00:00
|
|
|
|
iff.elim_right or.assoc H3)
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_or_imp_concat {x : T} {s t : list T} : x ∈ s ∨ x ∈ t → x ∈ s ++ t :=
|
2014-09-03 22:13:03 +00:00
|
|
|
|
induction_on s
|
2014-12-15 20:05:44 +00:00
|
|
|
|
(take H, or.elim H false.elim (assume H, H))
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(take y s,
|
|
|
|
|
assume IH : x ∈ s ∨ x ∈ t → x ∈ s ++ t,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
assume H : x ∈ y::s ∨ x ∈ t,
|
2014-09-05 04:25:21 +00:00
|
|
|
|
or.elim H
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(assume H1,
|
2014-09-05 04:25:21 +00:00
|
|
|
|
or.elim H1
|
|
|
|
|
(take H2 : x = y, or.inl H2)
|
|
|
|
|
(take H2 : x ∈ s, or.inr (IH (or.inl H2))))
|
|
|
|
|
(assume H1 : x ∈ t, or.inr (IH (or.inr H1))))
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_concat (x : T) (s t : list T) : x ∈ s ++ t ↔ x ∈ s ∨ x ∈ t :=
|
|
|
|
|
iff.intro mem_concat_imp_or mem_or_imp_concat
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-25 04:23:21 +00:00
|
|
|
|
attribute mem [reducible]
|
|
|
|
|
attribute append [reducible]
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem mem_split {x : T} {l : list T} : x ∈ l → ∃s t : list T, l = s ++ (x::t) :=
|
2014-09-03 22:13:03 +00:00
|
|
|
|
induction_on l
|
2015-01-07 21:38:11 +00:00
|
|
|
|
(take H : x ∈ nil, false.elim (iff.elim_left !mem_nil H))
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(take y l,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
assume IH : x ∈ l → ∃s t : list T, l = s ++ (x::t),
|
|
|
|
|
assume H : x ∈ y::l,
|
2014-09-05 04:25:21 +00:00
|
|
|
|
or.elim H
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(assume H1 : x = y,
|
2014-12-16 03:05:03 +00:00
|
|
|
|
exists.intro nil (!exists.intro (H1 ▸ rfl)))
|
2014-07-31 20:33:35 +00:00
|
|
|
|
(assume H1 : x ∈ l,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
obtain s (H2 : ∃t : list T, l = s ++ (x::t)), from IH H1,
|
|
|
|
|
obtain t (H3 : l = s ++ (x::t)), from H2,
|
|
|
|
|
have H4 : y :: l = (y::s) ++ (x::t),
|
2014-09-05 01:41:06 +00:00
|
|
|
|
from H3 ▸ rfl,
|
2014-12-16 03:05:03 +00:00
|
|
|
|
!exists.intro (!exists.intro H4)))
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2014-10-12 20:06:00 +00:00
|
|
|
|
definition mem.is_decidable [instance] (H : decidable_eq T) (x : T) (l : list T) : decidable (x ∈ l) :=
|
2014-09-08 04:06:32 +00:00
|
|
|
|
rec_on l
|
2015-01-07 21:38:11 +00:00
|
|
|
|
(decidable.inr (not_of_iff_false !mem_nil))
|
2014-10-05 20:09:56 +00:00
|
|
|
|
(take (h : T) (l : list T) (iH : decidable (x ∈ l)),
|
2014-09-10 23:42:27 +00:00
|
|
|
|
show decidable (x ∈ h::l), from
|
2014-09-08 04:06:32 +00:00
|
|
|
|
decidable.rec_on iH
|
2014-09-10 23:42:27 +00:00
|
|
|
|
(assume Hp : x ∈ l,
|
2014-09-08 04:06:32 +00:00
|
|
|
|
decidable.rec_on (H x h)
|
|
|
|
|
(assume Heq : x = h,
|
|
|
|
|
decidable.inl (or.inl Heq))
|
|
|
|
|
(assume Hne : x ≠ h,
|
|
|
|
|
decidable.inl (or.inr Hp)))
|
2014-09-10 23:42:27 +00:00
|
|
|
|
(assume Hn : ¬x ∈ l,
|
2014-09-08 04:06:32 +00:00
|
|
|
|
decidable.rec_on (H x h)
|
|
|
|
|
(assume Heq : x = h,
|
|
|
|
|
decidable.inl (or.inl Heq))
|
|
|
|
|
(assume Hne : x ≠ h,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
have H1 : ¬(x = h ∨ x ∈ l), from
|
|
|
|
|
assume H2 : x = h ∨ x ∈ l, or.elim H2
|
2014-09-08 04:06:32 +00:00
|
|
|
|
(assume Heq, absurd Heq Hne)
|
|
|
|
|
(assume Hp, absurd Hp Hn),
|
2014-09-10 23:42:27 +00:00
|
|
|
|
have H2 : ¬x ∈ h::l, from
|
2015-01-07 21:38:11 +00:00
|
|
|
|
iff.elim_right (not_iff_not_of_iff !mem_cons) H1,
|
2014-09-08 04:06:32 +00:00
|
|
|
|
decidable.inr H2)))
|
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- find -/
|
|
|
|
|
|
2014-10-05 20:09:56 +00:00
|
|
|
|
section
|
2014-10-12 20:06:00 +00:00
|
|
|
|
variable [H : decidable_eq T]
|
2014-10-05 20:09:56 +00:00
|
|
|
|
include H
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition find : T → list T → nat,
|
|
|
|
|
find a nil := 0,
|
|
|
|
|
find a (b :: l) := if a = b then 0 else succ (find a l)
|
2014-09-08 04:06:32 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem find_nil (x : T) : find x nil = 0
|
2014-09-08 04:06:32 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem find_cons (x y : T) (l : list T) : find x (y::l) = if x = y then 0 else succ (find x l)
|
2014-09-08 04:06:32 +00:00
|
|
|
|
|
2014-10-05 20:09:56 +00:00
|
|
|
|
theorem find.not_mem {l : list T} {x : T} : ¬x ∈ l → find x l = length l :=
|
2014-09-08 04:06:32 +00:00
|
|
|
|
rec_on l
|
2015-01-07 21:38:11 +00:00
|
|
|
|
(assume P₁ : ¬x ∈ nil, _)
|
2014-09-08 04:06:32 +00:00
|
|
|
|
(take y l,
|
2014-09-10 23:42:27 +00:00
|
|
|
|
assume iH : ¬x ∈ l → find x l = length l,
|
|
|
|
|
assume P₁ : ¬x ∈ y::l,
|
2015-01-07 21:38:11 +00:00
|
|
|
|
have P₂ : ¬(x = y ∨ x ∈ l), from iff.elim_right (not_iff_not_of_iff !mem_cons) P₁,
|
2014-12-15 20:05:44 +00:00
|
|
|
|
have P₃ : ¬x = y ∧ ¬x ∈ l, from (iff.elim_left not_or_iff_not_and_not P₂),
|
2014-09-08 04:06:32 +00:00
|
|
|
|
calc
|
2015-01-07 21:38:11 +00:00
|
|
|
|
find x (y::l) = if x = y then 0 else succ (find x l) : !find_cons
|
2014-09-10 23:42:27 +00:00
|
|
|
|
... = succ (find x l) : if_neg (and.elim_left P₃)
|
|
|
|
|
... = succ (length l) : {iH (and.elim_right P₃)}
|
2015-01-07 21:38:11 +00:00
|
|
|
|
... = length (y::l) : !length_cons⁻¹)
|
2014-10-05 20:09:56 +00:00
|
|
|
|
end
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2014-12-23 22:34:16 +00:00
|
|
|
|
/- nth element -/
|
2014-07-31 20:33:35 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
definition nth [h : inhabited T] : list T → nat → T,
|
|
|
|
|
nth nil n := arbitrary T,
|
|
|
|
|
nth (a :: l) 0 := a,
|
|
|
|
|
nth (a :: l) (n+1) := nth l n
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem nth_zero [h : inhabited T] (a : T) (l : list T) : nth (a :: l) 0 = a
|
2014-07-30 00:04:25 +00:00
|
|
|
|
|
2015-01-07 21:38:11 +00:00
|
|
|
|
theorem nth_succ [h : inhabited T] (a : T) (l : list T) (n : nat) : nth (a::l) (n+1) = nth l n
|
2014-10-10 23:33:58 +00:00
|
|
|
|
|
2014-08-05 00:07:59 +00:00
|
|
|
|
end list
|