| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An inductive definition of the sublist relation with respect to a |
| 5 |
-- setoid. This is a generalisation of what is commonly known as Order |
| 6 |
-- Preserving Embeddings (OPE). |
| 7 |
------------------------------------------------------------------------ |
| 8 |
|
| 9 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 10 |
{-# OPTIONS --postfix-projections #-} |
| 11 |
|
| 12 |
open import Relation.Binary.Core using (Rel) |
| 13 |
open import Relation.Binary.Bundles using (Setoid) |
| 14 |
|
| 15 |
module Data.List.Relation.Binary.Sublist.Setoid |
| 16 |
{c ℓ} (S : Setoid c ℓ) where |
| 17 |
|
| 18 |
open import Level using (_⊔_) |
| 19 |
|
| 20 |
open import Data.List.Base using (List; []; _∷_) |
| 21 |
import Data.List.Relation.Binary.Equality.Setoid as SetoidEquality |
| 22 |
import Data.List.Relation.Binary.Sublist.Heterogeneous as Heterogeneous |
| 23 |
import Data.List.Relation.Binary.Sublist.Heterogeneous.Core |
| 24 |
as HeterogeneousCore |
| 25 |
import Data.List.Relation.Binary.Sublist.Heterogeneous.Properties |
| 26 |
as HeterogeneousProperties |
| 27 |
open import Data.Product.Base using (∃; ∃₂; _×_; _,_; proj₂) |
| 28 |
|
| 29 |
open import Relation.Binary.Core using (_⇒_) |
| 30 |
open import Relation.Binary.Bundles using (Preorder; Poset) |
| 31 |
open import Relation.Binary.Structures using (IsPreorder; IsPartialOrder) |
| 32 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 33 |
open import Relation.Nullary using (¬_; Dec; yes; no) |
| 34 |
|
| 35 |
open Setoid S renaming (Carrier to A) |
| 36 |
open SetoidEquality S |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Definition |
| 40 |
|
| 41 |
infix 4 _⊆_ _⊇_ _⊂_ _⊃_ _⊈_ _⊉_ _⊄_ _⊅_ |
| 42 |
|
| 43 |
_⊆_ : Rel (List A) (c ⊔ ℓ) |
| 44 |
_⊆_ = Heterogeneous.Sublist _≈_ |
| 45 |
|
| 46 |
_⊇_ : Rel (List A) (c ⊔ ℓ) |
| 47 |
xs ⊇ ys = ys ⊆ xs |
| 48 |
|
| 49 |
_⊂_ : Rel (List A) (c ⊔ ℓ) |
| 50 |
xs ⊂ ys = xs ⊆ ys × ¬ (xs ≋ ys) |
| 51 |
|
| 52 |
_⊃_ : Rel (List A) (c ⊔ ℓ) |
| 53 |
xs ⊃ ys = ys ⊂ xs |
| 54 |
|
| 55 |
_⊈_ : Rel (List A) (c ⊔ ℓ) |
| 56 |
xs ⊈ ys = ¬ (xs ⊆ ys) |
| 57 |
|
| 58 |
_⊉_ : Rel (List A) (c ⊔ ℓ) |
| 59 |
xs ⊉ ys = ¬ (xs ⊇ ys) |
| 60 |
|
| 61 |
_⊄_ : Rel (List A) (c ⊔ ℓ) |
| 62 |
xs ⊄ ys = ¬ (xs ⊂ ys) |
| 63 |
|
| 64 |
_⊅_ : Rel (List A) (c ⊔ ℓ) |
| 65 |
xs ⊅ ys = ¬ (xs ⊃ ys) |
| 66 |
|
| 67 |
------------------------------------------------------------------------ |
| 68 |
-- Re-export definitions and operations from heterogeneous sublists |
| 69 |
|
| 70 |
open HeterogeneousCore _≈_ public |
| 71 |
using ([]; _∷_; _∷ʳ_) |
| 72 |
|
| 73 |
open Heterogeneous {R = _≈_} public |
| 74 |
hiding (Sublist; []; _∷_; _∷ʳ_) |
| 75 |
renaming |
| 76 |
( toAny to to∈ |
| 77 |
; fromAny to from∈ |
| 78 |
) |
| 79 |
|
| 80 |
open Disjoint public |
| 81 |
using ([]) |
| 82 |
|
| 83 |
open DisjointUnion public |
| 84 |
using ([]) |
| 85 |
|
| 86 |
------------------------------------------------------------------------ |
| 87 |
-- Relational properties holding for Setoid case |
| 88 |
|
| 89 |
⊆-reflexive : _≋_ ⇒ _⊆_ |
| 90 |
⊆-reflexive = HeterogeneousProperties.fromPointwise |
| 91 |
|
| 92 |
open HeterogeneousProperties.Reflexivity {R = _≈_} refl public using () |
| 93 |
renaming (refl to ⊆-refl) -- ⊆-refl : Reflexive _⊆_ |
| 94 |
|
| 95 |
open HeterogeneousProperties.Transitivity {R = _≈_} {S = _≈_} {T = _≈_} trans public using () |
| 96 |
renaming (trans to ⊆-trans) -- ⊆-trans : Transitive _⊆_ |
| 97 |
|
| 98 |
open HeterogeneousProperties.Antisymmetry {R = _≈_} {S = _≈_} (λ x≈y _ → x≈y) public using () |
| 99 |
renaming (antisym to ⊆-antisym) -- ⊆-antisym : Antisymmetric _≋_ _⊆_ |
| 100 |
|
| 101 |
⊆-isPreorder : IsPreorder _≋_ _⊆_ |
| 102 |
⊆-isPreorder = record |
| 103 |
{ isEquivalence = ≋-isEquivalence |
| 104 |
; reflexive = ⊆-reflexive |
| 105 |
; trans = ⊆-trans |
| 106 |
} |
| 107 |
|
| 108 |
⊆-isPartialOrder : IsPartialOrder _≋_ _⊆_ |
| 109 |
⊆-isPartialOrder = record |
| 110 |
{ isPreorder = ⊆-isPreorder |
| 111 |
; antisym = ⊆-antisym |
| 112 |
} |
| 113 |
|
| 114 |
⊆-preorder : Preorder c (c ⊔ ℓ) (c ⊔ ℓ) |
| 115 |
⊆-preorder = record |
| 116 |
{ isPreorder = ⊆-isPreorder |
| 117 |
} |
| 118 |
|
| 119 |
⊆-poset : Poset c (c ⊔ ℓ) (c ⊔ ℓ) |
| 120 |
⊆-poset = record |
| 121 |
{ isPartialOrder = ⊆-isPartialOrder |
| 122 |
} |
| 123 |
|
| 124 |
------------------------------------------------------------------------ |
| 125 |
-- Raw pushout |
| 126 |
-- |
| 127 |
-- The category _⊆_ does not have proper pushouts. For instance consider: |
| 128 |
-- |
| 129 |
-- τᵤ : [] ⊆ (u ∷ []) |
| 130 |
-- τᵥ : [] ⊆ (v ∷ []) |
| 131 |
-- |
| 132 |
-- Then, there are two unrelated upper bounds (u ∷ v ∷ []) and (v ∷ u ∷ []), |
| 133 |
-- since _⊆_ does not include permutations. |
| 134 |
-- |
| 135 |
-- Even though there are no unique least upper bounds, we can merge two |
| 136 |
-- extensions of a list, producing a minimial superlist of both. |
| 137 |
-- |
| 138 |
-- For the example, the left-biased merge would produce the pair: |
| 139 |
-- |
| 140 |
-- τᵤ′ : (u ∷ []) ⊆ (u ∷ v ∷ []) |
| 141 |
-- τᵥ′ : (v ∷ []) ⊆ (u ∷ v ∷ []) |
| 142 |
-- |
| 143 |
-- We call such a pair a raw pushout. It is then a weak pushout if the |
| 144 |
-- resulting square commutes, i.e.: |
| 145 |
-- |
| 146 |
-- ⊆-trans τᵤ τᵤ′ ~ ⊆-trans τᵥ τᵥ′ |
| 147 |
-- |
| 148 |
-- This requires a notion of equality _~_ on sublist morphisms. |
| 149 |
-- |
| 150 |
-- Further, commutation requires a similar commutation property |
| 151 |
-- for the underlying equality _≈_, namely |
| 152 |
-- |
| 153 |
-- trans x≈y (sym x≈y) == trans x≈z (sym x≈z) |
| 154 |
-- |
| 155 |
-- for some notion of equality _==_ for equality proofs _≈_. |
| 156 |
-- Such a property is given e.g. if _≈_ is proof irrelevant |
| 157 |
-- or forms a groupoid. |
| 158 |
|
| 159 |
record RawPushout {xs ys zs : List A} (τ : xs ⊆ ys) (σ : xs ⊆ zs) : Set (c ⊔ ℓ) where |
| 160 |
field |
| 161 |
{upperBound} : List A |
| 162 |
leg₁ : ys ⊆ upperBound |
| 163 |
leg₂ : zs ⊆ upperBound |
| 164 |
|
| 165 |
open RawPushout using (leg₁; leg₂) |
| 166 |
|
| 167 |
------------------------------------------------------------------------ |
| 168 |
-- Extending corners of a raw pushout square |
| 169 |
|
| 170 |
-- Extending the right upper corner. |
| 171 |
|
| 172 |
infixr 5 _∷ʳ₁_ _∷ʳ₂_ |
| 173 |
|
| 174 |
_∷ʳ₁_ : ∀ {xs ys zs : List A} {τ : xs ⊆ ys} {σ : xs ⊆ zs} → |
| 175 |
(y : A) → RawPushout τ σ → RawPushout (y ∷ʳ τ) σ |
| 176 |
y ∷ʳ₁ rpo = record |
| 177 |
{ leg₁ = refl ∷ leg₁ rpo |
| 178 |
; leg₂ = y ∷ʳ leg₂ rpo |
| 179 |
} |
| 180 |
|
| 181 |
-- Extending the left lower corner. |
| 182 |
|
| 183 |
_∷ʳ₂_ : ∀ {xs ys zs : List A} {τ : xs ⊆ ys} {σ : xs ⊆ zs} → |
| 184 |
(z : A) → RawPushout τ σ → RawPushout τ (z ∷ʳ σ) |
| 185 |
z ∷ʳ₂ rpo = record |
| 186 |
{ leg₁ = z ∷ʳ leg₁ rpo |
| 187 |
; leg₂ = refl ∷ leg₂ rpo |
| 188 |
} |
| 189 |
|
| 190 |
-- Extending both of these corners with equal elements. |
| 191 |
|
| 192 |
∷-rpo : ∀ {x y z : A} {xs ys zs : List A} {τ : xs ⊆ ys} {σ : xs ⊆ zs} → |
| 193 |
(x≈y : x ≈ y) (x≈z : x ≈ z) → RawPushout τ σ → RawPushout (x≈y ∷ τ) (x≈z ∷ σ) |
| 194 |
∷-rpo x≈y x≈z rpo = record |
| 195 |
{ leg₁ = sym x≈y ∷ leg₁ rpo |
| 196 |
; leg₂ = sym x≈z ∷ leg₂ rpo |
| 197 |
} |
| 198 |
|
| 199 |
------------------------------------------------------------------------ |
| 200 |
-- Left-biased pushout: add elements of left extension first. |
| 201 |
|
| 202 |
⊆-pushoutˡ : ∀ {xs ys zs : List A} → |
| 203 |
(τ : xs ⊆ ys) (σ : xs ⊆ zs) → RawPushout τ σ |
| 204 |
⊆-pushoutˡ [] σ = record { leg₁ = σ ; leg₂ = ⊆-refl } |
| 205 |
⊆-pushoutˡ (y ∷ʳ τ) σ = y ∷ʳ₁ ⊆-pushoutˡ τ σ |
| 206 |
⊆-pushoutˡ τ@(_ ∷ _) (z ∷ʳ σ) = z ∷ʳ₂ ⊆-pushoutˡ τ σ |
| 207 |
⊆-pushoutˡ (x≈y ∷ τ) (x≈z ∷ σ) = ∷-rpo x≈y x≈z (⊆-pushoutˡ τ σ) |
| 208 |
|
| 209 |
-- Join two extensions, returning the upper bound and the diagonal |
| 210 |
-- of the pushout square. |
| 211 |
|
| 212 |
⊆-joinˡ : ∀ {xs ys zs : List A} → |
| 213 |
(τ : xs ⊆ ys) (σ : xs ⊆ zs) → ∃ λ us → xs ⊆ us |
| 214 |
⊆-joinˡ τ σ = RawPushout.upperBound rpo , ⊆-trans τ (leg₁ rpo) |
| 215 |
where |
| 216 |
rpo = ⊆-pushoutˡ τ σ |
| 217 |
|
| 218 |
|
| 219 |
------------------------------------------------------------------------ |
| 220 |
-- Upper bound of two sublists xs,ys ⊆ zs |
| 221 |
-- |
| 222 |
-- Two sublists τ : xs ⊆ zs and σ : ys ⊆ zs |
| 223 |
-- can be joined in a unique way if τ and σ are respected. |
| 224 |
-- |
| 225 |
-- For instance, if τ : [x] ⊆ [x,y,x] and σ : [y] ⊆ [x,y,x] |
| 226 |
-- then the union will be [x,y] or [y,x], depending on whether |
| 227 |
-- τ picks the first x or the second one. |
| 228 |
-- |
| 229 |
-- NB: If the content of τ and σ were ignored then the union would not |
| 230 |
-- be unique. Expressing uniqueness would require a notion of equality |
| 231 |
-- of sublist proofs, which we do not (yet) have for the setoid case |
| 232 |
-- (however, for the propositional case). |
| 233 |
|
| 234 |
record UpperBound {xs ys zs} (τ : xs ⊆ zs) (σ : ys ⊆ zs) : Set (c ⊔ ℓ) where |
| 235 |
field |
| 236 |
{theUpperBound} : List A |
| 237 |
sub : theUpperBound ⊆ zs |
| 238 |
inj₁ : xs ⊆ theUpperBound |
| 239 |
inj₂ : ys ⊆ theUpperBound |
| 240 |
|
| 241 |
open UpperBound |
| 242 |
|
| 243 |
infixr 5 _∷ₗ-ub_ _∷ᵣ-ub_ |
| 244 |
|
| 245 |
∷ₙ-ub : ∀ {xs ys zs} {τ : xs ⊆ zs} {σ : ys ⊆ zs} {x} → |
| 246 |
UpperBound τ σ → UpperBound (x ∷ʳ τ) (x ∷ʳ σ) |
| 247 |
∷ₙ-ub u = record |
| 248 |
{ sub = _ ∷ʳ u .sub |
| 249 |
; inj₁ = u .inj₁ |
| 250 |
; inj₂ = u .inj₂ |
| 251 |
} |
| 252 |
|
| 253 |
_∷ₗ-ub_ : ∀ {xs ys zs} {τ : xs ⊆ zs} {σ : ys ⊆ zs} {x y} → |
| 254 |
(x≈y : x ≈ y) → UpperBound τ σ → UpperBound (x≈y ∷ τ) (y ∷ʳ σ) |
| 255 |
x≈y ∷ₗ-ub u = record |
| 256 |
{ sub = refl ∷ u .sub |
| 257 |
; inj₁ = x≈y ∷ u .inj₁ |
| 258 |
; inj₂ = _ ∷ʳ u .inj₂ |
| 259 |
} |
| 260 |
|
| 261 |
_∷ᵣ-ub_ : ∀ {xs ys zs} {τ : xs ⊆ zs} {σ : ys ⊆ zs} {x y} → |
| 262 |
(x≈y : x ≈ y) → UpperBound τ σ → UpperBound (y ∷ʳ τ) (x≈y ∷ σ) |
| 263 |
x≈y ∷ᵣ-ub u = record |
| 264 |
{ sub = refl ∷ u .sub |
| 265 |
; inj₁ = _ ∷ʳ u .inj₁ |
| 266 |
; inj₂ = x≈y ∷ u .inj₂ |
| 267 |
} |
| 268 |
|
| 269 |
_,_∷-ub_ : ∀ {xs ys zs} {τ : xs ⊆ zs} {σ : ys ⊆ zs} {x y z} → |
| 270 |
(x≈z : x ≈ z) (y≈z : y ≈ z) → UpperBound τ σ → UpperBound (x≈z ∷ τ) (y≈z ∷ σ) |
| 271 |
x≈z , y≈z ∷-ub u = record |
| 272 |
{ sub = refl ∷ u .sub |
| 273 |
; inj₁ = x≈z ∷ u .inj₁ |
| 274 |
; inj₂ = y≈z ∷ u .inj₂ |
| 275 |
} |
| 276 |
|
| 277 |
⊆-upper-bound : ∀ {xs ys zs} (τ : xs ⊆ zs) (σ : ys ⊆ zs) → UpperBound τ σ |
| 278 |
⊆-upper-bound [] [] = record { sub = [] ; inj₁ = [] ; inj₂ = [] } |
| 279 |
⊆-upper-bound (y ∷ʳ τ) (.y ∷ʳ σ) = ∷ₙ-ub (⊆-upper-bound τ σ) |
| 280 |
⊆-upper-bound (y ∷ʳ τ) (x≈y ∷ σ) = x≈y ∷ᵣ-ub ⊆-upper-bound τ σ |
| 281 |
⊆-upper-bound (x≈y ∷ τ) (y ∷ʳ σ) = x≈y ∷ₗ-ub ⊆-upper-bound τ σ |
| 282 |
⊆-upper-bound (x≈z ∷ τ) (y≈z ∷ σ) = x≈z , y≈z ∷-ub ⊆-upper-bound τ σ |
| 283 |
|
| 284 |
------------------------------------------------------------------------ |
| 285 |
-- Disjoint union |
| 286 |
-- |
| 287 |
-- Upper bound of two non-overlapping sublists. |
| 288 |
|
| 289 |
⊆-disjoint-union : ∀ {xs ys zs} {τ : xs ⊆ zs} {σ : ys ⊆ zs} → |
| 290 |
Disjoint τ σ → UpperBound τ σ |
| 291 |
⊆-disjoint-union {τ = τ} {σ = σ} _ = ⊆-upper-bound τ σ |