| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Sums of binary relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Sum.Relation.Binary.LeftOrder where |
| 10 |
|
| 11 |
open import Data.Sum.Base as Sum using (inj₁; inj₂; _⊎_) |
| 12 |
open import Data.Sum.Relation.Binary.Pointwise as PW |
| 13 |
using (Pointwise; inj₁; inj₂) |
| 14 |
open import Data.Product.Base using (_,_) |
| 15 |
open import Data.Empty using (⊥) |
| 16 |
open import Function.Base using (_$_; _∘_) |
| 17 |
open import Induction.WellFounded |
| 18 |
open import Level using (Level; _⊔_) |
| 19 |
open import Relation.Nullary.Negation.Core using (¬_) |
| 20 |
open import Relation.Nullary.Decidable.Core using (yes; no) |
| 21 |
import Relation.Nullary.Decidable as Dec using (Dec; map; map′) |
| 22 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 23 |
open import Relation.Binary.Bundles |
| 24 |
using (Preorder; Poset; StrictPartialOrder; TotalOrder; DecTotalOrder |
| 25 |
; StrictTotalOrder) |
| 26 |
open import Relation.Binary.Structures |
| 27 |
using (IsPreorder; IsPartialOrder; IsStrictPartialOrder; IsTotalOrder |
| 28 |
; IsDecTotalOrder; IsStrictTotalOrder) |
| 29 |
open import Relation.Binary.Definitions |
| 30 |
using (Reflexive; Transitive; Asymmetric; Total; Decidable; Irreflexive |
| 31 |
; Antisymmetric; Trichotomous; _Respectsʳ_; _Respectsˡ_; _Respects₂_ |
| 32 |
; tri<; tri>; tri≈) |
| 33 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_) |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Definition |
| 37 |
|
| 38 |
infixr 1 _⊎-<_ |
| 39 |
|
| 40 |
data _⊎-<_ {a₁ a₂} {A₁ : Set a₁} {A₂ : Set a₂} |
| 41 |
{ℓ₁ ℓ₂} (_∼₁_ : Rel A₁ ℓ₁) (_∼₂_ : Rel A₂ ℓ₂) : |
| 42 |
Rel (A₁ ⊎ A₂) (a₁ ⊔ a₂ ⊔ ℓ₁ ⊔ ℓ₂) where |
| 43 |
₁∼₂ : ∀ {x y} → (_∼₁_ ⊎-< _∼₂_) (inj₁ x) (inj₂ y) |
| 44 |
₁∼₁ : ∀ {x y} (x∼₁y : x ∼₁ y) → (_∼₁_ ⊎-< _∼₂_) (inj₁ x) (inj₁ y) |
| 45 |
₂∼₂ : ∀ {x y} (x∼₂y : x ∼₂ y) → (_∼₁_ ⊎-< _∼₂_) (inj₂ x) (inj₂ y) |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Some properties which are preserved by _⊎-<_ |
| 49 |
|
| 50 |
module _ {a₁ a₂} {A₁ : Set a₁} {A₂ : Set a₂} |
| 51 |
{ℓ₁ ℓ₂} {∼₁ : Rel A₁ ℓ₁} {∼₂ : Rel A₂ ℓ₂} |
| 52 |
where |
| 53 |
|
| 54 |
drop-inj₁ : ∀ {x y} → (∼₁ ⊎-< ∼₂) (inj₁ x) (inj₁ y) → ∼₁ x y |
| 55 |
drop-inj₁ (₁∼₁ x∼₁y) = x∼₁y |
| 56 |
|
| 57 |
drop-inj₂ : ∀ {x y} → (∼₁ ⊎-< ∼₂) (inj₂ x) (inj₂ y) → ∼₂ x y |
| 58 |
drop-inj₂ (₂∼₂ x∼₂y) = x∼₂y |
| 59 |
|
| 60 |
⊎-<-refl : Reflexive ∼₁ → Reflexive ∼₂ → |
| 61 |
Reflexive (∼₁ ⊎-< ∼₂) |
| 62 |
⊎-<-refl refl₁ refl₂ {inj₁ x} = ₁∼₁ refl₁ |
| 63 |
⊎-<-refl refl₁ refl₂ {inj₂ y} = ₂∼₂ refl₂ |
| 64 |
|
| 65 |
⊎-<-transitive : Transitive ∼₁ → Transitive ∼₂ → |
| 66 |
Transitive (∼₁ ⊎-< ∼₂) |
| 67 |
⊎-<-transitive trans₁ trans₂ ₁∼₂ (₂∼₂ x∼₂y) = ₁∼₂ |
| 68 |
⊎-<-transitive trans₁ trans₂ (₁∼₁ x∼₁y) ₁∼₂ = ₁∼₂ |
| 69 |
⊎-<-transitive trans₁ trans₂ (₁∼₁ x∼₁y) (₁∼₁ x∼₁y₁) = ₁∼₁ (trans₁ x∼₁y x∼₁y₁) |
| 70 |
⊎-<-transitive trans₁ trans₂ (₂∼₂ x∼₂y) (₂∼₂ x∼₂y₁) = ₂∼₂ (trans₂ x∼₂y x∼₂y₁) |
| 71 |
|
| 72 |
⊎-<-asymmetric : Asymmetric ∼₁ → Asymmetric ∼₂ → |
| 73 |
Asymmetric (∼₁ ⊎-< ∼₂) |
| 74 |
⊎-<-asymmetric asym₁ asym₂ (₁∼₁ x∼₁y) (₁∼₁ x∼₁y₁) = asym₁ x∼₁y x∼₁y₁ |
| 75 |
⊎-<-asymmetric asym₁ asym₂ (₂∼₂ x∼₂y) (₂∼₂ x∼₂y₁) = asym₂ x∼₂y x∼₂y₁ |
| 76 |
|
| 77 |
⊎-<-total : Total ∼₁ → Total ∼₂ → Total (∼₁ ⊎-< ∼₂) |
| 78 |
⊎-<-total total₁ total₂ = total |
| 79 |
where |
| 80 |
total : Total (_ ⊎-< _) |
| 81 |
total (inj₁ x) (inj₁ y) = Sum.map ₁∼₁ ₁∼₁ $ total₁ x y |
| 82 |
total (inj₁ x) (inj₂ y) = inj₁ ₁∼₂ |
| 83 |
total (inj₂ x) (inj₁ y) = inj₂ ₁∼₂ |
| 84 |
total (inj₂ x) (inj₂ y) = Sum.map ₂∼₂ ₂∼₂ $ total₂ x y |
| 85 |
|
| 86 |
⊎-<-decidable : Decidable ∼₁ → Decidable ∼₂ → |
| 87 |
Decidable (∼₁ ⊎-< ∼₂) |
| 88 |
⊎-<-decidable dec₁ dec₂ (inj₁ x) (inj₁ y) = Dec.map′ ₁∼₁ drop-inj₁ (dec₁ x y) |
| 89 |
⊎-<-decidable dec₁ dec₂ (inj₁ x) (inj₂ y) = yes ₁∼₂ |
| 90 |
⊎-<-decidable dec₁ dec₂ (inj₂ x) (inj₁ y) = no λ() |
| 91 |
⊎-<-decidable dec₁ dec₂ (inj₂ x) (inj₂ y) = Dec.map′ ₂∼₂ drop-inj₂ (dec₂ x y) |
| 92 |
|
| 93 |
⊎-<-wellFounded : WellFounded ∼₁ → WellFounded ∼₂ → WellFounded (∼₁ ⊎-< ∼₂) |
| 94 |
⊎-<-wellFounded wf₁ wf₂ x = acc (⊎-<-acc x) |
| 95 |
where |
| 96 |
⊎-<-acc₁ : ∀ {x} → Acc ∼₁ x → WfRec (∼₁ ⊎-< ∼₂) (Acc (∼₁ ⊎-< ∼₂)) (inj₁ x) |
| 97 |
⊎-<-acc₁ (acc rec) (₁∼₁ x∼₁y) = acc (⊎-<-acc₁ (rec x∼₁y)) |
| 98 |
|
| 99 |
⊎-<-acc₂ : ∀ {x} → Acc ∼₂ x → WfRec (∼₁ ⊎-< ∼₂) (Acc (∼₁ ⊎-< ∼₂)) (inj₂ x) |
| 100 |
⊎-<-acc₂ (acc rec) {inj₁ x} ₁∼₂ = acc (⊎-<-acc₁ (wf₁ x)) |
| 101 |
⊎-<-acc₂ (acc rec) (₂∼₂ x∼₂y) = acc (⊎-<-acc₂ (rec x∼₂y)) |
| 102 |
|
| 103 |
⊎-<-acc : ∀ x → WfRec (∼₁ ⊎-< ∼₂) (Acc (∼₁ ⊎-< ∼₂)) x |
| 104 |
⊎-<-acc (inj₁ x) = ⊎-<-acc₁ (wf₁ x) |
| 105 |
⊎-<-acc (inj₂ x) = ⊎-<-acc₂ (wf₂ x) |
| 106 |
|
| 107 |
module _ {a₁ a₂} {A₁ : Set a₁} {A₂ : Set a₂} |
| 108 |
{ℓ₁ ℓ₂} {∼₁ : Rel A₁ ℓ₁} {≈₁ : Rel A₁ ℓ₂} |
| 109 |
{ℓ₃ ℓ₄} {∼₂ : Rel A₂ ℓ₃} {≈₂ : Rel A₂ ℓ₄} |
| 110 |
where |
| 111 |
|
| 112 |
⊎-<-reflexive : ≈₁ ⇒ ∼₁ → ≈₂ ⇒ ∼₂ → |
| 113 |
(Pointwise ≈₁ ≈₂) ⇒ (∼₁ ⊎-< ∼₂) |
| 114 |
⊎-<-reflexive refl₁ refl₂ (inj₁ x) = ₁∼₁ (refl₁ x) |
| 115 |
⊎-<-reflexive refl₁ refl₂ (inj₂ x) = ₂∼₂ (refl₂ x) |
| 116 |
|
| 117 |
⊎-<-irreflexive : Irreflexive ≈₁ ∼₁ → Irreflexive ≈₂ ∼₂ → |
| 118 |
Irreflexive (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 119 |
⊎-<-irreflexive irrefl₁ irrefl₂ (inj₁ x) (₁∼₁ x∼₁y) = irrefl₁ x x∼₁y |
| 120 |
⊎-<-irreflexive irrefl₁ irrefl₂ (inj₂ x) (₂∼₂ x∼₂y) = irrefl₂ x x∼₂y |
| 121 |
|
| 122 |
⊎-<-antisymmetric : Antisymmetric ≈₁ ∼₁ → Antisymmetric ≈₂ ∼₂ → |
| 123 |
Antisymmetric (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 124 |
⊎-<-antisymmetric antisym₁ antisym₂ (₁∼₁ x∼₁y) (₁∼₁ x∼₁y₁) = inj₁ (antisym₁ x∼₁y x∼₁y₁) |
| 125 |
⊎-<-antisymmetric antisym₁ antisym₂ (₂∼₂ x∼₂y) (₂∼₂ x∼₂y₁) = inj₂ (antisym₂ x∼₂y x∼₂y₁) |
| 126 |
|
| 127 |
⊎-<-respectsʳ : ∼₁ Respectsʳ ≈₁ → ∼₂ Respectsʳ ≈₂ → |
| 128 |
(∼₁ ⊎-< ∼₂) Respectsʳ (Pointwise ≈₁ ≈₂) |
| 129 |
⊎-<-respectsʳ resp₁ resp₂ (inj₁ x₁) (₁∼₁ x∼₁y) = ₁∼₁ (resp₁ x₁ x∼₁y) |
| 130 |
⊎-<-respectsʳ resp₁ resp₂ (inj₂ x₁) ₁∼₂ = ₁∼₂ |
| 131 |
⊎-<-respectsʳ resp₁ resp₂ (inj₂ x₁) (₂∼₂ x∼₂y) = ₂∼₂ (resp₂ x₁ x∼₂y) |
| 132 |
|
| 133 |
⊎-<-respectsˡ : ∼₁ Respectsˡ ≈₁ → ∼₂ Respectsˡ ≈₂ → |
| 134 |
(∼₁ ⊎-< ∼₂) Respectsˡ (Pointwise ≈₁ ≈₂) |
| 135 |
⊎-<-respectsˡ resp₁ resp₂ (inj₁ x) ₁∼₂ = ₁∼₂ |
| 136 |
⊎-<-respectsˡ resp₁ resp₂ (inj₁ x) (₁∼₁ x∼₁y) = ₁∼₁ (resp₁ x x∼₁y) |
| 137 |
⊎-<-respectsˡ resp₁ resp₂ (inj₂ x) (₂∼₂ x∼₂y) = ₂∼₂ (resp₂ x x∼₂y) |
| 138 |
|
| 139 |
⊎-<-respects₂ : ∼₁ Respects₂ ≈₁ → ∼₂ Respects₂ ≈₂ → |
| 140 |
(∼₁ ⊎-< ∼₂) Respects₂ (Pointwise ≈₁ ≈₂) |
| 141 |
⊎-<-respects₂ (r₁ , l₁) (r₂ , l₂) = ⊎-<-respectsʳ r₁ r₂ , ⊎-<-respectsˡ l₁ l₂ |
| 142 |
|
| 143 |
⊎-<-trichotomous : Trichotomous ≈₁ ∼₁ → Trichotomous ≈₂ ∼₂ → |
| 144 |
Trichotomous (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 145 |
⊎-<-trichotomous tri₁ tri₂ (inj₁ x) (inj₂ y) = tri< ₁∼₂ (λ()) (λ()) |
| 146 |
⊎-<-trichotomous tri₁ tri₂ (inj₂ x) (inj₁ y) = tri> (λ()) (λ()) ₁∼₂ |
| 147 |
⊎-<-trichotomous tri₁ tri₂ (inj₁ x) (inj₁ y) with tri₁ x y |
| 148 |
... | tri< x<y x≉y x≯y = tri< (₁∼₁ x<y) (x≉y ∘ PW.drop-inj₁) (x≯y ∘ drop-inj₁) |
| 149 |
... | tri≈ x≮y x≈y x≯y = tri≈ (x≮y ∘ drop-inj₁) (inj₁ x≈y) (x≯y ∘ drop-inj₁) |
| 150 |
... | tri> x≮y x≉y x>y = tri> (x≮y ∘ drop-inj₁) (x≉y ∘ PW.drop-inj₁) (₁∼₁ x>y) |
| 151 |
⊎-<-trichotomous tri₁ tri₂ (inj₂ x) (inj₂ y) with tri₂ x y |
| 152 |
... | tri< x<y x≉y x≯y = tri< (₂∼₂ x<y) (x≉y ∘ PW.drop-inj₂) (x≯y ∘ drop-inj₂) |
| 153 |
... | tri≈ x≮y x≈y x≯y = tri≈ (x≮y ∘ drop-inj₂) (inj₂ x≈y) (x≯y ∘ drop-inj₂) |
| 154 |
... | tri> x≮y x≉y x>y = tri> (x≮y ∘ drop-inj₂) (x≉y ∘ PW.drop-inj₂) (₂∼₂ x>y) |
| 155 |
|
| 156 |
------------------------------------------------------------------------ |
| 157 |
-- Some collections of properties which are preserved |
| 158 |
|
| 159 |
module _ {a₁ a₂} {A₁ : Set a₁} {A₂ : Set a₂} |
| 160 |
{ℓ₁ ℓ₂} {≈₁ : Rel A₁ ℓ₁} {∼₁ : Rel A₁ ℓ₂} |
| 161 |
{ℓ₃ ℓ₄} {≈₂ : Rel A₂ ℓ₃} {∼₂ : Rel A₂ ℓ₄} where |
| 162 |
|
| 163 |
⊎-<-isPreorder : IsPreorder ≈₁ ∼₁ → IsPreorder ≈₂ ∼₂ → |
| 164 |
IsPreorder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 165 |
⊎-<-isPreorder pre₁ pre₂ = record |
| 166 |
{ isEquivalence = PW.⊎-isEquivalence (isEquivalence pre₁) (isEquivalence pre₂) |
| 167 |
; reflexive = ⊎-<-reflexive (reflexive pre₁) (reflexive pre₂) |
| 168 |
; trans = ⊎-<-transitive (trans pre₁) (trans pre₂) |
| 169 |
} |
| 170 |
where open IsPreorder |
| 171 |
|
| 172 |
⊎-<-isPartialOrder : IsPartialOrder ≈₁ ∼₁ → |
| 173 |
IsPartialOrder ≈₂ ∼₂ → |
| 174 |
IsPartialOrder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 175 |
⊎-<-isPartialOrder po₁ po₂ = record |
| 176 |
{ isPreorder = ⊎-<-isPreorder (isPreorder po₁) (isPreorder po₂) |
| 177 |
; antisym = ⊎-<-antisymmetric (antisym po₁) (antisym po₂) |
| 178 |
} |
| 179 |
where open IsPartialOrder |
| 180 |
|
| 181 |
⊎-<-isStrictPartialOrder : IsStrictPartialOrder ≈₁ ∼₁ → |
| 182 |
IsStrictPartialOrder ≈₂ ∼₂ → |
| 183 |
IsStrictPartialOrder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 184 |
⊎-<-isStrictPartialOrder spo₁ spo₂ = record |
| 185 |
{ isEquivalence = PW.⊎-isEquivalence (isEquivalence spo₁) (isEquivalence spo₂) |
| 186 |
; irrefl = ⊎-<-irreflexive (irrefl spo₁) (irrefl spo₂) |
| 187 |
; trans = ⊎-<-transitive (trans spo₁) (trans spo₂) |
| 188 |
; <-resp-≈ = ⊎-<-respects₂ (<-resp-≈ spo₁) (<-resp-≈ spo₂) |
| 189 |
} |
| 190 |
where open IsStrictPartialOrder |
| 191 |
|
| 192 |
⊎-<-isTotalOrder : IsTotalOrder ≈₁ ∼₁ → |
| 193 |
IsTotalOrder ≈₂ ∼₂ → |
| 194 |
IsTotalOrder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 195 |
⊎-<-isTotalOrder to₁ to₂ = record |
| 196 |
{ isPartialOrder = ⊎-<-isPartialOrder (isPartialOrder to₁) (isPartialOrder to₂) |
| 197 |
; total = ⊎-<-total (total to₁) (total to₂) |
| 198 |
} |
| 199 |
where open IsTotalOrder |
| 200 |
|
| 201 |
⊎-<-isDecTotalOrder : IsDecTotalOrder ≈₁ ∼₁ → |
| 202 |
IsDecTotalOrder ≈₂ ∼₂ → |
| 203 |
IsDecTotalOrder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 204 |
⊎-<-isDecTotalOrder to₁ to₂ = record |
| 205 |
{ isTotalOrder = ⊎-<-isTotalOrder (isTotalOrder to₁) (isTotalOrder to₂) |
| 206 |
; _≟_ = PW.⊎-decidable (_≟_ to₁) (_≟_ to₂) |
| 207 |
; _≤?_ = ⊎-<-decidable (_≤?_ to₁) (_≤?_ to₂) |
| 208 |
} |
| 209 |
where open IsDecTotalOrder |
| 210 |
|
| 211 |
⊎-<-isStrictTotalOrder : IsStrictTotalOrder ≈₁ ∼₁ → |
| 212 |
IsStrictTotalOrder ≈₂ ∼₂ → |
| 213 |
IsStrictTotalOrder (Pointwise ≈₁ ≈₂) (∼₁ ⊎-< ∼₂) |
| 214 |
⊎-<-isStrictTotalOrder sto₁ sto₂ = record |
| 215 |
{ isStrictPartialOrder = ⊎-<-isStrictPartialOrder (isStrictPartialOrder sto₁) (isStrictPartialOrder sto₂) |
| 216 |
; compare = ⊎-<-trichotomous (compare sto₁) (compare sto₂) |
| 217 |
} |
| 218 |
where open IsStrictTotalOrder |
| 219 |
|
| 220 |
------------------------------------------------------------------------ |
| 221 |
-- "Bundles" can also be combined. |
| 222 |
|
| 223 |
module _ {a b c d e f} where |
| 224 |
|
| 225 |
⊎-<-preorder : Preorder a b c → |
| 226 |
Preorder d e f → |
| 227 |
Preorder _ _ _ |
| 228 |
⊎-<-preorder p₁ p₂ = record |
| 229 |
{ isPreorder = |
| 230 |
⊎-<-isPreorder (isPreorder p₁) (isPreorder p₂) |
| 231 |
} where open Preorder |
| 232 |
|
| 233 |
⊎-<-poset : Poset a b c → |
| 234 |
Poset a b c → |
| 235 |
Poset _ _ _ |
| 236 |
⊎-<-poset po₁ po₂ = record |
| 237 |
{ isPartialOrder = |
| 238 |
⊎-<-isPartialOrder (isPartialOrder po₁) (isPartialOrder po₂) |
| 239 |
} where open Poset |
| 240 |
|
| 241 |
⊎-<-strictPartialOrder : StrictPartialOrder a b c → |
| 242 |
StrictPartialOrder d e f → |
| 243 |
StrictPartialOrder _ _ _ |
| 244 |
⊎-<-strictPartialOrder spo₁ spo₂ = record |
| 245 |
{ isStrictPartialOrder = |
| 246 |
⊎-<-isStrictPartialOrder (isStrictPartialOrder spo₁) (isStrictPartialOrder spo₂) |
| 247 |
} where open StrictPartialOrder |
| 248 |
|
| 249 |
⊎-<-totalOrder : TotalOrder a b c → |
| 250 |
TotalOrder d e f → |
| 251 |
TotalOrder _ _ _ |
| 252 |
⊎-<-totalOrder to₁ to₂ = record |
| 253 |
{ isTotalOrder = ⊎-<-isTotalOrder (isTotalOrder to₁) (isTotalOrder to₂) |
| 254 |
} where open TotalOrder |
| 255 |
|
| 256 |
⊎-<-decTotalOrder : DecTotalOrder a b c → |
| 257 |
DecTotalOrder d e f → |
| 258 |
DecTotalOrder _ _ _ |
| 259 |
⊎-<-decTotalOrder to₁ to₂ = record |
| 260 |
{ isDecTotalOrder = ⊎-<-isDecTotalOrder (isDecTotalOrder to₁) (isDecTotalOrder to₂) |
| 261 |
} where open DecTotalOrder |
| 262 |
|
| 263 |
⊎-<-strictTotalOrder : StrictTotalOrder a b c → |
| 264 |
StrictTotalOrder a b c → |
| 265 |
StrictTotalOrder _ _ _ |
| 266 |
⊎-<-strictTotalOrder sto₁ sto₂ = record |
| 267 |
{ isStrictTotalOrder = ⊎-<-isStrictTotalOrder (isStrictTotalOrder sto₁) (isStrictTotalOrder sto₂) |
| 268 |
} where open StrictTotalOrder |