| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Symmetric transitive closures of binary relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Relation.Binary.Construct.Closure.SymmetricTransitive where |
| 10 |
|
| 11 |
open import Level |
| 12 |
open import Function.Base |
| 13 |
open import Relation.Binary.Core using (Rel; _=[_]⇒_; _⇒_) |
| 14 |
open import Relation.Binary.Bundles using (PartialSetoid; Setoid) |
| 15 |
open import Relation.Binary.Structures |
| 16 |
using (IsPartialEquivalence; IsEquivalence) |
| 17 |
open import Relation.Binary.Definitions |
| 18 |
using (Transitive; Symmetric; Reflexive) |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
a c ℓ ℓ′ : Level |
| 23 |
A B : Set a |
| 24 |
|
| 25 |
module _ {A : Set a} (_≤_ : Rel A ℓ) where |
| 26 |
|
| 27 |
data Plus⇔ : Rel A (a ⊔ ℓ) where |
| 28 |
forth : ∀ {x y} → x ≤ y → Plus⇔ x y |
| 29 |
back : ∀ {x y} → y ≤ x → Plus⇔ x y |
| 30 |
forth⁺ : ∀ {x y z} → x ≤ y → Plus⇔ y z → Plus⇔ x z |
| 31 |
back⁺ : ∀ {x y z} → y ≤ x → Plus⇔ y z → Plus⇔ x z |
| 32 |
|
| 33 |
trans : Transitive Plus⇔ |
| 34 |
trans (forth r) rel′ = forth⁺ r rel′ |
| 35 |
trans (back r) rel′ = back⁺ r rel′ |
| 36 |
trans (forth⁺ r rel) rel′ = forth⁺ r (trans rel rel′) |
| 37 |
trans (back⁺ r rel) rel′ = back⁺ r (trans rel rel′) |
| 38 |
|
| 39 |
sym : Symmetric Plus⇔ |
| 40 |
sym (forth r) = back r |
| 41 |
sym (back r) = forth r |
| 42 |
sym (forth⁺ r rel) = trans (sym rel) (back r) |
| 43 |
sym (back⁺ r rel) = trans (sym rel) (forth r) |
| 44 |
|
| 45 |
isPartialEquivalence : IsPartialEquivalence Plus⇔ |
| 46 |
isPartialEquivalence = record |
| 47 |
{ sym = sym |
| 48 |
; trans = trans |
| 49 |
} |
| 50 |
|
| 51 |
partialSetoid : PartialSetoid _ _ |
| 52 |
partialSetoid = record |
| 53 |
{ Carrier = A |
| 54 |
; _≈_ = Plus⇔ |
| 55 |
; isPartialEquivalence = isPartialEquivalence |
| 56 |
} |
| 57 |
|
| 58 |
module _ (refl : Reflexive _≤_) where |
| 59 |
|
| 60 |
isEquivalence : IsEquivalence Plus⇔ |
| 61 |
isEquivalence = record |
| 62 |
{ refl = forth refl |
| 63 |
; sym = sym |
| 64 |
; trans = trans |
| 65 |
} |
| 66 |
|
| 67 |
setoid : Setoid _ _ |
| 68 |
setoid = record |
| 69 |
{ Carrier = A |
| 70 |
; _≈_ = Plus⇔ |
| 71 |
; isEquivalence = isEquivalence |
| 72 |
} |
| 73 |
|
| 74 |
module _ (S : Setoid c ℓ) where |
| 75 |
private |
| 76 |
module S = Setoid S |
| 77 |
|
| 78 |
minimal : (f : A → S.Carrier) → |
| 79 |
_≤_ =[ f ]⇒ S._≈_ → |
| 80 |
Plus⇔ =[ f ]⇒ S._≈_ |
| 81 |
minimal f inj (forth r) = inj r |
| 82 |
minimal f inj (back r) = S.sym (inj r) |
| 83 |
minimal f inj (forth⁺ r rel) = S.trans (inj r) (minimal f inj rel) |
| 84 |
minimal f inj (back⁺ r rel) = S.trans (S.sym (inj r)) (minimal f inj rel) |
| 85 |
|
| 86 |
|
| 87 |
module Plus⇔Reasoning (_≤_ : Rel A ℓ) where |
| 88 |
infix 3 forth-syntax back-syntax |
| 89 |
infixr 2 forth⁺-syntax back⁺-syntax |
| 90 |
|
| 91 |
forth-syntax : ∀ x y → x ≤ y → Plus⇔ _≤_ x y |
| 92 |
forth-syntax _ _ = forth |
| 93 |
|
| 94 |
syntax forth-syntax x y x≤y = x ⇒⟨ x≤y ⟩∎ y ∎ |
| 95 |
|
| 96 |
back-syntax : ∀ x y → y ≤ x → Plus⇔ _≤_ x y |
| 97 |
back-syntax _ _ = back |
| 98 |
|
| 99 |
syntax back-syntax x y y≤x = x ⇐⟨ y≤x ⟩∎ y ∎ |
| 100 |
|
| 101 |
forth⁺-syntax : ∀ x {y z} → x ≤ y → Plus⇔ _≤_ y z → Plus⇔ _≤_ x z |
| 102 |
forth⁺-syntax _ = forth⁺ |
| 103 |
|
| 104 |
syntax forth⁺-syntax x x≤y y⇔z = x ⇒⟨ x≤y ⟩ y⇔z |
| 105 |
|
| 106 |
back⁺-syntax : ∀ x {y z} → y ≤ x → Plus⇔ _≤_ y z → Plus⇔ _≤_ x z |
| 107 |
back⁺-syntax _ = back⁺ |
| 108 |
|
| 109 |
syntax back⁺-syntax x y≤x y⇔z = x ⇐⟨ y≤x ⟩ y⇔z |
| 110 |
|
| 111 |
|
| 112 |
module _ {_≤_ : Rel A ℓ} {_≼_ : Rel B ℓ′} where |
| 113 |
|
| 114 |
gmap : (f : A → B) (inj : _≤_ =[ f ]⇒ _≼_) → Plus⇔ _≤_ =[ f ]⇒ Plus⇔ _≼_ |
| 115 |
gmap f inj (forth r) = forth (inj r) |
| 116 |
gmap f inj (back r) = back (inj r) |
| 117 |
gmap f inj (forth⁺ r rel) = forth⁺ (inj r) (gmap f inj rel) |
| 118 |
gmap f inj (back⁺ r rel) = back⁺ (inj r) (gmap f inj rel) |
| 119 |
|
| 120 |
map : {_≤_ : Rel A ℓ} {_≼_ : Rel A ℓ′} (inj : _≤_ ⇒ _≼_) → Plus⇔ _≤_ ⇒ Plus⇔ _≼_ |
| 121 |
map = gmap id |