| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Intersection of two binary relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Relation.Binary.Construct.Intersection where |
| 10 |
|
| 11 |
open import Data.Product.Base using (_,_; _×_; map; zip; <_,_>) |
| 12 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]) |
| 13 |
open import Function.Base using (_∘_) |
| 14 |
open import Level using (Level; _⊔_) |
| 15 |
open import Relation.Binary.Core using (Rel; REL; _⇒_) |
| 16 |
open import Relation.Binary.Structures |
| 17 |
using (IsEquivalence; IsDecEquivalence; IsPreorder; IsPartialOrder |
| 18 |
; IsStrictPartialOrder) |
| 19 |
open import Relation.Binary.Definitions |
| 20 |
using (Reflexive; Symmetric; Transitive; Antisymmetric; Decidable; _Respects_ |
| 21 |
; _Respectsˡ_; _Respectsʳ_; _Respects₂_; Minimum; Maximum; Irreflexive) |
| 22 |
open import Relation.Nullary.Decidable using (yes; no; _×-dec_) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
a b ℓ₁ ℓ₂ ℓ₃ : Level |
| 27 |
A B : Set a |
| 28 |
≈ L R : Rel A ℓ₁ |
| 29 |
|
| 30 |
------------------------------------------------------------------------ |
| 31 |
-- Definition |
| 32 |
|
| 33 |
infixl 6 _∩_ |
| 34 |
|
| 35 |
_∩_ : REL A B ℓ₁ → REL A B ℓ₂ → REL A B (ℓ₁ ⊔ ℓ₂) |
| 36 |
L ∩ R = λ i j → L i j × R i j |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Properties |
| 40 |
|
| 41 |
module _ (L : Rel A ℓ₁) (R : Rel A ℓ₂) where |
| 42 |
|
| 43 |
reflexive : Reflexive L → Reflexive R → Reflexive (L ∩ R) |
| 44 |
reflexive L-refl R-refl = L-refl , R-refl |
| 45 |
|
| 46 |
symmetric : Symmetric L → Symmetric R → Symmetric (L ∩ R) |
| 47 |
symmetric L-sym R-sym = map L-sym R-sym |
| 48 |
|
| 49 |
transitive : Transitive L → Transitive R → Transitive (L ∩ R) |
| 50 |
transitive L-trans R-trans = zip L-trans R-trans |
| 51 |
|
| 52 |
respects : ∀ {p} (P : A → Set p) → |
| 53 |
P Respects L ⊎ P Respects R → P Respects (L ∩ R) |
| 54 |
respects P resp (Lxy , Rxy) = [ (λ x → x Lxy) , (λ x → x Rxy) ] resp |
| 55 |
|
| 56 |
min : ∀ {⊤} → Minimum L ⊤ → Minimum R ⊤ → Minimum (L ∩ R) ⊤ |
| 57 |
min L-min R-min = < L-min , R-min > |
| 58 |
|
| 59 |
max : ∀ {⊥} → Maximum L ⊥ → Maximum R ⊥ → Maximum (L ∩ R) ⊥ |
| 60 |
max L-max R-max = < L-max , R-max > |
| 61 |
|
| 62 |
module _ (≈ : REL A B ℓ₁) {L : REL A B ℓ₂} {R : REL A B ℓ₃} where |
| 63 |
|
| 64 |
implies : (≈ ⇒ L) → (≈ ⇒ R) → ≈ ⇒ (L ∩ R) |
| 65 |
implies ≈⇒L ≈⇒R = < ≈⇒L , ≈⇒R > |
| 66 |
|
| 67 |
module _ (≈ : REL A B ℓ₁) (L : REL A B ℓ₂) (R : REL A B ℓ₃) where |
| 68 |
|
| 69 |
irreflexive : Irreflexive ≈ L ⊎ Irreflexive ≈ R → Irreflexive ≈ (L ∩ R) |
| 70 |
irreflexive irrefl x≈y (Lxy , Rxy) = [ (λ x → x x≈y Lxy) , (λ x → x x≈y Rxy) ] irrefl |
| 71 |
|
| 72 |
module _ (≈ : Rel A ℓ₁) (L : Rel A ℓ₂) (R : Rel A ℓ₃) where |
| 73 |
|
| 74 |
respectsˡ : L Respectsˡ ≈ → R Respectsˡ ≈ → (L ∩ R) Respectsˡ ≈ |
| 75 |
respectsˡ L-resp R-resp x≈y = map (L-resp x≈y) (R-resp x≈y) |
| 76 |
|
| 77 |
respectsʳ : L Respectsʳ ≈ → R Respectsʳ ≈ → (L ∩ R) Respectsʳ ≈ |
| 78 |
respectsʳ L-resp R-resp x≈y = map (L-resp x≈y) (R-resp x≈y) |
| 79 |
|
| 80 |
respects₂ : L Respects₂ ≈ → R Respects₂ ≈ → (L ∩ R) Respects₂ ≈ |
| 81 |
respects₂ (Lʳ , Lˡ) (Rʳ , Rˡ) = respectsʳ Lʳ Rʳ , respectsˡ Lˡ Rˡ |
| 82 |
|
| 83 |
antisymmetric : Antisymmetric ≈ L ⊎ Antisymmetric ≈ R → Antisymmetric ≈ (L ∩ R) |
| 84 |
antisymmetric (inj₁ L-antisym) (Lxy , _) (Lyx , _) = L-antisym Lxy Lyx |
| 85 |
antisymmetric (inj₂ R-antisym) (_ , Rxy) (_ , Ryx) = R-antisym Rxy Ryx |
| 86 |
|
| 87 |
module _ {L : REL A B ℓ₁} {R : REL A B ℓ₂} where |
| 88 |
|
| 89 |
decidable : Decidable L → Decidable R → Decidable (L ∩ R) |
| 90 |
decidable L? R? x y = L? x y ×-dec R? x y |
| 91 |
|
| 92 |
------------------------------------------------------------------------ |
| 93 |
-- Structures |
| 94 |
|
| 95 |
isEquivalence : IsEquivalence L → IsEquivalence R → IsEquivalence (L ∩ R) |
| 96 |
isEquivalence {L = L} {R = R} eqₗ eqᵣ = record |
| 97 |
{ refl = reflexive L R L.refl R.refl |
| 98 |
; sym = symmetric L R L.sym R.sym |
| 99 |
; trans = transitive L R L.trans R.trans |
| 100 |
} where module L = IsEquivalence eqₗ; module R = IsEquivalence eqᵣ |
| 101 |
|
| 102 |
isDecEquivalence : IsDecEquivalence L → IsDecEquivalence R → IsDecEquivalence (L ∩ R) |
| 103 |
isDecEquivalence eqₗ eqᵣ = record |
| 104 |
{ isEquivalence = isEquivalence L.isEquivalence R.isEquivalence |
| 105 |
; _≟_ = decidable L._≟_ R._≟_ |
| 106 |
} where module L = IsDecEquivalence eqₗ; module R = IsDecEquivalence eqᵣ |
| 107 |
|
| 108 |
isPreorder : IsPreorder ≈ L → IsPreorder ≈ R → IsPreorder ≈ (L ∩ R) |
| 109 |
isPreorder {≈ = ≈} {L = L} {R = R} Oₗ Oᵣ = record |
| 110 |
{ isEquivalence = Oₗ.isEquivalence |
| 111 |
; reflexive = implies ≈ Oₗ.reflexive Oᵣ.reflexive |
| 112 |
; trans = transitive L R Oₗ.trans Oᵣ.trans |
| 113 |
} |
| 114 |
where module Oₗ = IsPreorder Oₗ; module Oᵣ = IsPreorder Oᵣ |
| 115 |
|
| 116 |
isPartialOrderˡ : IsPartialOrder ≈ L → IsPreorder ≈ R → IsPartialOrder ≈ (L ∩ R) |
| 117 |
isPartialOrderˡ {≈ = ≈} {L = L} {R = R} Oₗ Oᵣ = record |
| 118 |
{ isPreorder = isPreorder Oₗ.isPreorder Oᵣ |
| 119 |
; antisym = antisymmetric ≈ L R (inj₁ Oₗ.antisym) |
| 120 |
} where module Oₗ = IsPartialOrder Oₗ; module Oᵣ = IsPreorder Oᵣ |
| 121 |
|
| 122 |
isPartialOrderʳ : IsPreorder ≈ L → IsPartialOrder ≈ R → IsPartialOrder ≈ (L ∩ R) |
| 123 |
isPartialOrderʳ {≈ = ≈} {L = L} {R = R} Oₗ Oᵣ = record |
| 124 |
{ isPreorder = isPreorder Oₗ Oᵣ.isPreorder |
| 125 |
; antisym = antisymmetric ≈ L R (inj₂ Oᵣ.antisym) |
| 126 |
} where module Oₗ = IsPreorder Oₗ; module Oᵣ = IsPartialOrder Oᵣ |
| 127 |
|
| 128 |
isStrictPartialOrderˡ : IsStrictPartialOrder ≈ L → |
| 129 |
Transitive R → R Respects₂ ≈ → |
| 130 |
IsStrictPartialOrder ≈ (L ∩ R) |
| 131 |
isStrictPartialOrderˡ {≈ = ≈} {L = L} {R = R} Oₗ transᵣ respᵣ = record |
| 132 |
{ isEquivalence = Oₗ.isEquivalence |
| 133 |
; irrefl = irreflexive ≈ L R (inj₁ Oₗ.irrefl) |
| 134 |
; trans = transitive L R Oₗ.trans transᵣ |
| 135 |
; <-resp-≈ = respects₂ ≈ L R Oₗ.<-resp-≈ respᵣ |
| 136 |
} where module Oₗ = IsStrictPartialOrder Oₗ |
| 137 |
|
| 138 |
isStrictPartialOrderʳ : Transitive L → L Respects₂ ≈ → |
| 139 |
IsStrictPartialOrder ≈ R → |
| 140 |
IsStrictPartialOrder ≈ (L ∩ R) |
| 141 |
isStrictPartialOrderʳ {L = L} {≈ = ≈} {R = R} transₗ respₗ Oᵣ = record |
| 142 |
{ isEquivalence = Oᵣ.isEquivalence |
| 143 |
; irrefl = irreflexive ≈ L R (inj₂ Oᵣ.irrefl) |
| 144 |
; trans = transitive L R transₗ Oᵣ.trans |
| 145 |
; <-resp-≈ = respects₂ ≈ L R respₗ Oᵣ.<-resp-≈ |
| 146 |
} where module Oᵣ = IsStrictPartialOrder Oᵣ |