| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An equational reasoning library for propositional equality over |
| 5 |
-- vectors of different indices using cast. |
| 6 |
-- |
| 7 |
-- See README.Data.Vec.Relation.Binary.Equality.Cast for |
| 8 |
-- documentation and examples. |
| 9 |
------------------------------------------------------------------------ |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
module Data.Vec.Relation.Binary.Equality.Cast where |
| 14 |
|
| 15 |
open import Level using (Level) |
| 16 |
open import Function.Base using (_∘_) |
| 17 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 18 |
open import Data.Nat.Properties using (suc-injective) |
| 19 |
open import Data.Vec.Base |
| 20 |
open import Relation.Binary.Core using (REL; _⇒_) |
| 21 |
open import Relation.Binary.Definitions using (Sym; Trans) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core |
| 23 |
using (_≡_; refl; trans; sym; cong) |
| 24 |
open import Relation.Binary.PropositionalEquality.Properties |
| 25 |
using (module ≡-Reasoning) |
| 26 |
|
| 27 |
private |
| 28 |
variable |
| 29 |
a b : Level |
| 30 |
A B : Set a |
| 31 |
l m n o : ℕ |
| 32 |
xs ys zs : Vec A n |
| 33 |
|
| 34 |
|
| 35 |
cast-is-id : .(eq : m ≡ m) (xs : Vec A m) → cast eq xs ≡ xs |
| 36 |
cast-is-id eq [] = refl |
| 37 |
cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs) |
| 38 |
|
| 39 |
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (xs : Vec A m) → |
| 40 |
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs |
| 41 |
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl |
| 42 |
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) = |
| 43 |
cong (x ∷_) (cast-trans (suc-injective eq₁) (suc-injective eq₂) xs) |
| 44 |
|
| 45 |
|
| 46 |
infix 3 _≈[_]_ |
| 47 |
|
| 48 |
_≈[_]_ : ∀ {n m} → Vec A n → .(eq : n ≡ m) → Vec A m → Set _ |
| 49 |
xs ≈[ eq ] ys = cast eq xs ≡ ys |
| 50 |
|
| 51 |
------------------------------------------------------------------------ |
| 52 |
-- _≈[_]_ is ‘reflexive’, ‘symmetric’ and ‘transitive’ |
| 53 |
|
| 54 |
≈-reflexive : ∀ {n} → _≡_ ⇒ (λ xs ys → _≈[_]_ {A = A} {n} xs refl ys) |
| 55 |
≈-reflexive {x = x} eq = trans (cast-is-id refl x) eq |
| 56 |
|
| 57 |
≈-sym : .{m≡n : m ≡ n} → Sym {A = Vec A m} _≈[ m≡n ]_ _≈[ sym m≡n ]_ |
| 58 |
≈-sym {m≡n = m≡n} {xs} {ys} xs≈ys = begin |
| 59 |
cast (sym m≡n) ys ≡⟨ cong (cast (sym m≡n)) xs≈ys ⟨ |
| 60 |
cast (sym m≡n) (cast m≡n xs) ≡⟨ cast-trans m≡n (sym m≡n) xs ⟩ |
| 61 |
cast (trans m≡n (sym m≡n)) xs ≡⟨ cast-is-id (trans m≡n (sym m≡n)) xs ⟩ |
| 62 |
xs ∎ |
| 63 |
where open ≡-Reasoning |
| 64 |
|
| 65 |
≈-trans : ∀ .{m≡n : m ≡ n} .{n≡o : n ≡ o} → |
| 66 |
Trans {A = Vec A m} _≈[ m≡n ]_ _≈[ n≡o ]_ _≈[ trans m≡n n≡o ]_ |
| 67 |
≈-trans {m≡n = m≡n} {n≡o} {xs} {ys} {zs} xs≈ys ys≈zs = begin |
| 68 |
cast (trans m≡n n≡o) xs ≡⟨ cast-trans m≡n n≡o xs ⟨ |
| 69 |
cast n≡o (cast m≡n xs) ≡⟨ cong (cast n≡o) xs≈ys ⟩ |
| 70 |
cast n≡o ys ≡⟨ ys≈zs ⟩ |
| 71 |
zs ∎ |
| 72 |
where open ≡-Reasoning |
| 73 |
|
| 74 |
≈-cong′ : ∀ {f-len : ℕ → ℕ} (f : ∀ {n} → Vec A n → Vec B (f-len n)) |
| 75 |
{m n} {xs : Vec A m} {ys : Vec A n} .{eq} → xs ≈[ eq ] ys → |
| 76 |
f xs ≈[ cong f-len eq ] f ys |
| 77 |
≈-cong′ f {m = zero} {n = zero} {xs = []} {ys = []} refl = cast-is-id refl (f []) |
| 78 |
≈-cong′ f {m = suc m} {n = suc n} {xs = x ∷ xs} {ys = y ∷ ys} refl = ≈-cong′ (f ∘ (x ∷_)) refl |
| 79 |
|
| 80 |
------------------------------------------------------------------------ |
| 81 |
-- Reasoning combinators |
| 82 |
|
| 83 |
module CastReasoning where |
| 84 |
|
| 85 |
open ≡-Reasoning public |
| 86 |
renaming (begin_ to begin-≡_; _∎ to _≡-∎) |
| 87 |
|
| 88 |
begin_ : ∀ .{m≡n : m ≡ n} {xs : Vec A m} {ys} → xs ≈[ m≡n ] ys → cast m≡n xs ≡ ys |
| 89 |
begin xs≈ys = xs≈ys |
| 90 |
|
| 91 |
_∎ : (xs : Vec A n) → cast refl xs ≡ xs |
| 92 |
_∎ xs = ≈-reflexive refl |
| 93 |
|
| 94 |
_≈⟨⟩_ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys} → xs ≈[ m≡n ] ys → xs ≈[ m≡n ] ys |
| 95 |
xs ≈⟨⟩ xs≈ys = xs≈ys |
| 96 |
|
| 97 |
-- composition of _≈[_]_ |
| 98 |
step-≈-⟩ : ∀ .{m≡n : m ≡ n}.{m≡o : m ≡ o} (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → |
| 99 |
ys ≈[ trans (sym m≡n) m≡o ] zs → xs ≈[ m≡n ] ys → xs ≈[ m≡o ] zs |
| 100 |
step-≈-⟩ xs ys≈zs xs≈ys = ≈-trans xs≈ys ys≈zs |
| 101 |
|
| 102 |
step-≈-⟨ : ∀ .{n≡m : n ≡ m}.{m≡o : m ≡ o} (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → |
| 103 |
ys ≈[ trans n≡m m≡o ] zs → ys ≈[ n≡m ] xs → xs ≈[ m≡o ] zs |
| 104 |
step-≈-⟨ xs ys≈zs ys≈xs = step-≈-⟩ xs ys≈zs (≈-sym ys≈xs) |
| 105 |
|
| 106 |
-- composition of the equality type on the right-hand side of _≈[_]_, |
| 107 |
-- or escaping to ordinary _≡_ |
| 108 |
step-≃-⟩ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → ys ≡ zs → xs ≈[ m≡n ] ys → xs ≈[ m≡n ] zs |
| 109 |
step-≃-⟩ xs ys≡zs xs≈ys = ≈-trans xs≈ys (≈-reflexive ys≡zs) |
| 110 |
|
| 111 |
step-≃-⟨ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → ys ≡ zs → ys ≈[ sym m≡n ] xs → xs ≈[ m≡n ] zs |
| 112 |
step-≃-⟨ xs ys≡zs ys≈xs = step-≃-⟩ xs ys≡zs (≈-sym ys≈xs) |
| 113 |
|
| 114 |
-- composition of the equality type on the left-hand side of _≈[_]_ |
| 115 |
step-≂-⟩ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → ys ≈[ m≡n ] zs → xs ≡ ys → xs ≈[ m≡n ] zs |
| 116 |
step-≂-⟩ xs ys≈zs xs≡ys = ≈-trans (≈-reflexive xs≡ys) ys≈zs |
| 117 |
|
| 118 |
step-≂-⟨ : ∀ .{m≡n : m ≡ n} (xs : Vec A m) {ys zs} → ys ≈[ m≡n ] zs → ys ≡ xs → xs ≈[ m≡n ] zs |
| 119 |
step-≂-⟨ xs ys≈zs ys≡xs = step-≂-⟩ xs ys≈zs (sym ys≡xs) |
| 120 |
|
| 121 |
-- `cong` after a `_≈[_]_` step that exposes the `cast` to the `cong` |
| 122 |
-- operation |
| 123 |
≈-cong : ∀ .{l≡o : l ≡ o} .{m≡n : m ≡ n} {xs : Vec A m} {ys zs} (f : Vec A o → Vec A n) → |
| 124 |
xs ≈[ m≡n ] f (cast l≡o ys) → ys ≈[ l≡o ] zs → xs ≈[ m≡n ] f zs |
| 125 |
≈-cong f xs≈fys ys≈zs = trans xs≈fys (cong f ys≈zs) |
| 126 |
|
| 127 |
------------------------------------------------------------------------ |
| 128 |
-- convenient syntax for ‘equational’ reasoning |
| 129 |
|
| 130 |
infix 1 begin_ |
| 131 |
infixr 2 step-≃-⟩ step-≃-⟨ step-≂-⟩ step-≂-⟨ step-≈-⟩ step-≈-⟨ _≈⟨⟩_ ≈-cong |
| 132 |
infix 3 _∎ |
| 133 |
|
| 134 |
syntax step-≃-⟩ xs ys≡zs xs≈ys = xs ≃⟨ xs≈ys ⟩ ys≡zs |
| 135 |
syntax step-≃-⟨ xs ys≡zs xs≈ys = xs ≃⟨ xs≈ys ⟨ ys≡zs |
| 136 |
syntax step-≂-⟩ xs ys≈zs xs≡ys = xs ≂⟨ xs≡ys ⟩ ys≈zs |
| 137 |
syntax step-≂-⟨ xs ys≈zs ys≡xs = xs ≂⟨ ys≡xs ⟨ ys≈zs |
| 138 |
syntax step-≈-⟩ xs ys≈zs xs≈ys = xs ≈⟨ xs≈ys ⟩ ys≈zs |
| 139 |
syntax step-≈-⟨ xs ys≈zs ys≈xs = xs ≈⟨ ys≈xs ⟨ ys≈zs |