| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Coinductive vectors |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module Codata.Musical.Covec where |
| 10 |
|
| 11 |
open import Codata.Musical.Notation using (♭; ∞; ♯_) |
| 12 |
open import Codata.Musical.Conat as Coℕ using (Coℕ; zero; suc; _+_) |
| 13 |
open import Codata.Musical.Cofin using (Cofin; zero; suc) |
| 14 |
open import Codata.Musical.Colist as Colist using (Colist; []; _∷_) |
| 15 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 16 |
open import Data.Vec.Base using (Vec; []; _∷_) |
| 17 |
open import Data.Product.Base using (_,_) |
| 18 |
open import Function.Base using (_∋_) |
| 19 |
open import Level using (Level) |
| 20 |
open import Relation.Binary.Core using (_⇒_; _=[_]⇒_) |
| 21 |
open import Relation.Binary.Bundles using (Setoid; Poset) |
| 22 |
open import Relation.Binary.Definitions |
| 23 |
using (Reflexive; Symmetric; Transitive; Antisymmetric) |
| 24 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 25 |
|
| 26 |
private |
| 27 |
variable |
| 28 |
a b : Level |
| 29 |
A : Set a |
| 30 |
B : Set b |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- The type |
| 34 |
|
| 35 |
infixr 5 _∷_ |
| 36 |
data Covec (A : Set a) : Coℕ → Set a where |
| 37 |
[] : Covec A zero |
| 38 |
_∷_ : ∀ {n} (x : A) (xs : ∞ (Covec A (♭ n))) → Covec A (suc n) |
| 39 |
|
| 40 |
∷-injectiveˡ : ∀ {a b} {n} {as bs} → (Covec A (suc n) ∋ a ∷ as) ≡ b ∷ bs → a ≡ b |
| 41 |
∷-injectiveˡ ≡.refl = ≡.refl |
| 42 |
|
| 43 |
∷-injectiveʳ : ∀ {a b} {n} {as bs} → (Covec A (suc n) ∋ a ∷ as) ≡ b ∷ bs → as ≡ bs |
| 44 |
∷-injectiveʳ ≡.refl = ≡.refl |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Some operations |
| 48 |
|
| 49 |
map : ∀ {n} → (A → B) → Covec A n → Covec B n |
| 50 |
map f [] = [] |
| 51 |
map f (x ∷ xs) = f x ∷ ♯ map f (♭ xs) |
| 52 |
|
| 53 |
fromVec : ∀ {n} → Vec A n → Covec A (Coℕ.fromℕ n) |
| 54 |
fromVec [] = [] |
| 55 |
fromVec (x ∷ xs) = x ∷ ♯ fromVec xs |
| 56 |
|
| 57 |
fromColist : (xs : Colist A) → Covec A (Colist.length xs) |
| 58 |
fromColist [] = [] |
| 59 |
fromColist (x ∷ xs) = x ∷ ♯ fromColist (♭ xs) |
| 60 |
|
| 61 |
take : ∀ m {n} → Covec A (m + n) → Covec A m |
| 62 |
take zero xs = [] |
| 63 |
take (suc n) (x ∷ xs) = x ∷ ♯ take (♭ n) (♭ xs) |
| 64 |
|
| 65 |
drop : ∀ m {n} → Covec A (Coℕ.fromℕ m + n) → Covec A n |
| 66 |
drop zero xs = xs |
| 67 |
drop (suc n) (x ∷ xs) = drop n (♭ xs) |
| 68 |
|
| 69 |
replicate : ∀ n → A → Covec A n |
| 70 |
replicate zero x = [] |
| 71 |
replicate (suc n) x = x ∷ ♯ replicate (♭ n) x |
| 72 |
|
| 73 |
lookup : ∀ {n} → Covec A n → Cofin n → A |
| 74 |
lookup (x ∷ xs) zero = x |
| 75 |
lookup (x ∷ xs) (suc n) = lookup (♭ xs) n |
| 76 |
|
| 77 |
infixr 5 _++_ |
| 78 |
|
| 79 |
_++_ : ∀ {m n} → Covec A m → Covec A n → Covec A (m + n) |
| 80 |
[] ++ ys = ys |
| 81 |
(x ∷ xs) ++ ys = x ∷ ♯ (♭ xs ++ ys) |
| 82 |
|
| 83 |
[_] : A → Covec A (suc (♯ zero)) |
| 84 |
[ x ] = x ∷ ♯ [] |
| 85 |
|
| 86 |
------------------------------------------------------------------------ |
| 87 |
-- Equality and other relations |
| 88 |
|
| 89 |
-- xs ≈ ys means that xs and ys are equal. |
| 90 |
|
| 91 |
infix 4 _≈_ |
| 92 |
|
| 93 |
data _≈_ {A : Set a} : ∀ {n} (xs ys : Covec A n) → Set a where |
| 94 |
[] : [] ≈ [] |
| 95 |
_∷_ : ∀ {n} x {xs ys} |
| 96 |
(xs≈ : ∞ (♭ xs ≈ ♭ ys)) → _≈_ {n = suc n} (x ∷ xs) (x ∷ ys) |
| 97 |
|
| 98 |
-- x ∈ xs means that x is a member of xs. |
| 99 |
|
| 100 |
infix 4 _∈_ |
| 101 |
|
| 102 |
data _∈_ {A : Set a} : ∀ {n} → A → Covec A n → Set a where |
| 103 |
here : ∀ {n x } {xs} → _∈_ {n = suc n} x (x ∷ xs) |
| 104 |
there : ∀ {n x y} {xs} (x∈xs : x ∈ ♭ xs) → _∈_ {n = suc n} x (y ∷ xs) |
| 105 |
|
| 106 |
-- xs ⊑ ys means that xs is a prefix of ys. |
| 107 |
|
| 108 |
infix 4 _⊑_ |
| 109 |
|
| 110 |
data _⊑_ {A : Set a} : ∀ {m n} → Covec A m → Covec A n → Set a where |
| 111 |
[] : ∀ {n} {ys : Covec A n} → [] ⊑ ys |
| 112 |
_∷_ : ∀ {m n} x {xs ys} (p : ∞ (♭ xs ⊑ ♭ ys)) → |
| 113 |
_⊑_ {m = suc m} {suc n} (x ∷ xs) (x ∷ ys) |
| 114 |
|
| 115 |
------------------------------------------------------------------------ |
| 116 |
-- Some proofs |
| 117 |
|
| 118 |
setoid : ∀ {a} → Set a → Coℕ → Setoid _ _ |
| 119 |
setoid A n = record |
| 120 |
{ Carrier = Covec A n |
| 121 |
; _≈_ = _≈_ |
| 122 |
; isEquivalence = record |
| 123 |
{ refl = refl |
| 124 |
; sym = sym |
| 125 |
; trans = trans |
| 126 |
} |
| 127 |
} |
| 128 |
where |
| 129 |
refl : ∀ {n} → Reflexive (_≈_ {n = n}) |
| 130 |
refl {x = []} = [] |
| 131 |
refl {x = x ∷ xs} = x ∷ ♯ refl |
| 132 |
|
| 133 |
sym : ∀ {n} → Symmetric (_≈_ {A = A} {n}) |
| 134 |
sym [] = [] |
| 135 |
sym (x ∷ xs≈) = x ∷ ♯ sym (♭ xs≈) |
| 136 |
|
| 137 |
trans : ∀ {n} → Transitive (_≈_ {A = A} {n}) |
| 138 |
trans [] [] = [] |
| 139 |
trans (x ∷ xs≈) (.x ∷ ys≈) = x ∷ ♯ trans (♭ xs≈) (♭ ys≈) |
| 140 |
|
| 141 |
poset : ∀ {a} → Set a → Coℕ → Poset _ _ _ |
| 142 |
poset A n = record |
| 143 |
{ Carrier = Covec A n |
| 144 |
; _≈_ = _≈_ |
| 145 |
; _≤_ = _⊑_ |
| 146 |
; isPartialOrder = record |
| 147 |
{ isPreorder = record |
| 148 |
{ isEquivalence = Setoid.isEquivalence (setoid A n) |
| 149 |
; reflexive = reflexive |
| 150 |
; trans = trans |
| 151 |
} |
| 152 |
; antisym = antisym |
| 153 |
} |
| 154 |
} |
| 155 |
where |
| 156 |
reflexive : ∀ {n} → _≈_ {n = n} ⇒ _⊑_ |
| 157 |
reflexive [] = [] |
| 158 |
reflexive (x ∷ xs≈) = x ∷ ♯ reflexive (♭ xs≈) |
| 159 |
|
| 160 |
trans : ∀ {n} → Transitive (_⊑_ {n = n}) |
| 161 |
trans [] _ = [] |
| 162 |
trans (x ∷ xs≈) (.x ∷ ys≈) = x ∷ ♯ trans (♭ xs≈) (♭ ys≈) |
| 163 |
|
| 164 |
tail : ∀ {n x y xs ys} → |
| 165 |
_∷_ {n = n} x xs ⊑ _∷_ {n = n} y ys → ♭ xs ⊑ ♭ ys |
| 166 |
tail (_ ∷ p) = ♭ p |
| 167 |
|
| 168 |
antisym : ∀ {n} → Antisymmetric (_≈_ {n = n}) _⊑_ |
| 169 |
antisym [] [] = [] |
| 170 |
antisym (x ∷ p₁) p₂ = x ∷ ♯ antisym (♭ p₁) (tail p₂) |
| 171 |
|
| 172 |
map-cong : ∀ {n} (f : A → B) → _≈_ {n = n} =[ map f ]⇒ _≈_ |
| 173 |
map-cong f [] = [] |
| 174 |
map-cong f (x ∷ xs≈) = f x ∷ ♯ map-cong f (♭ xs≈) |
| 175 |
|
| 176 |
take-⊑ : ∀ m {n} (xs : Covec A (m + n)) → take m xs ⊑ xs |
| 177 |
take-⊑ zero xs = [] |
| 178 |
take-⊑ (suc n) (x ∷ xs) = x ∷ ♯ take-⊑ (♭ n) (♭ xs) |