Debian Codegraph @

src/Data/Sum/Relation/Binary/Pointwise.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Pointwise sum
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Sum.Relation.Binary.Pointwise where
10
11 open import Data.Product.Base using (_,_)
12 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
13 open import Induction.WellFounded
14 open import Level using (Level; _⊔_)
15 open import Function.Base using (const; _∘_; id)
16 open import Function.Bundles using (Inverse; mk↔)
17 open import Relation.Nullary.Decidable.Core as Dec using (yes; no; map′)
18 open import Relation.Nullary.Negation.Core using (¬_)
19 open import Relation.Binary
20 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
21 import Relation.Binary.PropositionalEquality.Properties as ≡
22
23 private
24 variable
25 a b c d ℓ₁ ℓ₂ ℓ₃ ℓ : Level
26 A B C D : Set ℓ
27 R S T U : REL A B ℓ
28 ≈₁ ≈₂ : Rel A ℓ
29
30 ------------------------------------------------------------------------
31 -- Definition
32
33 data Pointwise {A : Set a} {B : Set b} {C : Set c} {D : Set d}
34 (R : REL A C ℓ₁) (S : REL B D ℓ₂)
35 : REL (A ⊎ B) (C ⊎ D) (a ⊔ b ⊔ c ⊔ d ⊔ ℓ₁ ⊔ ℓ₂) where
36 inj₁ : ∀ {a c} → R a c → Pointwise R S (inj₁ a) (inj₁ c)
37 inj₂ : ∀ {b d} → S b d → Pointwise R S (inj₂ b) (inj₂ d)
38
39 ----------------------------------------------------------------------
40 -- Functions
41
42 map : ∀ {f : A → C} {g : B → D} →
43 R =[ f ]⇒ T → S =[ g ]⇒ U →
44 Pointwise R S =[ Sum.map f g ]⇒ Pointwise T U
45 map R⇒T _ (inj₁ x) = inj₁ (R⇒T x)
46 map _ S⇒U (inj₂ x) = inj₂ (S⇒U x)
47
48 ------------------------------------------------------------------------
49 -- Relational properties
50
51 drop-inj₁ : ∀ {x y} → Pointwise R S (inj₁ x) (inj₁ y) → R x y
52 drop-inj₁ (inj₁ x) = x
53
54 drop-inj₂ : ∀ {x y} → Pointwise R S (inj₂ x) (inj₂ y) → S x y
55 drop-inj₂ (inj₂ x) = x
56
57 ⊎-refl : Reflexive R → Reflexive S → Reflexive (Pointwise R S)
58 ⊎-refl refl₁ refl₂ {inj₁ x} = inj₁ refl₁
59 ⊎-refl refl₁ refl₂ {inj₂ y} = inj₂ refl₂
60
61 ⊎-symmetric : Symmetric R → Symmetric S →
62 Symmetric (Pointwise R S)
63 ⊎-symmetric sym₁ sym₂ (inj₁ x) = inj₁ (sym₁ x)
64 ⊎-symmetric sym₁ sym₂ (inj₂ x) = inj₂ (sym₂ x)
65
66 ⊎-transitive : Transitive R → Transitive S →
67 Transitive (Pointwise R S)
68 ⊎-transitive trans₁ trans₂ (inj₁ x) (inj₁ y) = inj₁ (trans₁ x y)
69 ⊎-transitive trans₁ trans₂ (inj₂ x) (inj₂ y) = inj₂ (trans₂ x y)
70
71 ⊎-asymmetric : Asymmetric R → Asymmetric S →
72 Asymmetric (Pointwise R S)
73 ⊎-asymmetric asym₁ asym₂ (inj₁ x) = λ { (inj₁ y) → asym₁ x y }
74 ⊎-asymmetric asym₁ asym₂ (inj₂ x) = λ { (inj₂ y) → asym₂ x y }
75
76 ⊎-substitutive : Substitutive R ℓ₃ → Substitutive S ℓ₃ →
77 Substitutive (Pointwise R S) ℓ₃
78 ⊎-substitutive subst₁ subst₂ P (inj₁ x) = subst₁ (P ∘ inj₁) x
79 ⊎-substitutive subst₁ subst₂ P (inj₂ x) = subst₂ (P ∘ inj₂) x
80
81 ⊎-decidable : Decidable R → Decidable S → Decidable (Pointwise R S)
82 ⊎-decidable _≟₁_ _≟₂_ (inj₁ x) (inj₁ y) = Dec.map′ inj₁ drop-inj₁ (x ≟₁ y)
83 ⊎-decidable _≟₁_ _≟₂_ (inj₁ x) (inj₂ y) = no λ()
84 ⊎-decidable _≟₁_ _≟₂_ (inj₂ x) (inj₁ y) = no λ()
85 ⊎-decidable _≟₁_ _≟₂_ (inj₂ x) (inj₂ y) = Dec.map′ inj₂ drop-inj₂ (x ≟₂ y)
86
87 ⊎-reflexive : ≈₁ ⇒ R → ≈₂ ⇒ S →
88 (Pointwise ≈₁ ≈₂) ⇒ (Pointwise R S)
89 ⊎-reflexive refl₁ refl₂ (inj₁ x) = inj₁ (refl₁ x)
90 ⊎-reflexive refl₁ refl₂ (inj₂ x) = inj₂ (refl₂ x)
91
92 ⊎-irreflexive : Irreflexive ≈₁ R → Irreflexive ≈₂ S →
93 Irreflexive (Pointwise ≈₁ ≈₂) (Pointwise R S)
94 ⊎-irreflexive irrefl₁ irrefl₂ (inj₁ x) (inj₁ y) = irrefl₁ x y
95 ⊎-irreflexive irrefl₁ irrefl₂ (inj₂ x) (inj₂ y) = irrefl₂ x y
96
97 ⊎-wellFounded : WellFounded ≈₁ → WellFounded ≈₂ → WellFounded (Pointwise ≈₁ ≈₂)
98 ⊎-wellFounded {≈₁ = ≈₁} {≈₂ = ≈₂} wf₁ wf₂ x = acc (⊎-acc x)
99 where
100 ⊎-acc₁ : ∀ {x} → Acc ≈₁ x → WfRec (Pointwise ≈₁ ≈₂) (Acc (Pointwise ≈₁ ≈₂)) (inj₁ x)
101 ⊎-acc₁ (acc rec) (inj₁ x≈₁y) = acc (⊎-acc₁ (rec x≈₁y))
102
103 ⊎-acc₂ : ∀ {x} → Acc ≈₂ x → WfRec (Pointwise ≈₁ ≈₂) (Acc (Pointwise ≈₁ ≈₂)) (inj₂ x)
104 ⊎-acc₂ (acc rec) (inj₂ x≈₂y) = acc (⊎-acc₂ (rec x≈₂y))
105
106 ⊎-acc : ∀ x → WfRec (Pointwise ≈₁ ≈₂) (Acc (Pointwise ≈₁ ≈₂)) x
107 ⊎-acc (inj₁ x) = ⊎-acc₁ (wf₁ x)
108 ⊎-acc (inj₂ x) = ⊎-acc₂ (wf₂ x)
109
110 ⊎-antisymmetric : Antisymmetric ≈₁ R → Antisymmetric ≈₂ S →
111 Antisymmetric (Pointwise ≈₁ ≈₂) (Pointwise R S)
112 ⊎-antisymmetric antisym₁ antisym₂ (inj₁ x) (inj₁ y) = inj₁ (antisym₁ x y)
113 ⊎-antisymmetric antisym₁ antisym₂ (inj₂ x) (inj₂ y) = inj₂ (antisym₂ x y)
114
115 ⊎-respectsˡ : R Respectsˡ ≈₁ → S Respectsˡ ≈₂ →
116 (Pointwise R S) Respectsˡ (Pointwise ≈₁ ≈₂)
117 ⊎-respectsˡ resp₁ resp₂ (inj₁ x) (inj₁ y) = inj₁ (resp₁ x y)
118 ⊎-respectsˡ resp₁ resp₂ (inj₂ x) (inj₂ y) = inj₂ (resp₂ x y)
119
120 ⊎-respectsʳ : R Respectsʳ ≈₁ → S Respectsʳ ≈₂ →
121 (Pointwise R S) Respectsʳ (Pointwise ≈₁ ≈₂)
122 ⊎-respectsʳ resp₁ resp₂ (inj₁ x) (inj₁ y) = inj₁ (resp₁ x y)
123 ⊎-respectsʳ resp₁ resp₂ (inj₂ x) (inj₂ y) = inj₂ (resp₂ x y)
124
125 ⊎-respects₂ : R Respects₂ ≈₁ → S Respects₂ ≈₂ →
126 (Pointwise R S) Respects₂ (Pointwise ≈₁ ≈₂)
127 ⊎-respects₂ (r₁ , l₁) (r₂ , l₂) = ⊎-respectsʳ r₁ r₂ , ⊎-respectsˡ l₁ l₂
128
129 ------------------------------------------------------------------------
130 -- Structures
131
132 ⊎-isEquivalence : IsEquivalence ≈₁ → IsEquivalence ≈₂ →
133 IsEquivalence (Pointwise ≈₁ ≈₂)
134 ⊎-isEquivalence eq₁ eq₂ = record
135 { refl = ⊎-refl (refl eq₁) (refl eq₂)
136 ; sym = ⊎-symmetric (sym eq₁) (sym eq₂)
137 ; trans = ⊎-transitive (trans eq₁) (trans eq₂)
138 } where open IsEquivalence
139
140 ⊎-isDecEquivalence : IsDecEquivalence ≈₁ → IsDecEquivalence ≈₂ →
141 IsDecEquivalence (Pointwise ≈₁ ≈₂)
142 ⊎-isDecEquivalence eq₁ eq₂ = record
143 { isEquivalence =
144 ⊎-isEquivalence (isEquivalence eq₁) (isEquivalence eq₂)
145 ; _≟_ = ⊎-decidable (_≟_ eq₁) (_≟_ eq₂)
146 } where open IsDecEquivalence
147
148 ⊎-isPreorder : IsPreorder ≈₁ R → IsPreorder ≈₂ S →
149 IsPreorder (Pointwise ≈₁ ≈₂) (Pointwise R S)
150 ⊎-isPreorder pre₁ pre₂ = record
151 { isEquivalence =
152 ⊎-isEquivalence (isEquivalence pre₁) (isEquivalence pre₂)
153 ; reflexive = ⊎-reflexive (reflexive pre₁) (reflexive pre₂)
154 ; trans = ⊎-transitive (trans pre₁) (trans pre₂)
155 } where open IsPreorder
156
157 ⊎-isPartialOrder : IsPartialOrder ≈₁ R → IsPartialOrder ≈₂ S →
158 IsPartialOrder
159 (Pointwise ≈₁ ≈₂) (Pointwise R S)
160 ⊎-isPartialOrder po₁ po₂ = record
161 { isPreorder = ⊎-isPreorder (isPreorder po₁) (isPreorder po₂)
162 ; antisym = ⊎-antisymmetric (antisym po₁) (antisym po₂)
163 } where open IsPartialOrder
164
165 ⊎-isStrictPartialOrder : IsStrictPartialOrder ≈₁ R →
166 IsStrictPartialOrder ≈₂ S →
167 IsStrictPartialOrder
168 (Pointwise ≈₁ ≈₂) (Pointwise R S)
169 ⊎-isStrictPartialOrder spo₁ spo₂ = record
170 { isEquivalence =
171 ⊎-isEquivalence (isEquivalence spo₁) (isEquivalence spo₂)
172 ; irrefl = ⊎-irreflexive (irrefl spo₁) (irrefl spo₂)
173 ; trans = ⊎-transitive (trans spo₁) (trans spo₂)
174 ; <-resp-≈ = ⊎-respects₂ (<-resp-≈ spo₁) (<-resp-≈ spo₂)
175 } where open IsStrictPartialOrder
176
177 ------------------------------------------------------------------------
178 -- Bundles
179
180 ⊎-setoid : Setoid a b → Setoid c d → Setoid _ _
181 ⊎-setoid s₁ s₂ = record
182 { isEquivalence =
183 ⊎-isEquivalence (isEquivalence s₁) (isEquivalence s₂)
184 } where open Setoid
185
186 ⊎-decSetoid : DecSetoid a b → DecSetoid c d → DecSetoid _ _
187 ⊎-decSetoid ds₁ ds₂ = record
188 { isDecEquivalence =
189 ⊎-isDecEquivalence (isDecEquivalence ds₁) (isDecEquivalence ds₂)
190 } where open DecSetoid
191
192 ⊎-preorder : Preorder a b ℓ₁ → Preorder c d ℓ₂ → Preorder _ _ _
193 ⊎-preorder p₁ p₂ = record
194 { isPreorder =
195 ⊎-isPreorder (isPreorder p₁) (isPreorder p₂)
196 } where open Preorder
197
198 ⊎-poset : Poset a b c → Poset a b c → Poset _ _ _
199 ⊎-poset po₁ po₂ = record
200 { isPartialOrder =
201 ⊎-isPartialOrder (isPartialOrder po₁) (isPartialOrder po₂)
202 } where open Poset
203
204 ------------------------------------------------------------------------
205 -- Additional notation
206
207 -- Infix combining setoids
208 infix 4 _⊎ₛ_
209 _⊎ₛ_ : Setoid a b → Setoid c d → Setoid _ _
210 _⊎ₛ_ = ⊎-setoid
211
212 ------------------------------------------------------------------------
213 -- The propositional equality setoid over products can be
214 -- decomposed using Pointwise
215
216 Pointwise-≡⇒≡ : (Pointwise _≡_ _≡_) ⇒ _≡_ {A = A ⊎ B}
217 Pointwise-≡⇒≡ (inj₁ x) = ≡.cong inj₁ x
218 Pointwise-≡⇒≡ (inj₂ x) = ≡.cong inj₂ x
219
220 ≡⇒Pointwise-≡ : _≡_ {A = A ⊎ B} ⇒ (Pointwise _≡_ _≡_)
221 ≡⇒Pointwise-≡ ≡.refl = ⊎-refl ≡.refl ≡.refl
222
223 Pointwise-≡↔≡ : (A : Set a) (B : Set b) →
224 Inverse (≡.setoid A ⊎ₛ ≡.setoid B) (≡.setoid (A ⊎ B))
225 Pointwise-≡↔≡ _ _ = record
226 { to = id
227 ; from = id
228 ; to-cong = Pointwise-≡⇒≡
229 ; from-cong = ≡⇒Pointwise-≡
230 ; inverse = Pointwise-≡⇒≡ , ≡⇒Pointwise-≡
231 }