2014-12-22 20:33:29 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2014 Floris van Doorn. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
|
|
Module: data.vector
|
|
|
|
|
Author: Floris van Doorn, Leonardo de Moura
|
|
|
|
|
-/
|
2015-01-08 19:17:28 +00:00
|
|
|
|
import data.nat.basic
|
|
|
|
|
open nat prod
|
2014-12-22 20:33:29 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
inductive vector (A : Type) : nat → Type :=
|
2015-02-26 01:00:10 +00:00
|
|
|
|
| nil {} : vector A zero
|
|
|
|
|
| cons : Π {n}, A → vector A n → vector A (succ n)
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2014-09-19 23:15:04 +00:00
|
|
|
|
namespace vector
|
2014-10-21 21:08:07 +00:00
|
|
|
|
notation a :: b := cons a b
|
2014-09-05 16:45:01 +00:00
|
|
|
|
notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l
|
|
|
|
|
|
2014-11-16 06:36:52 +00:00
|
|
|
|
variables {A B C : Type}
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
protected definition is_inhabited [instance] [h : inhabited A] : ∀ (n : nat), inhabited (vector A n)
|
|
|
|
|
| is_inhabited 0 := inhabited.mk nil
|
|
|
|
|
| is_inhabited (n+1) := inhabited.mk (inhabited.value h :: inhabited.value (is_inhabited n))
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem vector0_eq_nil : ∀ (v : vector A 0), v = nil
|
|
|
|
|
| vector0_eq_nil nil := rfl
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition head : Π {n : nat}, vector A (succ n) → A
|
|
|
|
|
| head (a::v) := a
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition tail : Π {n : nat}, vector A (succ n) → vector A n
|
|
|
|
|
| tail (a::v) := v
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem head_cons {n : nat} (h : A) (t : vector A n) : head (h :: t) = h :=
|
2014-11-16 04:21:18 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem tail_cons {n : nat} (h : A) (t : vector A n) : tail (h :: t) = t :=
|
2014-11-16 04:21:18 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem eta : ∀ {n : nat} (v : vector A (succ n)), head v :: tail v = v
|
|
|
|
|
| eta (a::v) := rfl
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition last : Π {n : nat}, vector A (succ n) → A
|
|
|
|
|
| last (a::nil) := a
|
|
|
|
|
| last (a::v) := last v
|
2014-11-16 04:21:18 +00:00
|
|
|
|
|
2014-11-16 06:36:52 +00:00
|
|
|
|
theorem last_singleton (a : A) : last (a :: nil) = a :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem last_cons {n : nat} (a : A) (v : vector A (succ n)) : last (a :: v) = last v :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition const : Π (n : nat), A → vector A n
|
|
|
|
|
| const 0 a := nil
|
|
|
|
|
| const (succ n) a := a :: const n a
|
2014-11-16 04:21:18 +00:00
|
|
|
|
|
2014-11-16 06:36:52 +00:00
|
|
|
|
theorem head_const (n : nat) (a : A) : head (const (succ n) a) = a :=
|
2014-11-16 04:21:18 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem last_const : ∀ (n : nat) (a : A), last (const (succ n) a) = a
|
|
|
|
|
| last_const 0 a := rfl
|
|
|
|
|
| last_const (succ n) a := last_const n a
|
2014-11-16 04:21:18 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition map (f : A → B) : Π {n : nat}, vector A n → vector B n
|
|
|
|
|
| map nil := nil
|
|
|
|
|
| map (a::v) := f a :: map v
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem map_nil (f : A → B) : map f nil = nil :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem map_cons {n : nat} (f : A → B) (h : A) (t : vector A n) : map f (h :: t) = f h :: map f t :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition map2 (f : A → B → C) : Π {n : nat}, vector A n → vector B n → vector C n
|
|
|
|
|
| map2 nil nil := nil
|
|
|
|
|
| map2 (a::va) (b::vb) := f a b :: map2 va vb
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem map2_nil (f : A → B → C) : map2 f nil nil = nil :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem map2_cons {n : nat} (f : A → B → C) (h₁ : A) (h₂ : B) (t₁ : vector A n) (t₂ : vector B n) :
|
|
|
|
|
map2 f (h₁ :: t₁) (h₂ :: t₂) = f h₁ h₂ :: map2 f t₁ t₂ :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
-- Remark: why do we need to provide indices?
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition append : Π {n m : nat}, vector A n → vector A m → vector A (n ⊕ m)
|
2015-03-07 01:37:03 +00:00
|
|
|
|
| 0 m nil w := w
|
|
|
|
|
| (succ n) m (a::v) w := a :: (append v w)
|
2014-11-12 06:33:47 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem append_nil {n : nat} (v : vector A n) : append nil v = v :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem append_cons {n m : nat} (h : A) (t : vector A n) (v : vector A m) :
|
|
|
|
|
append (h::t) v = h :: (append t v) :=
|
2014-11-12 06:33:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition unzip : Π {n : nat}, vector (A × B) n → vector A n × vector B n
|
|
|
|
|
| unzip nil := (nil, nil)
|
|
|
|
|
| unzip ((a, b) :: v) := (a :: pr₁ (unzip v), b :: pr₂ (unzip v))
|
2014-11-16 06:19:23 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem unzip_nil : unzip (@nil (A × B)) = (nil, nil) :=
|
|
|
|
|
rfl
|
2014-11-16 06:19:23 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem unzip_cons {n : nat} (a : A) (b : B) (v : vector (A × B) n) :
|
|
|
|
|
unzip ((a, b) :: v) = (a :: pr₁ (unzip v), b :: pr₂ (unzip v)) :=
|
|
|
|
|
rfl
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition zip : Π {n : nat}, vector A n → vector B n → vector (A × B) n
|
|
|
|
|
| zip nil nil := nil
|
|
|
|
|
| zip (a::va) (b::vb) := ((a, b) :: zip va vb)
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem zip_nil_nil : zip (@nil A) (@nil B) = nil :=
|
2014-11-16 06:36:52 +00:00
|
|
|
|
rfl
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem zip_cons_cons {n : nat} (a : A) (b : B) (va : vector A n) (vb : vector B n) :
|
|
|
|
|
zip (a::va) (b::vb) = ((a, b) :: zip va vb) :=
|
2014-11-16 06:36:52 +00:00
|
|
|
|
rfl
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem unzip_zip : ∀ {n : nat} (v₁ : vector A n) (v₂ : vector B n), unzip (zip v₁ v₂) = (v₁, v₂)
|
2015-03-07 01:37:03 +00:00
|
|
|
|
| 0 nil nil := rfl
|
|
|
|
|
| (succ n) (a::va) (b::vb) := calc
|
2015-01-08 19:17:28 +00:00
|
|
|
|
unzip (zip (a :: va) (b :: vb))
|
|
|
|
|
= (a :: pr₁ (unzip (zip va vb)), b :: pr₂ (unzip (zip va vb))) : rfl
|
|
|
|
|
... = (a :: pr₁ (va, vb), b :: pr₂ (va, vb)) : {unzip_zip va vb}
|
|
|
|
|
... = (a :: va, b :: vb) : rfl
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem zip_unzip : ∀ {n : nat} (v : vector (A × B) n), zip (pr₁ (unzip v)) (pr₂ (unzip v)) = v
|
2015-03-07 01:37:03 +00:00
|
|
|
|
| 0 nil := rfl
|
|
|
|
|
| (succ n) ((a, b) :: v) := calc
|
2015-01-08 19:17:28 +00:00
|
|
|
|
zip (pr₁ (unzip ((a, b) :: v))) (pr₂ (unzip ((a, b) :: v)))
|
|
|
|
|
= (a, b) :: zip (pr₁ (unzip v)) (pr₂ (unzip v)) : rfl
|
|
|
|
|
... = (a, b) :: v : {zip_unzip v}
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2014-12-22 20:33:29 +00:00
|
|
|
|
/- Concat -/
|
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
definition concat : Π {n : nat}, vector A n → A → vector A (succ n)
|
|
|
|
|
| concat nil a := a :: nil
|
|
|
|
|
| concat (b::v) a := b :: concat v a
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2014-11-16 06:36:52 +00:00
|
|
|
|
theorem concat_nil (a : A) : concat nil a = a :: nil :=
|
|
|
|
|
rfl
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-01-08 19:17:28 +00:00
|
|
|
|
theorem concat_cons {n : nat} (b : A) (v : vector A n) (a : A) : concat (b :: v) a = b :: concat v a :=
|
|
|
|
|
rfl
|
2014-09-05 16:45:01 +00:00
|
|
|
|
|
2015-02-26 00:20:44 +00:00
|
|
|
|
theorem last_concat : ∀ {n : nat} (v : vector A n) (a : A), last (concat v a) = a
|
2015-03-07 01:37:03 +00:00
|
|
|
|
| 0 nil a := rfl
|
|
|
|
|
| (succ n) (b::v) a := calc
|
2015-01-08 19:17:28 +00:00
|
|
|
|
last (concat (b::v) a) = last (concat v a) : rfl
|
|
|
|
|
... = a : last_concat v a
|
2014-09-19 23:15:04 +00:00
|
|
|
|
end vector
|