| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Sublist-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Sublist.Propositional.Properties where |
| 10 |
|
| 11 |
open import Data.List.Base using (List; []; _∷_; map) |
| 12 |
open import Data.List.Membership.Propositional using (_∈_) |
| 13 |
open import Data.List.Relation.Unary.All using (All; []; _∷_) |
| 14 |
open import Data.List.Relation.Unary.Any using (Any; here; there) |
| 15 |
open import Data.List.Relation.Unary.Any.Properties |
| 16 |
using (here-injective; there-injective) |
| 17 |
open import Data.List.Relation.Binary.Sublist.Propositional |
| 18 |
hiding (map) |
| 19 |
import Data.List.Relation.Binary.Sublist.Setoid |
| 20 |
as SetoidSublist |
| 21 |
import Data.List.Relation.Binary.Sublist.Setoid.Properties |
| 22 |
as SetoidProperties |
| 23 |
open import Data.Product.Base using (∃; _,_; proj₂) |
| 24 |
open import Function.Base using (id; _∘_; _∘′_) |
| 25 |
open import Level using (Level) |
| 26 |
open import Relation.Binary.Bundles using (Setoid) |
| 27 |
open import Relation.Binary.Definitions using (_Respects_) |
| 28 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 29 |
using (_≡_; refl; cong; _≗_; trans) |
| 30 |
open import Relation.Binary.PropositionalEquality.Properties |
| 31 |
using (setoid; subst-injective; trans-reflʳ; trans-assoc) |
| 32 |
open import Relation.Unary using (Pred) |
| 33 |
|
| 34 |
private |
| 35 |
variable |
| 36 |
a ℓ : Level |
| 37 |
A B : Set a |
| 38 |
x y : A |
| 39 |
ws xs ys zs : List A |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Re-exporting setoid properties |
| 43 |
|
| 44 |
module _ {A : Set a} where |
| 45 |
open SetoidProperties (setoid A) public |
| 46 |
hiding (map⁺; ⊆-trans-idˡ; ⊆-trans-idʳ; ⊆-trans-assoc) |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
-- Relationship between _⊆_ and Setoid._⊆_ |
| 50 |
------------------------------------------------------------------------ |
| 51 |
|
| 52 |
module _ (S : Setoid a ℓ) where |
| 53 |
|
| 54 |
open Setoid S using (reflexive) |
| 55 |
open SetoidSublist S using () renaming (_⊆_ to _⊆ₛ_) |
| 56 |
|
| 57 |
⊆⇒⊆ₛ : ∀ {as bs} → as ⊆ bs → as ⊆ₛ bs |
| 58 |
⊆⇒⊆ₛ = SetoidSublist.map (setoid _) reflexive |
| 59 |
|
| 60 |
------------------------------------------------------------------------ |
| 61 |
-- Functoriality of map |
| 62 |
|
| 63 |
map⁺ : (f : A → B) → xs ⊆ ys → map f xs ⊆ map f ys |
| 64 |
map⁺ f = SetoidProperties.map⁺ (setoid _) (setoid _) (cong f) |
| 65 |
|
| 66 |
------------------------------------------------------------------------ |
| 67 |
-- Category laws for _⊆_ |
| 68 |
|
| 69 |
⊆-trans-idˡ : ∀ {τ : xs ⊆ ys} → ⊆-trans ⊆-refl τ ≡ τ |
| 70 |
⊆-trans-idˡ {τ = τ} = SetoidProperties.⊆-trans-idˡ (setoid _) (λ _ → refl) τ |
| 71 |
|
| 72 |
⊆-trans-idʳ : ∀ {τ : xs ⊆ ys} → ⊆-trans τ ⊆-refl ≡ τ |
| 73 |
⊆-trans-idʳ {τ = τ} = SetoidProperties.⊆-trans-idʳ (setoid _) trans-reflʳ τ |
| 74 |
|
| 75 |
-- Note: The associativity law is oriented such that rewriting with it |
| 76 |
-- may trigger reductions of ⊆-trans, which matches first on its |
| 77 |
-- second argument and then on its first argument. |
| 78 |
|
| 79 |
⊆-trans-assoc : ∀ {τ₁ : ws ⊆ xs} {τ₂ : xs ⊆ ys} {τ₃ : ys ⊆ zs} → |
| 80 |
⊆-trans τ₁ (⊆-trans τ₂ τ₃) ≡ ⊆-trans (⊆-trans τ₁ τ₂) τ₃ |
| 81 |
⊆-trans-assoc {τ₁ = τ₁} {τ₂ = τ₂} {τ₃ = τ₃} = |
| 82 |
SetoidProperties.⊆-trans-assoc (setoid _) (λ p _ _ → ≡.sym (trans-assoc p)) τ₁ τ₂ τ₃ |
| 83 |
|
| 84 |
------------------------------------------------------------------------ |
| 85 |
-- Laws concerning ⊆-trans and ∷ˡ⁻ |
| 86 |
|
| 87 |
⊆-trans-∷ˡ⁻ᵣ : ∀ {τ : xs ⊆ ys} {σ : (y ∷ ys) ⊆ zs} → |
| 88 |
⊆-trans τ (∷ˡ⁻ σ) ≡ ⊆-trans (y ∷ʳ τ) σ |
| 89 |
⊆-trans-∷ˡ⁻ᵣ {σ = x ∷ σ} = refl |
| 90 |
⊆-trans-∷ˡ⁻ᵣ {σ = y ∷ʳ σ} = cong (y ∷ʳ_) ⊆-trans-∷ˡ⁻ᵣ |
| 91 |
|
| 92 |
⊆-trans-∷ˡ⁻ₗ : ∀ {τ : (x ∷ xs) ⊆ ys} {σ : ys ⊆ zs} → |
| 93 |
⊆-trans (∷ˡ⁻ τ) σ ≡ ∷ˡ⁻ (⊆-trans τ σ) |
| 94 |
⊆-trans-∷ˡ⁻ₗ {σ = y ∷ʳ σ} = cong (y ∷ʳ_) ⊆-trans-∷ˡ⁻ₗ |
| 95 |
⊆-trans-∷ˡ⁻ₗ {τ = y ∷ʳ τ} {σ = refl ∷ σ} = cong (y ∷ʳ_) ⊆-trans-∷ˡ⁻ₗ |
| 96 |
⊆-trans-∷ˡ⁻ₗ {τ = refl ∷ τ} {σ = refl ∷ σ} = refl |
| 97 |
|
| 98 |
⊆-∷ˡ⁻trans-∷ : ∀ {τ : xs ⊆ ys} {σ : (y ∷ ys) ⊆ zs} → |
| 99 |
∷ˡ⁻ (⊆-trans (refl ∷ τ) σ) ≡ ⊆-trans (y ∷ʳ τ) σ |
| 100 |
⊆-∷ˡ⁻trans-∷ {σ = y ∷ʳ σ} = cong (y ∷ʳ_) ⊆-∷ˡ⁻trans-∷ |
| 101 |
⊆-∷ˡ⁻trans-∷ {σ = refl ∷ σ} = refl |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- Relationships to other predicates |
| 105 |
|
| 106 |
-- All P is a contravariant functor from _⊆_ to Set. |
| 107 |
|
| 108 |
All-resp-⊆ : {P : Pred A ℓ} → (All P) Respects _⊇_ |
| 109 |
All-resp-⊆ [] [] = [] |
| 110 |
All-resp-⊆ (_ ∷ʳ p) (_ ∷ xs) = All-resp-⊆ p xs |
| 111 |
All-resp-⊆ (refl ∷ p) (x ∷ xs) = x ∷ All-resp-⊆ p xs |
| 112 |
|
| 113 |
-- Any P is a covariant functor from _⊆_ to Set. |
| 114 |
|
| 115 |
Any-resp-⊆ : {P : Pred A ℓ} → (Any P) Respects _⊆_ |
| 116 |
Any-resp-⊆ = lookup |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- Functor laws for All-resp-⊆ |
| 120 |
|
| 121 |
-- First functor law: identity. |
| 122 |
|
| 123 |
All-resp-⊆-refl : ∀ {P : Pred A ℓ} → |
| 124 |
All-resp-⊆ ⊆-refl ≗ id {A = All P xs} |
| 125 |
All-resp-⊆-refl [] = refl |
| 126 |
All-resp-⊆-refl (p ∷ ps) = cong (p ∷_) (All-resp-⊆-refl ps) |
| 127 |
|
| 128 |
-- Second functor law: composition. |
| 129 |
|
| 130 |
All-resp-⊆-trans : ∀ {P : Pred A ℓ} {τ : xs ⊆ ys} (τ′ : ys ⊆ zs) → |
| 131 |
All-resp-⊆ {P = P} (⊆-trans τ τ′) ≗ All-resp-⊆ τ ∘ All-resp-⊆ τ′ |
| 132 |
All-resp-⊆-trans (_ ∷ʳ τ′) (p ∷ ps) = All-resp-⊆-trans τ′ ps |
| 133 |
All-resp-⊆-trans {τ = _ ∷ʳ _ } (refl ∷ τ′) (p ∷ ps) = All-resp-⊆-trans τ′ ps |
| 134 |
All-resp-⊆-trans {τ = refl ∷ _} (refl ∷ τ′) (p ∷ ps) = cong (p ∷_) (All-resp-⊆-trans τ′ ps) |
| 135 |
All-resp-⊆-trans {τ = [] } ([] ) [] = refl |
| 136 |
|
| 137 |
------------------------------------------------------------------------ |
| 138 |
-- Functor laws for Any-resp-⊆ / lookup |
| 139 |
|
| 140 |
-- First functor law: identity. |
| 141 |
|
| 142 |
Any-resp-⊆-refl : ∀ {P : Pred A ℓ} → |
| 143 |
Any-resp-⊆ ⊆-refl ≗ id {A = Any P xs} |
| 144 |
Any-resp-⊆-refl (here p) = refl |
| 145 |
Any-resp-⊆-refl (there i) = cong there (Any-resp-⊆-refl i) |
| 146 |
|
| 147 |
lookup-⊆-refl = Any-resp-⊆-refl |
| 148 |
|
| 149 |
-- Second functor law: composition. |
| 150 |
|
| 151 |
Any-resp-⊆-trans : ∀ {P : Pred A ℓ} {τ : xs ⊆ ys} (τ′ : ys ⊆ zs) → |
| 152 |
Any-resp-⊆ {P = P} (⊆-trans τ τ′) ≗ Any-resp-⊆ τ′ ∘ Any-resp-⊆ τ |
| 153 |
Any-resp-⊆-trans (_ ∷ʳ τ′) i = cong there (Any-resp-⊆-trans τ′ i) |
| 154 |
Any-resp-⊆-trans {τ = _ ∷ʳ _} (_ ∷ τ′) i = cong there (Any-resp-⊆-trans τ′ i) |
| 155 |
Any-resp-⊆-trans {τ = _ ∷ _} (_ ∷ τ′) (there i) = cong there (Any-resp-⊆-trans τ′ i) |
| 156 |
Any-resp-⊆-trans {τ = refl ∷ _} (_ ∷ τ′) (here _) = refl |
| 157 |
Any-resp-⊆-trans {τ = [] } [] () |
| 158 |
|
| 159 |
lookup-⊆-trans = Any-resp-⊆-trans |
| 160 |
|
| 161 |
------------------------------------------------------------------------ |
| 162 |
-- The `lookup` function for `xs ⊆ ys` is injective. |
| 163 |
-- |
| 164 |
-- Note: `lookup` can be seen as a strictly increasing reindexing |
| 165 |
-- function for indices into `xs`, producing indices into `ys`. |
| 166 |
|
| 167 |
lookup-injective : ∀ {P : Pred A ℓ} {τ : xs ⊆ ys} {i j : Any P xs} → |
| 168 |
lookup τ i ≡ lookup τ j → i ≡ j |
| 169 |
lookup-injective {τ = _ ∷ʳ _} = lookup-injective ∘′ there-injective |
| 170 |
lookup-injective {τ = x≡y ∷ _} {here _} {here _} = cong here ∘′ subst-injective x≡y ∘′ here-injective |
| 171 |
-- Note: instead of using subst-injective, we could match x≡y against refl on the lhs. |
| 172 |
-- However, this turns the following clause into a non-strict match. |
| 173 |
lookup-injective {τ = _ ∷ _} {there _} {there _} = cong there ∘′ lookup-injective ∘′ there-injective |
| 174 |
|
| 175 |
------------------------------------------------------------------------ |
| 176 |
-- from∈ ∘ to∈ turns a sublist morphism τ : x∷xs ⊆ ys into a morphism |
| 177 |
-- [x] ⊆ ys. The same morphism is obtained by pre-composing τ with |
| 178 |
-- the canonial morphism [x] ⊆ x∷xs. |
| 179 |
-- |
| 180 |
-- Note: This lemma does not hold for Sublist.Setoid, but could hold for |
| 181 |
-- a hypothetical Sublist.Groupoid where trans refl = id. |
| 182 |
|
| 183 |
from∈∘to∈ : ∀ (τ : x ∷ xs ⊆ ys) → |
| 184 |
from∈ (to∈ τ) ≡ ⊆-trans (refl ∷ minimum xs) τ |
| 185 |
from∈∘to∈ (x≡y ∷ τ) = cong (x≡y ∷_) ([]⊆-irrelevant _ _) |
| 186 |
from∈∘to∈ (y ∷ʳ τ) = cong (y ∷ʳ_) (from∈∘to∈ τ) |
| 187 |
|
| 188 |
from∈∘lookup : ∀ (τ : xs ⊆ ys) (i : x ∈ xs) → |
| 189 |
from∈ (lookup τ i) ≡ ⊆-trans (from∈ i) τ |
| 190 |
from∈∘lookup (y ∷ʳ τ) i = cong (y ∷ʳ_) (from∈∘lookup τ i) |
| 191 |
from∈∘lookup (_ ∷ τ) (there i) = cong (_ ∷ʳ_) (from∈∘lookup τ i) |
| 192 |
from∈∘lookup (refl ∷ τ) (here refl) = cong (refl ∷_) ([]⊆-irrelevant _ _) |
| 193 |
|
| 194 |
------------------------------------------------------------------------ |
| 195 |
-- Weak pushout (wpo) |
| 196 |
|
| 197 |
-- A raw pushout is a weak pushout if the pushout square commutes. |
| 198 |
|
| 199 |
IsWeakPushout : ∀ {τ : xs ⊆ ys} {σ : xs ⊆ zs} → RawPushout τ σ → Set _ |
| 200 |
IsWeakPushout {τ = τ} {σ = σ} rpo = |
| 201 |
⊆-trans τ (RawPushout.leg₁ rpo) ≡ |
| 202 |
⊆-trans σ (RawPushout.leg₂ rpo) |
| 203 |
|
| 204 |
-- Joining two list extensions with ⊆-pushout produces a weak pushout. |
| 205 |
|
| 206 |
⊆-pushoutˡ-is-wpo : ∀ (τ : xs ⊆ ys) (σ : xs ⊆ zs) → |
| 207 |
IsWeakPushout (⊆-pushoutˡ τ σ) |
| 208 |
⊆-pushoutˡ-is-wpo [] σ |
| 209 |
rewrite ⊆-trans-idʳ {τ = σ} |
| 210 |
= ⊆-trans-idˡ {xs = []} |
| 211 |
⊆-pushoutˡ-is-wpo (y ∷ʳ τ) σ = cong (y ∷ʳ_) (⊆-pushoutˡ-is-wpo τ σ) |
| 212 |
⊆-pushoutˡ-is-wpo (x≡y ∷ τ) (z ∷ʳ σ) = cong (z ∷ʳ_) (⊆-pushoutˡ-is-wpo (x≡y ∷ τ) σ) |
| 213 |
⊆-pushoutˡ-is-wpo (refl ∷ τ) (refl ∷ σ) = cong (refl ∷_) (⊆-pushoutˡ-is-wpo τ σ) |
| 214 |
|
| 215 |
------------------------------------------------------------------------ |
| 216 |
-- Properties of disjointness |
| 217 |
|
| 218 |
-- From τ₁ ⊎ τ₂ = τ, compute the injection ι₁ such that τ₁ = ⊆-trans ι₁ τ. |
| 219 |
|
| 220 |
DisjointUnion-inj₁ : ∀ {xys : List A} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : xys ⊆ zs} → |
| 221 |
DisjointUnion τ₁ τ₂ τ → ∃ λ (ι₁ : xs ⊆ xys) → ⊆-trans ι₁ τ ≡ τ₁ |
| 222 |
DisjointUnion-inj₁ [] = [] , refl |
| 223 |
DisjointUnion-inj₁ (y ∷ₙ d) = _ , cong (y ∷ʳ_) (proj₂ (DisjointUnion-inj₁ d)) |
| 224 |
DisjointUnion-inj₁ (x≈y ∷ₗ d) = refl ∷ _ , cong (x≈y ∷_) (proj₂ (DisjointUnion-inj₁ d)) |
| 225 |
DisjointUnion-inj₁ (x≈y ∷ᵣ d) = _ ∷ʳ _ , cong (_ ∷ʳ_) (proj₂ (DisjointUnion-inj₁ d)) |
| 226 |
|
| 227 |
-- From τ₁ ⊎ τ₂ = τ, compute the injection ι₂ such that τ₂ = ⊆-trans ι₂ τ. |
| 228 |
|
| 229 |
DisjointUnion-inj₂ : ∀ {xys : List A} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : xys ⊆ zs} → |
| 230 |
DisjointUnion τ₁ τ₂ τ → ∃ λ (ι₂ : ys ⊆ xys) → ⊆-trans ι₂ τ ≡ τ₂ |
| 231 |
DisjointUnion-inj₂ [] = [] , refl |
| 232 |
DisjointUnion-inj₂ (y ∷ₙ d) = _ , cong (y ∷ʳ_) (proj₂ (DisjointUnion-inj₂ d)) |
| 233 |
DisjointUnion-inj₂ (x≈y ∷ᵣ d) = refl ∷ _ , cong (x≈y ∷_) (proj₂ (DisjointUnion-inj₂ d)) |
| 234 |
DisjointUnion-inj₂ (x≈y ∷ₗ d) = _ ∷ʳ _ , cong (_ ∷ʳ_) (proj₂ (DisjointUnion-inj₂ d)) |
| 235 |
|
| 236 |
-- A sublist σ disjoint to both τ₁ and τ₂ is an equalizer |
| 237 |
-- for the separators of τ₁ and τ₂. |
| 238 |
|
| 239 |
equalize-separators : ∀ {σ : ws ⊆ zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} (let s = separateˡ τ₁ τ₂) → |
| 240 |
Disjoint σ τ₁ → Disjoint σ τ₂ → |
| 241 |
⊆-trans σ (Separation.separator₁ s) ≡ |
| 242 |
⊆-trans σ (Separation.separator₂ s) |
| 243 |
equalize-separators [] [] = refl |
| 244 |
equalize-separators (y ∷ₙ d₁) (.y ∷ₙ d₂) = cong (y ∷ʳ_) (equalize-separators d₁ d₂) |
| 245 |
equalize-separators (y ∷ₙ d₁) (refl ∷ᵣ d₂) = cong (y ∷ʳ_) (equalize-separators d₁ d₂) |
| 246 |
equalize-separators (refl ∷ᵣ d₁) (y ∷ₙ d₂) = cong (y ∷ʳ_) (equalize-separators d₁ d₂) |
| 247 |
equalize-separators {τ₁ = refl ∷ _} {τ₂ = refl ∷ _} -- match here to work around deficiency of Agda's forcing translation |
| 248 |
(_ ∷ᵣ d₁) (_ ∷ᵣ d₂) = cong (_ ∷ʳ_) (cong (_ ∷ʳ_) (equalize-separators d₁ d₂)) |
| 249 |
equalize-separators (x≈y ∷ₗ d₁) (.x≈y ∷ₗ d₂) = cong (trans x≈y refl ∷_) (equalize-separators d₁ d₂) |