Debian Codegraph @

src/Relation/Binary/HeterogeneousEquality.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Heterogeneous equality
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K --safe #-}
8
9 module Relation.Binary.HeterogeneousEquality where
10
11 open import Data.Product.Base using (_,_)
12 open import Function.Base using (case_of_; _∋_; id)
13 open import Function.Bundles using (Inverse)
14 open import Level using (Level; _⊔_)
15 open import Relation.Binary.Core using (Rel; REL; _⇒_)
16 open import Relation.Binary.Bundles using (Setoid; DecSetoid; Preorder)
17 open import Relation.Binary.Structures using (IsEquivalence; IsPreorder)
18 open import Relation.Binary.Definitions using (Substitutive; Irrelevant
19 ; Decidable; _Respects₂_; Trans; Reflexive)
20 open import Relation.Binary.Consequences using (subst⇒resp₂)
21 open import Relation.Binary.Indexed.Heterogeneous
22 using (IndexedSetoid)
23 open import Relation.Binary.Indexed.Heterogeneous.Construct.At
24 using (_atₛ_)
25 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_; refl)
26 open import Relation.Binary.Reasoning.Syntax
27 open import Relation.Nullary.Decidable.Core using (yes; no)
28 open import Relation.Nullary.Negation.Core using (¬_)
29 open import Relation.Unary using (Pred)
30
31 import Relation.Binary.PropositionalEquality.Properties as ≡
32 import Relation.Binary.HeterogeneousEquality.Core as Core
33
34 private
35 variable
36 a b c p r ℓ : Level
37 A : Set a
38 B : Set b
39 C : Set c
40
41 ------------------------------------------------------------------------
42 -- Heterogeneous equality
43
44 infix 4 _≇_
45
46 open Core public using (_≅_; refl)
47
48 -- Nonequality.
49
50 _≇_ : ∀ {A : Set a} → A → {B : Set b} → B → Set a
51 x ≇ y = ¬ x ≅ y
52
53 ------------------------------------------------------------------------
54 -- Conversion
55
56 open Core public using (≅-to-≡; ≡-to-≅)
57
58 ≅-to-type-≡ : ∀ {A B : Set a} {x : A} {y : B} → x ≅ y → A ≡ B
59 ≅-to-type-≡ refl = refl
60
61 ≅-to-subst-≡ : ∀ {A B : Set a} {x : A} {y : B} → (p : x ≅ y) →
62 ≡.subst (λ x → x) (≅-to-type-≡ p) x ≡ y
63 ≅-to-subst-≡ refl = refl
64
65 ------------------------------------------------------------------------
66 -- Some properties
67
68 reflexive : _⇒_ {A = A} _≡_ (λ x y → x ≅ y)
69 reflexive refl = refl
70
71 sym : ∀ {x : A} {y : B} → x ≅ y → y ≅ x
72 sym refl = refl
73
74 trans : ∀ {x : A} {y : B} {z : C} → x ≅ y → y ≅ z → x ≅ z
75 trans refl eq = eq
76
77 subst : Substitutive {A = A} (λ x y → x ≅ y) ℓ
78 subst P refl p = p
79
80 subst₂ : ∀ (_∼_ : REL A B r) {x y u v} → x ≅ y → u ≅ v → x ∼ u → y ∼ v
81 subst₂ _∼_ refl refl z = z
82
83 subst-removable : ∀ (P : Pred A p) {x y} (eq : x ≅ y) (z : P x) →
84 subst P eq z ≅ z
85 subst-removable P refl z = refl
86
87 subst₂-removable : ∀ (_∼_ : REL A B r) {x y u v} (eq₁ : x ≅ y) (eq₂ : u ≅ v) (z : x ∼ u) →
88 subst₂ _∼_ eq₁ eq₂ z ≅ z
89 subst₂-removable _∼_ refl refl z = refl
90
91 ≡-subst-removable : ∀ (P : Pred A p) {x y} (eq : x ≡ y) (z : P x) →
92 ≡.subst P eq z ≅ z
93 ≡-subst-removable P refl z = refl
94
95 cong : ∀ {A : Set a} {B : A → Set b} {x y}
96 (f : (x : A) → B x) → x ≅ y → f x ≅ f y
97 cong f refl = refl
98
99 cong-app : ∀ {A : Set a} {B : A → Set b} {f g : (x : A) → B x} →
100 f ≅ g → (x : A) → f x ≅ g x
101 cong-app refl x = refl
102
103 cong₂ : ∀ {A : Set a} {B : A → Set b} {C : ∀ x → B x → Set c} {x y u v}
104 (f : (x : A) (y : B x) → C x y) → x ≅ y → u ≅ v → f x u ≅ f y v
105 cong₂ f refl refl = refl
106
107 resp₂ : ∀ (∼ : Rel A ℓ) → ∼ Respects₂ (λ x y → x ≅ y)
108 resp₂ _∼_ = subst⇒resp₂ _∼_ subst
109
110 module _ {I : Set ℓ} (A : I → Set a) {B : {k : I} → A k → Set b} where
111
112 icong : {i j : I} {x : A i} {y : A j} →
113 i ≡ j →
114 (f : {k : I} → (z : A k) → B z) →
115 x ≅ y →
116 f x ≅ f y
117 icong refl _ refl = refl
118
119 icong₂ : {C : {k : I} → (a : A k) → B a → Set c}
120 {i j : I} {x : A i} {y : A j} {u : B x} {v : B y} →
121 i ≡ j →
122 (f : {k : I} → (z : A k) → (w : B z) → C z w) →
123 x ≅ y → u ≅ v →
124 f x u ≅ f y v
125 icong₂ refl _ refl refl = refl
126
127 icong-subst-removable : {i j : I} (eq : i ≅ j)
128 (f : {k : I} → (z : A k) → B z)
129 (x : A i) →
130 f (subst A eq x) ≅ f x
131 icong-subst-removable refl _ _ = refl
132
133 icong-≡-subst-removable : {i j : I} (eq : i ≡ j)
134 (f : {k : I} → (z : A k) → B z)
135 (x : A i) →
136 f (≡.subst A eq x) ≅ f x
137 icong-≡-subst-removable refl _ _ = refl
138
139 ------------------------------------------------------------------------
140 --Proof irrelevance
141
142 ≅-irrelevant : {A B : Set ℓ} → Irrelevant ((A → B → Set ℓ) ∋ λ a → a ≅_)
143 ≅-irrelevant refl refl = refl
144
145 module _ {A C : Set a} {B D : Set ℓ}
146 {w : A} {x : B} {y : C} {z : D} where
147
148 ≅-heterogeneous-irrelevant : (p : w ≅ x) (q : y ≅ z) → x ≅ y → p ≅ q
149 ≅-heterogeneous-irrelevant refl refl refl = refl
150
151 ≅-heterogeneous-irrelevantˡ : (p : w ≅ x) (q : y ≅ z) → w ≅ y → p ≅ q
152 ≅-heterogeneous-irrelevantˡ refl refl refl = refl
153
154 ≅-heterogeneous-irrelevantʳ : (p : w ≅ x) (q : y ≅ z) → x ≅ z → p ≅ q
155 ≅-heterogeneous-irrelevantʳ refl refl refl = refl
156
157 ------------------------------------------------------------------------
158 -- Structures
159
160 isEquivalence : IsEquivalence {A = A} (λ x y → x ≅ y)
161 isEquivalence = record
162 { refl = refl
163 ; sym = sym
164 ; trans = trans
165 }
166
167 setoid : Set ℓ → Setoid ℓ ℓ
168 setoid A = record
169 { Carrier = A
170 ; _≈_ = λ x y → x ≅ y
171 ; isEquivalence = isEquivalence
172 }
173
174 indexedSetoid : (A → Set b) → IndexedSetoid A _ _
175 indexedSetoid B = record
176 { Carrier = B
177 ; _≈_ = λ x y → x ≅ y
178 ; isEquivalence = record
179 { refl = refl
180 ; sym = sym
181 ; trans = trans
182 }
183 }
184
185 ≡↔≅ : ∀ {A : Set a} (B : A → Set b) {x : A} →
186 Inverse (≡.setoid (B x)) ((indexedSetoid B) atₛ x)
187 ≡↔≅ B = record
188 { to = id
189 ; to-cong = ≡-to-≅
190 ; from = id
191 ; from-cong = ≅-to-≡
192 ; inverse = (λ { ≡.refl → refl }) , λ { refl → ≡.refl }
193 }
194
195 decSetoid : Decidable {A = A} {B = A} (λ x y → x ≅ y) →
196 DecSetoid _ _
197 decSetoid dec = record
198 { _≈_ = λ x y → x ≅ y
199 ; isDecEquivalence = record
200 { isEquivalence = isEquivalence
201 ; _≟_ = dec
202 }
203 }
204
205 isPreorder : IsPreorder {A = A} (λ x y → x ≅ y) (λ x y → x ≅ y)
206 isPreorder = record
207 { isEquivalence = isEquivalence
208 ; reflexive = id
209 ; trans = trans
210 }
211
212 isPreorder-≡ : IsPreorder {A = A} _≡_ (λ x y → x ≅ y)
213 isPreorder-≡ = record
214 { isEquivalence = ≡.isEquivalence
215 ; reflexive = reflexive
216 ; trans = trans
217 }
218
219 preorder : Set ℓ → Preorder ℓ ℓ ℓ
220 preorder A = record
221 { Carrier = A
222 ; _≈_ = _≡_
223 ; _≲_ = λ x y → x ≅ y
224 ; isPreorder = isPreorder-≡
225 }
226
227 ------------------------------------------------------------------------
228 -- Convenient syntax for equational reasoning
229
230 module ≅-Reasoning where
231
232 -- The code in `Relation.Binary.Reasoning.Setoid` cannot handle
233 -- heterogeneous equalities, hence the code duplication here.
234
235 infix 4 _IsRelatedTo_
236
237 data _IsRelatedTo_ {A : Set a} {B : Set b} (x : A) (y : B) : Set a where
238 relTo : (x≅y : x ≅ y) → x IsRelatedTo y
239
240 start : ∀ {x : A} {y : B} → x IsRelatedTo y → x ≅ y
241 start (relTo x≅y) = x≅y
242
243 ≡-go : ∀ {A : Set a} {B : Set b} → Trans {A = A} {C = B} _≡_ _IsRelatedTo_ _IsRelatedTo_
244 ≡-go x≡y (relTo y≅z) = relTo (trans (reflexive x≡y) y≅z)
245
246 -- Combinators with one heterogeneous relation
247 module _ {A : Set a} {B : Set b} where
248 open begin-syntax (_IsRelatedTo_ {A = A} {B}) start public
249 open ≡-syntax (_IsRelatedTo_ {A = A} {B}) ≡-go public
250
251 -- Combinators with homogeneous relations
252 module _ {A : Set a} where
253 open end-syntax (_IsRelatedTo_ {A = A}) (relTo refl) public
254
255 -- Can't create syntax in the standard `Syntax` module for
256 -- heterogeneous steps because it would force that module to use
257 -- the `--with-k` option.
258 infixr 2 _≅⟨_⟩_ _≅⟨_⟨_
259
260 _≅⟨_⟩_ : ∀ (x : A) {y : B} {z : C} →
261 x ≅ y → y IsRelatedTo z → x IsRelatedTo z
262 _ ≅⟨ x≅y ⟩ relTo y≅z = relTo (trans x≅y y≅z)
263
264 _≅⟨_⟨_ : ∀ (x : A) {y : B} {z : C} →
265 y ≅ x → y IsRelatedTo z → x IsRelatedTo z
266 _ ≅⟨ y≅x ⟨ relTo y≅z = relTo (trans (sym y≅x) y≅z)
267
268 -- Deprecated
269 infixr 2 _≅˘⟨_⟩_
270 _≅˘⟨_⟩_ = _≅⟨_⟨_
271 {-# WARNING_ON_USAGE _≅˘⟨_⟩_
272 "Warning: _≅˘⟨_⟩_ was deprecated in v2.0.
273 Please use _≅⟨_⟨_ instead."
274 #-}
275
276 ------------------------------------------------------------------------
277 -- Inspect
278
279 -- Inspect can be used when you want to pattern match on the result r
280 -- of some expression e, and you also need to "remember" that r ≡ e.
281
282 record Reveal_·_is_ {A : Set a} {B : A → Set b}
283 (f : (x : A) → B x) (x : A) (y : B x) :
284 Set (a ⊔ b) where
285 constructor [_]
286 field eq : f x ≅ y
287
288 inspect : ∀ {A : Set a} {B : A → Set b}
289 (f : (x : A) → B x) (x : A) → Reveal f · x is f x
290 inspect f x = [ refl ]
291
292 -- Example usage:
293
294 -- f x y with g x | inspect g x
295 -- f x y | c z | [ eq ] = ...