| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of n-ary products |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Recursive.Properties where |
| 10 |
|
| 11 |
open import Level using (Level) |
| 12 |
open import Data.Nat.Base hiding (_^_) |
| 13 |
open import Data.Product.Base |
| 14 |
open import Data.Vec.Recursive |
| 15 |
open import Data.Vec.Base using (Vec; _∷_) |
| 16 |
open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core |
| 18 |
using (_≡_; refl; cong; cong₂) |
| 19 |
open import Relation.Binary.PropositionalEquality.Properties |
| 20 |
using (module ≡-Reasoning) |
| 21 |
open ≡-Reasoning |
| 22 |
|
| 23 |
private |
| 24 |
variable |
| 25 |
a : Level |
| 26 |
A : Set a |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- Basic proofs |
| 30 |
|
| 31 |
cons-head-tail-identity : ∀ n (as : A ^ suc n) → cons n (head n as) (tail n as) ≡ as |
| 32 |
cons-head-tail-identity 0 as = refl |
| 33 |
cons-head-tail-identity (suc n) as = refl |
| 34 |
|
| 35 |
head-cons-identity : ∀ n a (as : A ^ n) → head n (cons n a as) ≡ a |
| 36 |
head-cons-identity 0 a as = refl |
| 37 |
head-cons-identity (suc n) a as = refl |
| 38 |
|
| 39 |
tail-cons-identity : ∀ n a (as : A ^ n) → tail n (cons n a as) ≡ as |
| 40 |
tail-cons-identity 0 a as = refl |
| 41 |
tail-cons-identity (suc n) a as = refl |
| 42 |
|
| 43 |
append-cons : ∀ m n a (xs : A ^ m) ys → |
| 44 |
append (suc m) n (cons m a xs) ys ≡ cons (m + n) a (append m n xs ys) |
| 45 |
append-cons 0 n a xs ys = refl |
| 46 |
append-cons (suc m) n a xs ys = refl |
| 47 |
|
| 48 |
append-splitAt-identity : ∀ m n (as : A ^ (m + n)) → uncurry (append m n) (splitAt m n as) ≡ as |
| 49 |
append-splitAt-identity 0 n as = refl |
| 50 |
append-splitAt-identity (suc m) n as = begin |
| 51 |
let x = head (m + n) as in |
| 52 |
let (xs , ys) = splitAt m n (tail (m + n) as) in |
| 53 |
append (suc m) n (cons m (head (m + n) as) xs) ys |
| 54 |
≡⟨ append-cons m n x xs ys ⟩ |
| 55 |
cons (m + n) x (append m n xs ys) |
| 56 |
≡⟨ cong (cons (m + n) x) (append-splitAt-identity m n (tail (m + n) as)) ⟩ |
| 57 |
cons (m + n) x (tail (m + n) as) |
| 58 |
≡⟨ cons-head-tail-identity (m + n) as ⟩ |
| 59 |
as |
| 60 |
∎ |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- Conversion to and from Vec |
| 64 |
|
| 65 |
fromVec∘toVec : ∀ n (xs : A ^ n) → fromVec (toVec n xs) ≡ xs |
| 66 |
fromVec∘toVec 0 _ = refl |
| 67 |
fromVec∘toVec (suc n) xs = begin |
| 68 |
cons n (head n xs) (fromVec (toVec n (tail n xs))) |
| 69 |
≡⟨ cong (cons n (head n xs)) (fromVec∘toVec n (tail n xs)) ⟩ |
| 70 |
cons n (head n xs) (tail n xs) |
| 71 |
≡⟨ cons-head-tail-identity n xs ⟩ |
| 72 |
xs ∎ |
| 73 |
|
| 74 |
toVec∘fromVec : ∀ {n} (xs : Vec A n) → toVec n (fromVec xs) ≡ xs |
| 75 |
toVec∘fromVec Vec.[] = refl |
| 76 |
toVec∘fromVec {n = suc n} (x Vec.∷ xs) = begin |
| 77 |
head n (cons n x (fromVec xs)) Vec.∷ toVec n (tail n (cons n x (fromVec xs))) |
| 78 |
≡⟨ cong₂ (λ x xs → x Vec.∷ toVec n xs) hd-prf tl-prf ⟩ |
| 79 |
x Vec.∷ toVec n (fromVec xs) |
| 80 |
≡⟨ cong (x Vec.∷_) (toVec∘fromVec xs) ⟩ |
| 81 |
x Vec.∷ xs |
| 82 |
∎ where |
| 83 |
|
| 84 |
hd-prf = head-cons-identity _ x (fromVec xs) |
| 85 |
tl-prf = tail-cons-identity _ x (fromVec xs) |
| 86 |
|
| 87 |
↔Vec : ∀ n → A ^ n ↔ Vec A n |
| 88 |
↔Vec n = mk↔ₛ′ (toVec n) fromVec toVec∘fromVec (fromVec∘toVec n) |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- DEPRECATED NAMES |
| 92 |
------------------------------------------------------------------------ |
| 93 |
-- Please use the new names as continuing support for the old names is |
| 94 |
-- not guaranteed. |
| 95 |
|
| 96 |
-- Version 2.0 |
| 97 |
|
| 98 |
append-cons-commute = append-cons |
| 99 |
{-# WARNING_ON_USAGE append-cons-commute |
| 100 |
"Warning: append-cons-commute was deprecated in v2.0. |
| 101 |
Please use append-cons instead." |
| 102 |
#-} |