| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Some properties of reflexive closures |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --safe --cubical-compatible #-} |
| 8 |
|
| 9 |
module Relation.Binary.Construct.Closure.Reflexive.Properties where |
| 10 |
|
| 11 |
open import Data.Product.Base as Product using (_,_; map) |
| 12 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 13 |
open import Function.Bundles using (_⇔_; mk⇔) |
| 14 |
open import Function.Base using (id) |
| 15 |
open import Level using (Level; _⊔_) |
| 16 |
open import Relation.Binary.Core using (Rel; REL; _=[_]⇒_) |
| 17 |
open import Relation.Binary.Structures |
| 18 |
using (IsPreorder; IsStrictPartialOrder; IsPartialOrder |
| 19 |
; IsDecStrictPartialOrder; IsDecPartialOrder; IsStrictTotalOrder |
| 20 |
; IsTotalOrder; IsDecTotalOrder) |
| 21 |
open import Relation.Binary.Definitions |
| 22 |
using (Symmetric; Transitive; Reflexive; Asymmetric; Antisymmetric |
| 23 |
; Trichotomous; Total; Decidable; DecidableEquality; tri<; tri≈; tri> |
| 24 |
; _Respectsˡ_; _Respectsʳ_; _Respects_; _Respects₂_) |
| 25 |
open import Relation.Binary.Construct.Closure.Reflexive |
| 26 |
using (ReflClosure; [_]; refl) |
| 27 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 28 |
import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 29 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 30 |
open import Relation.Nullary.Decidable as Dec using (yes; no) |
| 31 |
open import Relation.Unary using (Pred) |
| 32 |
|
| 33 |
private |
| 34 |
variable |
| 35 |
a b ℓ p q : Level |
| 36 |
A : Set a |
| 37 |
B : Set b |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Relational properties |
| 41 |
|
| 42 |
module _ {P : Rel A p} {Q : Rel B q} where |
| 43 |
|
| 44 |
=[]⇒ : ∀ {f : A → B} → P =[ f ]⇒ Q → ReflClosure P =[ f ]⇒ ReflClosure Q |
| 45 |
=[]⇒ x [ x∼y ] = [ x x∼y ] |
| 46 |
=[]⇒ x refl = refl |
| 47 |
|
| 48 |
module _ {_~_ : Rel A ℓ} where |
| 49 |
|
| 50 |
private |
| 51 |
_~ᵒ_ = ReflClosure _~_ |
| 52 |
|
| 53 |
fromSum : ∀ {x y} → x ≡ y ⊎ x ~ y → x ~ᵒ y |
| 54 |
fromSum (inj₁ refl) = refl |
| 55 |
fromSum (inj₂ y) = [ y ] |
| 56 |
|
| 57 |
toSum : ∀ {x y} → x ~ᵒ y → x ≡ y ⊎ x ~ y |
| 58 |
toSum [ x∼y ] = inj₂ x∼y |
| 59 |
toSum refl = inj₁ refl |
| 60 |
|
| 61 |
⊎⇔Refl : ∀ {a b} → (a ≡ b ⊎ a ~ b) ⇔ a ~ᵒ b |
| 62 |
⊎⇔Refl = mk⇔ fromSum toSum |
| 63 |
|
| 64 |
sym : Symmetric _~_ → Symmetric _~ᵒ_ |
| 65 |
sym ~-sym [ x∼y ] = [ ~-sym x∼y ] |
| 66 |
sym ~-sym refl = refl |
| 67 |
|
| 68 |
trans : Transitive _~_ → Transitive _~ᵒ_ |
| 69 |
trans ~-trans [ x∼y ] [ x∼y₁ ] = [ ~-trans x∼y x∼y₁ ] |
| 70 |
trans ~-trans [ x∼y ] refl = [ x∼y ] |
| 71 |
trans ~-trans refl [ x∼y ] = [ x∼y ] |
| 72 |
trans ~-trans refl refl = refl |
| 73 |
|
| 74 |
antisym : (_≈_ : Rel A p) → Reflexive _≈_ → |
| 75 |
Asymmetric _~_ → Antisymmetric _≈_ _~ᵒ_ |
| 76 |
antisym _≈_ ref asym [ x∼y ] [ y∼x ] = contradiction x∼y (asym y∼x) |
| 77 |
antisym _≈_ ref asym [ x∼y ] refl = ref |
| 78 |
antisym _≈_ ref asym refl _ = ref |
| 79 |
|
| 80 |
total : Trichotomous _≡_ _~_ → Total _~ᵒ_ |
| 81 |
total compare x y with compare x y |
| 82 |
... | tri< a _ _ = inj₁ [ a ] |
| 83 |
... | tri≈ _ refl _ = inj₁ refl |
| 84 |
... | tri> _ _ c = inj₂ [ c ] |
| 85 |
|
| 86 |
dec : DecidableEquality A → Decidable _~_ → Decidable _~ᵒ_ |
| 87 |
dec ≡-dec ~-dec a b = Dec.map ⊎⇔Refl (≡-dec a b Dec.⊎-dec ~-dec a b) |
| 88 |
|
| 89 |
decidable : Trichotomous _≡_ _~_ → Decidable _~ᵒ_ |
| 90 |
decidable ~-tri a b with ~-tri a b |
| 91 |
... | tri< a~b _ _ = yes [ a~b ] |
| 92 |
... | tri≈ _ refl _ = yes refl |
| 93 |
... | tri> ¬a ¬b _ = no λ { refl → ¬b refl ; [ p ] → ¬a p } |
| 94 |
|
| 95 |
respˡ : ∀ {P : REL A B p} → P Respectsˡ _~_ → P Respectsˡ _~ᵒ_ |
| 96 |
respˡ p-respˡ-~ [ x∼y ] = p-respˡ-~ x∼y |
| 97 |
respˡ _ refl = id |
| 98 |
|
| 99 |
respʳ : ∀ {P : REL B A p} → P Respectsʳ _~_ → P Respectsʳ _~ᵒ_ |
| 100 |
respʳ = respˡ |
| 101 |
|
| 102 |
module _ {_~_ : Rel A ℓ} {P : Pred A p} where |
| 103 |
|
| 104 |
resp : P Respects _~_ → P Respects (ReflClosure _~_) |
| 105 |
resp p-resp-~ [ x∼y ] = p-resp-~ x∼y |
| 106 |
resp _ refl = id |
| 107 |
|
| 108 |
module _ {_~_ : Rel A ℓ} {P : Rel A p} where |
| 109 |
|
| 110 |
resp₂ : P Respects₂ _~_ → P Respects₂ (ReflClosure _~_) |
| 111 |
resp₂ = Product.map respˡ respʳ |
| 112 |
|
| 113 |
------------------------------------------------------------------------ |
| 114 |
-- Structures |
| 115 |
|
| 116 |
module _ {_~_ : Rel A ℓ} where |
| 117 |
|
| 118 |
private |
| 119 |
_~ᵒ_ = ReflClosure _~_ |
| 120 |
|
| 121 |
isPreorder : Transitive _~_ → IsPreorder _≡_ _~ᵒ_ |
| 122 |
isPreorder ~-trans = record |
| 123 |
{ isEquivalence = ≡.isEquivalence |
| 124 |
; reflexive = λ { refl → refl } |
| 125 |
; trans = trans ~-trans |
| 126 |
} |
| 127 |
|
| 128 |
isPartialOrder : IsStrictPartialOrder _≡_ _~_ → IsPartialOrder _≡_ _~ᵒ_ |
| 129 |
isPartialOrder O = record |
| 130 |
{ isPreorder = isPreorder O.trans |
| 131 |
; antisym = antisym _≡_ refl O.asym |
| 132 |
} where module O = IsStrictPartialOrder O |
| 133 |
|
| 134 |
isDecPartialOrder : IsDecStrictPartialOrder _≡_ _~_ → IsDecPartialOrder _≡_ _~ᵒ_ |
| 135 |
isDecPartialOrder O = record |
| 136 |
{ isPartialOrder = isPartialOrder O.isStrictPartialOrder |
| 137 |
; _≟_ = O._≟_ |
| 138 |
; _≤?_ = dec O._≟_ O._<?_ |
| 139 |
} where module O = IsDecStrictPartialOrder O |
| 140 |
|
| 141 |
isTotalOrder : IsStrictTotalOrder _≡_ _~_ → IsTotalOrder _≡_ _~ᵒ_ |
| 142 |
isTotalOrder O = record |
| 143 |
{ isPartialOrder = isPartialOrder isStrictPartialOrder |
| 144 |
; total = total compare |
| 145 |
} where open IsStrictTotalOrder O |
| 146 |
|
| 147 |
isDecTotalOrder : IsStrictTotalOrder _≡_ _~_ → IsDecTotalOrder _≡_ _~ᵒ_ |
| 148 |
isDecTotalOrder O = record |
| 149 |
{ isTotalOrder = isTotalOrder O |
| 150 |
; _≟_ = _≟_ |
| 151 |
; _≤?_ = dec _≟_ _<?_ |
| 152 |
} where open IsStrictTotalOrder O |