| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of the Any predicate on colists |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module Codata.Musical.Colist.Relation.Unary.Any.Properties where |
| 10 |
|
| 11 |
open import Codata.Musical.Colist.Base |
| 12 |
open import Codata.Musical.Colist.Bisimilarity |
| 13 |
open import Codata.Musical.Colist.Relation.Unary.Any |
| 14 |
open import Codata.Musical.Notation |
| 15 |
open import Data.Maybe using (Is-just) |
| 16 |
open import Data.Maybe.Relation.Unary.Any using (just) |
| 17 |
open import Data.Nat.Base using (suc; _≥′_; ≤′-refl; ≤′-step) |
| 18 |
open import Data.Nat.Properties using (s≤′s) |
| 19 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′; [_,_]) |
| 20 |
open import Function.Base using (_∋_; _∘_) |
| 21 |
open import Function.Bundles |
| 22 |
open import Level using (Level; _⊔_) |
| 23 |
open import Relation.Binary.Bundles using (Setoid) |
| 24 |
open import Relation.Binary.PropositionalEquality.Core |
| 25 |
using (_≡_; refl; cong) |
| 26 |
open import Relation.Unary using (Pred) |
| 27 |
|
| 28 |
private |
| 29 |
variable |
| 30 |
a b p q : Level |
| 31 |
A : Set a |
| 32 |
B : Set b |
| 33 |
P : Pred A p |
| 34 |
Q : Pred A q |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- Equality properties |
| 38 |
|
| 39 |
here-injective : ∀ {x xs p q} → (Any P (x ∷ xs) ∋ here p) ≡ here q → p ≡ q |
| 40 |
here-injective refl = refl |
| 41 |
|
| 42 |
there-injective : ∀ {x xs p q} → (Any P (x ∷ xs) ∋ there p) ≡ there q → p ≡ q |
| 43 |
there-injective refl = refl |
| 44 |
|
| 45 |
Any-resp : ∀ {a p q} {A : Set a} {P : A → Set p} {Q : A → Set q} |
| 46 |
{xs ys} → (∀ {x} → P x → Q x) → xs ≈ ys → |
| 47 |
Any P xs → Any Q ys |
| 48 |
Any-resp f (x ∷ xs≈) (here px) = here (f px) |
| 49 |
Any-resp f (x ∷ xs≈) (there p) = there (Any-resp f (♭ xs≈) p) |
| 50 |
|
| 51 |
-- Any maps pointwise isomorphic predicates and equal colists to |
| 52 |
-- isomorphic types. |
| 53 |
|
| 54 |
Any-cong : ∀ {a p q} {A : Set a} {P : A → Set p} {Q : A → Set q} |
| 55 |
{xs ys} → (∀ {i} → P i ↔ Q i) → xs ≈ ys → Any P xs ↔ Any Q ys |
| 56 |
Any-cong {A = A} {P} {Q} {xs} {ys} P↔Q xs≈ys = |
| 57 |
mk↔ₛ′ (to xs≈ys) (from xs≈ys) (to∘from _) (from∘to _) |
| 58 |
where |
| 59 |
open Setoid (setoid _) using (sym) |
| 60 |
|
| 61 |
to : ∀ {xs ys} → xs ≈ ys → Any P xs → Any Q ys |
| 62 |
to xs≈ys = Any-resp (Inverse.to P↔Q) xs≈ys |
| 63 |
|
| 64 |
from : ∀ {xs ys} → xs ≈ ys → Any Q ys → Any P xs |
| 65 |
from xs≈ys = Any-resp (Inverse.from P↔Q) (sym xs≈ys) |
| 66 |
|
| 67 |
to∘from : ∀ {xs ys} (xs≈ys : xs ≈ ys) (q : Any Q ys) → |
| 68 |
to xs≈ys (from xs≈ys q) ≡ q |
| 69 |
to∘from (x ∷ xs≈) (there q) = cong there (to∘from (♭ xs≈) q) |
| 70 |
to∘from (x ∷ xs≈) (here qx) = |
| 71 |
cong here (Inverse.strictlyInverseˡ P↔Q qx) |
| 72 |
|
| 73 |
from∘to : ∀ {xs ys} (xs≈ys : xs ≈ ys) (p : Any P xs) → |
| 74 |
from xs≈ys (to xs≈ys p) ≡ p |
| 75 |
from∘to (x ∷ xs≈) (there p) = cong there (from∘to (♭ xs≈) p) |
| 76 |
from∘to (x ∷ xs≈) (here px) = |
| 77 |
cong here (Inverse.strictlyInverseʳ P↔Q px) |
| 78 |
|
| 79 |
------------------------------------------------------------------------ |
| 80 |
-- map |
| 81 |
|
| 82 |
module _ {f : A → B} where |
| 83 |
|
| 84 |
map⁻ : ∀ {xs} → Any P (map f xs) → Any (P ∘ f) xs |
| 85 |
map⁻ {xs = x ∷ xs} (here px) = here px |
| 86 |
map⁻ {xs = x ∷ xs} (there p) = there (map⁻ p) |
| 87 |
|
| 88 |
map⁺ : ∀ {xs} → Any (P ∘ f) xs → Any P (map f xs) |
| 89 |
map⁺ (here px) = here px |
| 90 |
map⁺ (there p) = there (map⁺ p) |
| 91 |
|
| 92 |
Any-map : ∀ {xs} → Any P (map f xs) ↔ Any (P ∘ f) xs |
| 93 |
Any-map {xs = xs} = mk↔ₛ′ map⁻ map⁺ to∘from from∘to |
| 94 |
where |
| 95 |
from∘to : ∀ {xs} (p : Any P (map f xs)) → map⁺ (map⁻ p) ≡ p |
| 96 |
from∘to {xs = x ∷ xs} (here px) = refl |
| 97 |
from∘to {xs = x ∷ xs} (there p) = cong there (from∘to p) |
| 98 |
|
| 99 |
to∘from : ∀ {xs} (p : Any (P ∘ f) xs) → map⁻ {P = P} (map⁺ p) ≡ p |
| 100 |
to∘from (here px) = refl |
| 101 |
to∘from (there p) = cong there (to∘from p) |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- _⋎_ |
| 105 |
|
| 106 |
⋎⁻ : ∀ xs {ys} → Any P (xs ⋎ ys) → Any P xs ⊎ Any P ys |
| 107 |
⋎⁻ [] p = inj₂ p |
| 108 |
⋎⁻ (x ∷ xs) (here px) = inj₁ (here px) |
| 109 |
⋎⁻ (x ∷ xs) (there p) = [ inj₂ , inj₁ ∘ there ]′ (⋎⁻ _ p) |
| 110 |
|
| 111 |
mutual |
| 112 |
|
| 113 |
⋎⁺₁ : ∀ {xs ys} → Any P xs → Any P (xs ⋎ ys) |
| 114 |
⋎⁺₁ (here px) = here px |
| 115 |
⋎⁺₁ {ys = ys} (there p) = there (⋎⁺₂ ys p) |
| 116 |
|
| 117 |
⋎⁺₂ : ∀ xs {ys} → Any P ys → Any P (xs ⋎ ys) |
| 118 |
⋎⁺₂ [] p = p |
| 119 |
⋎⁺₂ (x ∷ xs) p = there (⋎⁺₁ p) |
| 120 |
|
| 121 |
⋎⁺ : ∀ xs {ys} → Any P xs ⊎ Any P ys → Any P (xs ⋎ ys) |
| 122 |
⋎⁺ xs = [ ⋎⁺₁ , ⋎⁺₂ xs ] |
| 123 |
|
| 124 |
Any-⋎ : ∀ {a p} {A : Set a} {P : A → Set p} xs {ys} → |
| 125 |
Any P (xs ⋎ ys) ↔ (Any P xs ⊎ Any P ys) |
| 126 |
Any-⋎ {P = P} xs = mk↔ₛ′ (⋎⁻ xs) (⋎⁺ xs) (to∘from xs) (from∘to xs) |
| 127 |
where |
| 128 |
|
| 129 |
from∘to : ∀ xs {ys} (p : Any P (xs ⋎ ys)) → ⋎⁺ xs (⋎⁻ xs p) ≡ p |
| 130 |
from∘to [] p = refl |
| 131 |
from∘to (x ∷ xs) (here px) = refl |
| 132 |
from∘to (x ∷ xs) {ys = ys} (there p) with ⋎⁻ ys p | from∘to ys p |
| 133 |
from∘to (x ∷ xs) {ys = ys} (there .(⋎⁺₁ q)) | inj₁ q | refl = refl |
| 134 |
from∘to (x ∷ xs) {ys = ys} (there .(⋎⁺₂ ys q)) | inj₂ q | refl = refl |
| 135 |
|
| 136 |
mutual |
| 137 |
|
| 138 |
to∘from₁ : ∀ {xs ys} (p : Any P xs) → |
| 139 |
⋎⁻ xs {ys = ys} (⋎⁺₁ p) ≡ inj₁ p |
| 140 |
to∘from₁ (here px) = refl |
| 141 |
to∘from₁ {ys = ys} (there p) |
| 142 |
rewrite to∘from₂ ys p = refl |
| 143 |
|
| 144 |
to∘from₂ : ∀ xs {ys} (p : Any P ys) → |
| 145 |
⋎⁻ xs (⋎⁺₂ xs p) ≡ inj₂ p |
| 146 |
to∘from₂ [] p = refl |
| 147 |
to∘from₂ (x ∷ xs) {ys = ys} p |
| 148 |
rewrite to∘from₁ {xs = ys} {ys = ♭ xs} p = refl |
| 149 |
|
| 150 |
to∘from : ∀ xs {ys} (p : Any P xs ⊎ Any P ys) → ⋎⁻ xs (⋎⁺ xs p) ≡ p |
| 151 |
to∘from xs = [ to∘from₁ , to∘from₂ xs ] |
| 152 |
|
| 153 |
------------------------------------------------------------------------ |
| 154 |
-- index |
| 155 |
|
| 156 |
-- The position returned by index is guaranteed to be within bounds. |
| 157 |
|
| 158 |
lookup-index : ∀ {xs} (p : Any P xs) → Is-just (lookup xs (index p)) |
| 159 |
lookup-index (here px) = just _ |
| 160 |
lookup-index (there p) = lookup-index p |
| 161 |
|
| 162 |
-- Any-resp preserves the index. |
| 163 |
|
| 164 |
index-Any-resp : ∀ {f : ∀ {x} → P x → Q x} {xs ys} |
| 165 |
(xs≈ys : xs ≈ ys) (p : Any P xs) → |
| 166 |
index (Any-resp f xs≈ys p) ≡ index p |
| 167 |
index-Any-resp (x ∷ xs≈) (here px) = refl |
| 168 |
index-Any-resp (x ∷ xs≈) (there p) = |
| 169 |
cong suc (index-Any-resp (♭ xs≈) p) |
| 170 |
|
| 171 |
-- The left-to-right direction of Any-⋎ returns a proof whose size is |
| 172 |
-- no larger than that of the input proof. |
| 173 |
|
| 174 |
index-Any-⋎ : ∀ xs {ys} (p : Any P (xs ⋎ ys)) → |
| 175 |
index p ≥′ [ index , index ]′ (Inverse.to (Any-⋎ xs) p) |
| 176 |
index-Any-⋎ [] p = ≤′-refl |
| 177 |
index-Any-⋎ (x ∷ xs) (here px) = ≤′-refl |
| 178 |
index-Any-⋎ (x ∷ xs) {ys = ys} (there p) |
| 179 |
with Inverse.to (Any-⋎ ys) p | index-Any-⋎ ys p |
| 180 |
... | inj₁ q | q≤p = ≤′-step q≤p |
| 181 |
... | inj₂ q | q≤p = s≤′s q≤p |