| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Conversion of < to ≤, along with a number of properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- Possible TODO: Prove that a conversion ≤ → < → ≤ returns a |
| 8 |
-- relation equivalent to the original one (and similarly for |
| 9 |
-- < → ≤ → <). |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 14 |
|
| 15 |
module Relation.Binary.Construct.StrictToNonStrict |
| 16 |
{a ℓ₁ ℓ₂} {A : Set a} |
| 17 |
(_≈_ : Rel A ℓ₁) (_<_ : Rel A ℓ₂) |
| 18 |
where |
| 19 |
|
| 20 |
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂) |
| 21 |
open import Data.Sum.Base using (inj₁; inj₂; _⊎_; [_,_]; [_,_]′) |
| 22 |
open import Data.Empty using (⊥; ⊥-elim) |
| 23 |
open import Function.Base using (_∘_; flip; _$_) |
| 24 |
open import Relation.Binary.Consequences using (trans∧irr⇒asym) |
| 25 |
open import Relation.Binary.Structures |
| 26 |
using (IsEquivalence; IsPreorder; IsStrictPartialOrder; IsPartialOrder |
| 27 |
; IsStrictTotalOrder; IsTotalOrder; IsDecTotalOrder) |
| 28 |
open import Relation.Binary.Definitions |
| 29 |
using (Transitive; Symmetric; Irreflexive; Antisymmetric; Trichotomous; Decidable |
| 30 |
; Trans; Total; _Respects₂_; _Respectsʳ_; _Respectsˡ_; tri<; tri≈; tri>) |
| 31 |
open import Relation.Nullary.Decidable.Core using (_⊎-dec_; yes; no) |
| 32 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Conversion |
| 36 |
|
| 37 |
-- _<_ can be turned into _≤_ as follows: |
| 38 |
infix 4 _≤_ |
| 39 |
|
| 40 |
_≤_ : Rel A _ |
| 41 |
x ≤ y = (x < y) ⊎ (x ≈ y) |
| 42 |
|
| 43 |
------------------------------------------------------------------------ |
| 44 |
-- The converted relations have certain properties |
| 45 |
-- (if the original relations have certain other properties) |
| 46 |
|
| 47 |
<⇒≤ : _<_ ⇒ _≤_ |
| 48 |
<⇒≤ = inj₁ |
| 49 |
|
| 50 |
reflexive : _≈_ ⇒ _≤_ |
| 51 |
reflexive = inj₂ |
| 52 |
|
| 53 |
antisym : IsEquivalence _≈_ → Transitive _<_ → Irreflexive _≈_ _<_ → |
| 54 |
Antisymmetric _≈_ _≤_ |
| 55 |
antisym eq trans irrefl = as |
| 56 |
where |
| 57 |
module Eq = IsEquivalence eq |
| 58 |
|
| 59 |
as : Antisymmetric _≈_ _≤_ |
| 60 |
as (inj₂ x≈y) _ = x≈y |
| 61 |
as (inj₁ _) (inj₂ y≈x) = Eq.sym y≈x |
| 62 |
as (inj₁ x<y) (inj₁ y<x) = |
| 63 |
contradiction y<x (trans∧irr⇒asym {_≈_ = _≈_} Eq.refl trans irrefl x<y) |
| 64 |
|
| 65 |
trans : IsEquivalence _≈_ → _<_ Respects₂ _≈_ → Transitive _<_ → |
| 66 |
Transitive _≤_ |
| 67 |
trans eq (respʳ , respˡ) <-trans = tr |
| 68 |
where |
| 69 |
module Eq = IsEquivalence eq |
| 70 |
|
| 71 |
tr : Transitive _≤_ |
| 72 |
tr (inj₁ x<y) (inj₁ y<z) = inj₁ $ <-trans x<y y<z |
| 73 |
tr (inj₁ x<y) (inj₂ y≈z) = inj₁ $ respʳ y≈z x<y |
| 74 |
tr (inj₂ x≈y) (inj₁ y<z) = inj₁ $ respˡ (Eq.sym x≈y) y<z |
| 75 |
tr (inj₂ x≈y) (inj₂ y≈z) = inj₂ $ Eq.trans x≈y y≈z |
| 76 |
|
| 77 |
<-≤-trans : Transitive _<_ → _<_ Respectsʳ _≈_ → Trans _<_ _≤_ _<_ |
| 78 |
<-≤-trans trans respʳ x<y (inj₁ y<z) = trans x<y y<z |
| 79 |
<-≤-trans trans respʳ x<y (inj₂ y≈z) = respʳ y≈z x<y |
| 80 |
|
| 81 |
≤-<-trans : Symmetric _≈_ → Transitive _<_ → _<_ Respectsˡ _≈_ → Trans _≤_ _<_ _<_ |
| 82 |
≤-<-trans sym trans respˡ (inj₁ x<y) y<z = trans x<y y<z |
| 83 |
≤-<-trans sym trans respˡ (inj₂ x≈y) y<z = respˡ (sym x≈y) y<z |
| 84 |
|
| 85 |
≤-respʳ-≈ : Transitive _≈_ → _<_ Respectsʳ _≈_ → _≤_ Respectsʳ _≈_ |
| 86 |
≤-respʳ-≈ trans respʳ y′≈y (inj₁ x<y′) = inj₁ (respʳ y′≈y x<y′) |
| 87 |
≤-respʳ-≈ trans respʳ y′≈y (inj₂ x≈y′) = inj₂ (trans x≈y′ y′≈y) |
| 88 |
|
| 89 |
≤-respˡ-≈ : Symmetric _≈_ → Transitive _≈_ → _<_ Respectsˡ _≈_ → _≤_ Respectsˡ _≈_ |
| 90 |
≤-respˡ-≈ sym trans respˡ x′≈x (inj₁ x′<y) = inj₁ (respˡ x′≈x x′<y) |
| 91 |
≤-respˡ-≈ sym trans respˡ x′≈x (inj₂ x′≈y) = inj₂ (trans (sym x′≈x) x′≈y) |
| 92 |
|
| 93 |
≤-resp-≈ : IsEquivalence _≈_ → _<_ Respects₂ _≈_ → _≤_ Respects₂ _≈_ |
| 94 |
≤-resp-≈ eq (respʳ , respˡ) = ≤-respʳ-≈ Eq.trans respʳ , ≤-respˡ-≈ Eq.sym Eq.trans respˡ |
| 95 |
where module Eq = IsEquivalence eq |
| 96 |
|
| 97 |
total : Trichotomous _≈_ _<_ → Total _≤_ |
| 98 |
total <-tri x y with <-tri x y |
| 99 |
... | tri< x<y x≉y x≯y = inj₁ (inj₁ x<y) |
| 100 |
... | tri≈ x≮y x≈y x≯y = inj₁ (inj₂ x≈y) |
| 101 |
... | tri> x≮y x≉y x>y = inj₂ (inj₁ x>y) |
| 102 |
|
| 103 |
decidable : Decidable _≈_ → Decidable _<_ → Decidable _≤_ |
| 104 |
decidable ≈-dec <-dec x y = <-dec x y ⊎-dec ≈-dec x y |
| 105 |
|
| 106 |
decidable′ : Trichotomous _≈_ _<_ → Decidable _≤_ |
| 107 |
decidable′ compare x y with compare x y |
| 108 |
... | tri< x<y _ _ = yes (inj₁ x<y) |
| 109 |
... | tri≈ _ x≈y _ = yes (inj₂ x≈y) |
| 110 |
... | tri> x≮y x≉y _ = no [ x≮y , x≉y ]′ |
| 111 |
|
| 112 |
------------------------------------------------------------------------ |
| 113 |
-- Converting structures |
| 114 |
|
| 115 |
isPreorder₁ : IsPreorder _≈_ _<_ → IsPreorder _≈_ _≤_ |
| 116 |
isPreorder₁ PO = record |
| 117 |
{ isEquivalence = S.isEquivalence |
| 118 |
; reflexive = reflexive |
| 119 |
; trans = trans S.isEquivalence S.∼-resp-≈ S.trans |
| 120 |
} |
| 121 |
where module S = IsPreorder PO |
| 122 |
|
| 123 |
isPreorder₂ : IsStrictPartialOrder _≈_ _<_ → IsPreorder _≈_ _≤_ |
| 124 |
isPreorder₂ SPO = record |
| 125 |
{ isEquivalence = S.isEquivalence |
| 126 |
; reflexive = reflexive |
| 127 |
; trans = trans S.isEquivalence S.<-resp-≈ S.trans |
| 128 |
} |
| 129 |
where module S = IsStrictPartialOrder SPO |
| 130 |
|
| 131 |
isPartialOrder : IsStrictPartialOrder _≈_ _<_ → IsPartialOrder _≈_ _≤_ |
| 132 |
isPartialOrder SPO = record |
| 133 |
{ isPreorder = isPreorder₂ SPO |
| 134 |
; antisym = antisym S.isEquivalence S.trans S.irrefl |
| 135 |
} |
| 136 |
where module S = IsStrictPartialOrder SPO |
| 137 |
|
| 138 |
isTotalOrder : IsStrictTotalOrder _≈_ _<_ → IsTotalOrder _≈_ _≤_ |
| 139 |
isTotalOrder STO = record |
| 140 |
{ isPartialOrder = isPartialOrder S.isStrictPartialOrder |
| 141 |
; total = total S.compare |
| 142 |
} |
| 143 |
where module S = IsStrictTotalOrder STO |
| 144 |
|
| 145 |
isDecTotalOrder : IsStrictTotalOrder _≈_ _<_ → IsDecTotalOrder _≈_ _≤_ |
| 146 |
isDecTotalOrder STO = record |
| 147 |
{ isTotalOrder = isTotalOrder STO |
| 148 |
; _≟_ = S._≟_ |
| 149 |
; _≤?_ = decidable′ S.compare |
| 150 |
} |
| 151 |
where module S = IsStrictTotalOrder STO |
| 152 |
|
| 153 |
|
| 154 |
------------------------------------------------------------------------ |
| 155 |
-- DEPRECATED |
| 156 |
------------------------------------------------------------------------ |
| 157 |
-- Please use the new names as continuing support for the old names is |
| 158 |
-- not guaranteed. |
| 159 |
|
| 160 |
-- Version 1.4 |
| 161 |
|
| 162 |
decidable' : Trichotomous _≈_ _<_ → Decidable _≤_ |
| 163 |
decidable' = decidable′ |
| 164 |
{-# WARNING_ON_USAGE decidable' |
| 165 |
"Warning: decidable' (ending in an apostrophe) was deprecated in v1.4. |
| 166 |
Please use decidable′ (ending in a prime) instead." |
| 167 |
#-} |