2014-11-09 04:11:46 +00:00
|
|
|
|
import logic data.nat.basic data.prod data.unit
|
|
|
|
|
open nat prod
|
2014-10-26 22:47:29 +00:00
|
|
|
|
|
|
|
|
|
inductive vector (A : Type) : nat → Type :=
|
2015-02-26 01:00:10 +00:00
|
|
|
|
| vnil {} : vector A zero
|
|
|
|
|
| vcons : Π {n : nat}, A → vector A n → vector A (succ n)
|
2014-10-26 22:47:29 +00:00
|
|
|
|
|
|
|
|
|
namespace vector
|
2014-12-02 01:11:06 +00:00
|
|
|
|
-- print definition no_confusion
|
2014-11-12 01:23:59 +00:00
|
|
|
|
infixr `::` := vcons
|
2014-10-26 22:47:29 +00:00
|
|
|
|
|
2015-02-11 20:49:27 +00:00
|
|
|
|
local abbreviation no_confusion := @vector.no_confusion
|
|
|
|
|
local abbreviation below := @vector.below
|
|
|
|
|
|
2014-10-26 22:47:29 +00:00
|
|
|
|
theorem vcons.inj₁ {A : Type} {n : nat} (a₁ a₂ : A) (v₁ v₂ : vector A n) : vcons a₁ v₁ = vcons a₂ v₂ → a₁ = a₂ :=
|
|
|
|
|
begin
|
|
|
|
|
intro h, apply (no_confusion h), intros, assumption
|
|
|
|
|
end
|
|
|
|
|
|
2014-11-09 02:56:52 +00:00
|
|
|
|
theorem vcons.inj₂ {A : Type} {n : nat} (a₁ a₂ : A) (v₁ v₂ : vector A n) : vcons a₁ v₁ = vcons a₂ v₂ → v₁ = v₂ :=
|
2014-10-26 22:47:29 +00:00
|
|
|
|
begin
|
2014-11-09 02:56:52 +00:00
|
|
|
|
intro h, apply heq.to_eq, apply (no_confusion h), intros, eassumption,
|
2014-10-26 22:47:29 +00:00
|
|
|
|
end
|
2014-11-09 04:11:46 +00:00
|
|
|
|
|
2014-12-02 01:11:06 +00:00
|
|
|
|
set_option pp.universes true
|
|
|
|
|
check @below
|
|
|
|
|
|
2014-12-03 18:39:22 +00:00
|
|
|
|
namespace manual
|
2014-11-09 04:11:46 +00:00
|
|
|
|
section
|
|
|
|
|
universe variables l₁ l₂
|
|
|
|
|
variable {A : Type.{l₁}}
|
2014-12-02 01:11:06 +00:00
|
|
|
|
variable {C : Π (n : nat), vector A n → Type.{l₂}}
|
2015-02-11 20:49:27 +00:00
|
|
|
|
definition brec_on {n : nat} (v : vector A n) (H : Π (n : nat) (v : vector A n), @vector.below A C n v → C n v) : C n v :=
|
|
|
|
|
have general : C n v × @vector.below A C n v, from
|
|
|
|
|
vector.rec_on v
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(pair (H zero vnil unit.star) unit.star)
|
2015-02-11 20:49:27 +00:00
|
|
|
|
(λ (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (r₁ : C n₁ v₁ × @vector.below A C n₁ v₁),
|
|
|
|
|
have b : @vector.below A C _ (vcons a₁ v₁), from
|
2014-11-09 04:11:46 +00:00
|
|
|
|
r₁,
|
|
|
|
|
have c : C (succ n₁) (vcons a₁ v₁), from
|
|
|
|
|
H (succ n₁) (vcons a₁ v₁) b,
|
|
|
|
|
pair c b),
|
|
|
|
|
pr₁ general
|
|
|
|
|
end
|
2014-12-03 18:39:22 +00:00
|
|
|
|
end manual
|
2014-11-09 04:11:46 +00:00
|
|
|
|
|
2015-02-11 20:49:27 +00:00
|
|
|
|
-- check vector.brec_on
|
2014-11-09 04:11:46 +00:00
|
|
|
|
|
2014-11-13 00:38:46 +00:00
|
|
|
|
definition bw := @below
|
|
|
|
|
|
2014-11-12 01:23:59 +00:00
|
|
|
|
definition sum {n : nat} (v : vector nat n) : nat :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
vector.brec_on v (λ (n : nat) (v : vector nat n),
|
|
|
|
|
vector.cases_on v
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ (B : bw vnil), zero)
|
|
|
|
|
(λ (n₁ : nat) (a : nat) (v₁ : vector nat n₁) (B : bw (vcons a v₁)),
|
|
|
|
|
a + pr₁ B))
|
|
|
|
|
|
|
|
|
|
example : sum (10 :: 20 :: vnil) = 30 :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
definition addk {n : nat} (v : vector nat n) (k : nat) : vector nat n :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
vector.brec_on v (λ (n : nat) (v : vector nat n),
|
|
|
|
|
vector.cases_on v
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ (B : bw vnil), vnil)
|
|
|
|
|
(λ (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)),
|
|
|
|
|
vcons (a₁+k) (pr₁ B)))
|
|
|
|
|
|
|
|
|
|
example : addk (1 :: 2 :: vnil) 3 = 4 :: 5 :: vnil :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2014-11-13 00:38:46 +00:00
|
|
|
|
definition append.{l} {A : Type.{l+1}} {n m : nat} (w : vector A m) (v : vector A n) : vector A (n + m) :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
vector.brec_on w (λ (n : nat) (w : vector A n),
|
|
|
|
|
vector.cases_on w
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ (B : bw vnil), v)
|
|
|
|
|
(λ (n₁ : nat) (a₁ : A) (v₁ : vector A n₁) (B : bw (vcons a₁ v₁)),
|
|
|
|
|
vcons a₁ (pr₁ B)))
|
|
|
|
|
|
|
|
|
|
example : append (1 :: 2 :: vnil) (3 :: vnil) = 1 :: 2 :: 3 :: vnil :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
definition head {A : Type} {n : nat} (v : vector A (succ n)) : A :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
vector.cases_on v
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ H : succ n = 0, nat.no_confusion H)
|
|
|
|
|
(λn' h t (H : succ n = succ n'), h)
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
definition tail {A : Type} {n : nat} (v : vector A (succ n)) : vector A n :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
@vector.cases_on A (λn' v, succ n = n' → vector A (pred n')) (succ n) v
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ H : succ n = 0, nat.no_confusion H)
|
|
|
|
|
(λ (n' : nat) (h : A) (t : vector A n') (H : succ n = succ n'),
|
|
|
|
|
t)
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
definition add {n : nat} (w v : vector nat n) : vector nat n :=
|
2015-02-11 20:49:27 +00:00
|
|
|
|
@vector.brec_on nat (λ (n : nat) (v : vector nat n), vector nat n → vector nat n) n w
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ (n : nat) (w : vector nat n),
|
2015-02-11 20:49:27 +00:00
|
|
|
|
vector.cases_on w
|
2014-11-12 01:23:59 +00:00
|
|
|
|
(λ (B : bw vnil) (w : vector nat zero), vnil)
|
|
|
|
|
(λ (n₁ : nat) (a₁ : nat) (v₁ : vector nat n₁) (B : bw (vcons a₁ v₁)) (v : vector nat (succ n₁)),
|
|
|
|
|
vcons (a₁ + head v) (pr₁ B (tail v)))) v
|
|
|
|
|
|
|
|
|
|
example : add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2014-12-02 01:11:06 +00:00
|
|
|
|
definition map {A B C : Type} {n : nat} (f : A → B → C) (w : vector A n) (v : vector B n) : vector C n :=
|
2014-11-29 21:28:01 +00:00
|
|
|
|
let P := λ (n : nat) (v : vector A n), vector B n → vector C n in
|
2015-02-11 20:49:27 +00:00
|
|
|
|
@vector.brec_on A P n w
|
2014-11-29 21:28:01 +00:00
|
|
|
|
(λ (n : nat) (w : vector A n),
|
|
|
|
|
begin
|
2015-05-14 21:32:54 +00:00
|
|
|
|
cases w with [n', h₁, t₁],
|
2014-11-29 21:28:01 +00:00
|
|
|
|
show @below A P zero vnil → vector B zero → vector C zero, from
|
|
|
|
|
λ b v, vnil,
|
2015-05-14 21:32:54 +00:00
|
|
|
|
show @below A P (succ n') (h₁ :: t₁) → vector B (succ n') → vector C (succ n'), from
|
2014-11-29 21:28:01 +00:00
|
|
|
|
λ b v,
|
|
|
|
|
begin
|
2015-05-14 21:32:54 +00:00
|
|
|
|
cases v with [n', h₂, t₂],
|
|
|
|
|
have r : vector B n' → vector C n', from pr₁ b,
|
2015-02-25 21:58:39 +00:00
|
|
|
|
exact ((f h₁ h₂) :: r t₂),
|
2014-11-29 21:28:01 +00:00
|
|
|
|
end
|
|
|
|
|
end) v
|
|
|
|
|
|
2014-12-02 01:11:06 +00:00
|
|
|
|
theorem map_nil_nil {A B C : Type} (f : A → B → C) : map f vnil vnil = vnil :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
theorem map_cons_cons {A B C : Type} (f : A → B → C) (a : A) (b : B) {n : nat} (va : vector A n) (vb : vector B n) :
|
|
|
|
|
map f (a :: va) (b :: vb) = f a b :: map f va vb :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2014-11-30 21:53:02 +00:00
|
|
|
|
example : map nat.add (1 :: 2 :: vnil) (3 :: 5 :: vnil) = 4 :: 7 :: vnil :=
|
2014-11-29 21:28:01 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
print definition map
|
|
|
|
|
|
2014-10-26 22:47:29 +00:00
|
|
|
|
end vector
|