Debian Codegraph @

src/Relation/Binary/Reasoning/Syntax.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Syntax for the building blocks of equational reasoning modules
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 -- List of `Reasoning` modules that do not use this framework and so
10 -- need to be updated manually if the syntax changes.
11 --
12 -- Data/Vec/Relation/Binary/Equality/Cast
13 -- Relation/Binary/HeterogeneousEquality
14 -- Effect/Monad/Partiality
15 -- Effect/Monad/Partiality/All
16 -- Codata/Guarded/Stream/Relation/Binary/Pointwise
17 -- Function/Reasoning
18
19 module Relation.Binary.Reasoning.Syntax where
20
21 open import Level using (Level; _⊔_; suc)
22 open import Relation.Nullary.Decidable.Core
23 using (Dec; True; toWitness)
24 open import Relation.Nullary.Negation.Core using (contradiction)
25 open import Relation.Binary.Core using (Rel; REL; _⇒_)
26 open import Relation.Binary.Definitions
27 using (_Respectsʳ_; Asymmetric; Trans; Sym; Reflexive)
28 open import Relation.Binary.PropositionalEquality.Core as ≡
29 using (_≡_)
30
31 private
32 variable
33 a ℓ₁ ℓ₂ ℓ₃ ℓ₄ : Level
34 A B C : Set a
35 x y z : A
36
37 ------------------------------------------------------------------------
38 -- Syntax for beginning a reasoning chain
39 ------------------------------------------------------------------------
40
41 ------------------------------------------------------------------------
42 -- Basic begin syntax
43
44 module begin-syntax
45 (R : REL A B ℓ₁)
46 {S : REL A B ℓ₂}
47 (reflexive : R ⇒ S)
48 where
49
50 infix 1 begin_
51
52 begin_ : R x y → S x y
53 begin_ = reflexive
54
55 ------------------------------------------------------------------------
56 -- Begin subrelation syntax
57
58 -- Sometimes we want to support sub-relations with the
59 -- same reasoning operators as the main relations (e.g. perform equality
60 -- proofs with non-strict reasoning operators). This record bundles all
61 -- the parts needed to extract the sub-relation proofs.
62 record SubRelation {A : Set a} (R : Rel A ℓ₁) ℓ₂ ℓ₃ : Set (a ⊔ ℓ₁ ⊔ suc ℓ₂ ⊔ suc ℓ₃) where
63 field
64 S : Rel A ℓ₂
65 IsS : R x y → Set ℓ₃
66 IsS? : ∀ (xRy : R x y) → Dec (IsS xRy)
67 extract : ∀ {xRy : R x y} → IsS xRy → S x y
68
69 module begin-subrelation-syntax
70 (R : Rel A ℓ₁)
71 (sub : SubRelation R ℓ₂ ℓ₃)
72 where
73 open SubRelation sub
74
75 infix 1 begin_
76
77 begin_ : ∀ {x y} (xRy : R x y) → {s : True (IsS? xRy)} → S x y
78 begin_ r {s} = extract (toWitness s)
79
80 -- Begin equality syntax
81 module begin-equality-syntax
82 (R : Rel A ℓ₁)
83 (sub : SubRelation R ℓ₂ ℓ₃) where
84
85 open begin-subrelation-syntax R sub public
86 renaming (begin_ to begin-equality_)
87
88 -- Begin apartness syntax
89 module begin-apartness-syntax
90 (R : Rel A ℓ₁)
91 (sub : SubRelation R ℓ₂ ℓ₃) where
92
93 open begin-subrelation-syntax R sub public
94 renaming (begin_ to begin-apartness_)
95
96 -- Begin strict syntax
97 module begin-strict-syntax
98 (R : Rel A ℓ₁)
99 (sub : SubRelation R ℓ₂ ℓ₃) where
100
101 open begin-subrelation-syntax R sub public
102 renaming (begin_ to begin-strict_)
103
104 ------------------------------------------------------------------------
105 -- Begin membership syntax
106
107 module begin-membership-syntax
108 (R : Rel A ℓ₁)
109 (_∈_ : REL B A ℓ₂)
110 (resp : _∈_ Respectsʳ R) where
111
112 infix 1 step-∈
113
114 step-∈ : ∀ (x : B) {xs ys} → R xs ys → x ∈ xs → x ∈ ys
115 step-∈ x = resp
116
117 syntax step-∈ x xs⊆ys x∈xs = x ∈⟨ x∈xs ⟩ xs⊆ys
118
119 ------------------------------------------------------------------------
120 -- Begin contradiction syntax
121
122 -- Used with asymmetric subrelations to derive a contradiction from a
123 -- proof that an element is related to itself.
124 module begin-contradiction-syntax
125 (R : Rel A ℓ₁)
126 (sub : SubRelation R ℓ₂ ℓ₃)
127 (asym : Asymmetric (SubRelation.S sub))
128 where
129
130 open SubRelation sub
131
132 infix 1 begin-contradiction_
133
134 begin-contradiction_ : ∀ (xRx : R x x) {s : True (IsS? xRx)} →
135 ∀ {b} {B : Set b} → B
136 begin-contradiction_ {x} r {s} = contradiction x<x (asym x<x)
137 where
138 x<x : S x x
139 x<x = extract (toWitness s)
140
141 ------------------------------------------------------------------------
142 -- Syntax for continuing a chain of reasoning steps
143 ------------------------------------------------------------------------
144
145 -- Note that the arguments to the `step`s are not provided in their
146 -- "natural" order and syntax declarations are later used to re-order
147 -- them. This is because the `step` ordering allows the type-checker to
148 -- better infer the middle argument `y` from the `_IsRelatedTo_`
149 -- argument (see issue 622).
150 --
151 -- This has two practical benefits. First it speeds up type-checking by
152 -- approximately a factor of 5. Secondly it allows the combinators to be
153 -- used with macros that use reflection, e.g. `Tactic.RingSolver`, where
154 -- they need to be able to extract `y` using reflection.
155
156 ------------------------------------------------------------------------
157 -- Syntax for unidirectional relations
158
159 -- See https://github.com/agda/agda-stdlib/issues/2150 for a possible
160 -- simplification.
161
162 module _
163 {R : REL A B ℓ₂}
164 (S : REL B C ℓ₁)
165 (T : REL A C ℓ₃)
166 (step : Trans R S T)
167 where
168
169 forward : ∀ (x : A) {y z} → S y z → R x y → T x z
170 forward x yRz x∼y = step {x} x∼y yRz
171
172 -- Arbitrary relation syntax
173 module ∼-syntax where
174 infixr 2 step-∼
175 step-∼ = forward
176 syntax step-∼ x yRz x∼y = x ∼⟨ x∼y ⟩ yRz
177
178
179 -- Preorder syntax
180 module ≲-syntax where
181 infixr 2 step-≲
182 step-≲ = forward
183 syntax step-≲ x yRz x≲y = x ≲⟨ x≲y ⟩ yRz
184
185
186 -- Partial order syntax
187 module ≤-syntax where
188 infixr 2 step-≤
189 step-≤ = forward
190 syntax step-≤ x yRz x≤y = x ≤⟨ x≤y ⟩ yRz
191
192
193 -- Strict partial order syntax
194 module <-syntax where
195 infixr 2 step-<
196 step-< = forward
197 syntax step-< x yRz x<y = x <⟨ x<y ⟩ yRz
198
199
200 -- Subset order syntax
201 module ⊆-syntax where
202 infixr 2 step-⊆
203 step-⊆ = forward
204 syntax step-⊆ x yRz x⊆y = x ⊆⟨ x⊆y ⟩ yRz
205
206
207 -- Strict subset order syntax
208 module ⊂-syntax where
209 infixr 2 step-⊂
210 step-⊂ = forward
211 syntax step-⊂ x yRz x⊂y = x ⊂⟨ x⊂y ⟩ yRz
212
213
214 -- Square subset order syntax
215 module ⊑-syntax where
216 infixr 2 step-⊑
217 step-⊑ = forward
218 syntax step-⊑ x yRz x⊑y = x ⊑⟨ x⊑y ⟩ yRz
219
220
221 -- Strict square subset order syntax
222 module ⊏-syntax where
223 infixr 2 step-⊏
224 step-⊏ = forward
225 syntax step-⊏ x yRz x⊏y = x ⊏⟨ x⊏y ⟩ yRz
226
227
228 -- Divisibility syntax
229 module ∣-syntax where
230 infixr 2 step-∣
231 step-∣ = forward
232 syntax step-∣ x yRz x∣y = x ∣⟨ x∣y ⟩ yRz
233
234
235 -- Single-step syntax
236 module ⟶-syntax where
237 infixr 2 step-⟶
238 step-⟶ = forward
239 syntax step-⟶ x yRz x∣y = x ⟶⟨ x∣y ⟩ yRz
240
241
242 -- Multi-step syntax
243 module ⟶*-syntax where
244 infixr 2 step-⟶*
245 step-⟶* = forward
246 syntax step-⟶* x yRz x∣y = x ⟶*⟨ x∣y ⟩ yRz
247
248
249 ------------------------------------------------------------------------
250 -- Syntax for bidirectional relations
251
252 module _
253 {U : REL B A ℓ₄}
254 (sym : Sym U R)
255 where
256
257 backward : ∀ x {y z} → S y z → U y x → T x z
258 backward x yRz x≈y = forward x yRz (sym x≈y)
259
260 -- Setoid equality syntax
261 module ≈-syntax where
262 infixr 2 step-≈-⟩ step-≈-⟨
263 step-≈-⟩ = forward
264 step-≈-⟨ = backward
265 syntax step-≈-⟩ x yRz x≈y = x ≈⟨ x≈y ⟩ yRz
266 syntax step-≈-⟨ x yRz y≈x = x ≈⟨ y≈x ⟨ yRz
267
268 -- Deprecated
269 infixr 2 step-≈ step-≈˘
270 step-≈ = step-≈-⟩
271 {-# WARNING_ON_USAGE step-≈
272 "Warning: step-≈ was deprecated in v2.0.
273 Please use step-≈-⟩ instead."
274 #-}
275 step-≈˘ = step-≈-⟨
276 {-# WARNING_ON_USAGE step-≈˘
277 "Warning: step-≈˘ and _≈˘⟨_⟩_ was deprecated in v2.0.
278 Please use step-≈-⟨ and _≈⟨_⟨_ instead."
279 #-}
280 syntax step-≈˘ x yRz y≈x = x ≈˘⟨ y≈x ⟩ yRz
281
282
283 -- Container equality syntax
284 module ≋-syntax where
285 infixr 2 step-≋-⟩ step-≋-⟨
286 step-≋-⟩ = forward
287 step-≋-⟨ = backward
288 syntax step-≋-⟩ x yRz x≋y = x ≋⟨ x≋y ⟩ yRz
289 syntax step-≋-⟨ x yRz y≋x = x ≋⟨ y≋x ⟨ yRz
290
291
292 -- Don't remove until https://github.com/agda/agda/issues/5617 fixed.
293 infixr 2 step-≋ step-≋˘
294 step-≋ = step-≋-⟩
295 {-# WARNING_ON_USAGE step-≋
296 "Warning: step-≋ was deprecated in v2.0.
297 Please use step-≋-⟩ instead."
298 #-}
299 step-≋˘ = step-≋-⟨
300 {-# WARNING_ON_USAGE step-≋˘
301 "Warning: step-≋˘ and _≋˘⟨_⟩_ was deprecated in v2.0.
302 Please use step-≋-⟨ and _≋⟨_⟨_ instead."
303 #-}
304 syntax step-≋˘ x yRz y≋x = x ≋˘⟨ y≋x ⟩ yRz
305
306
307 -- Other equality syntax
308 module ≃-syntax where
309 infixr 2 step-≃-⟩ step-≃-⟨
310 step-≃-⟩ = forward
311 step-≃-⟨ = backward
312 syntax step-≃-⟩ x yRz x≃y = x ≃⟨ x≃y ⟩ yRz
313 syntax step-≃-⟨ x yRz y≃x = x ≃⟨ y≃x ⟨ yRz
314
315
316 -- Apartness relation syntax
317 module #-syntax where
318 infixr 2 step-#-⟩ step-#-⟨
319 step-#-⟩ = forward
320 step-#-⟨ = backward
321 syntax step-#-⟩ x yRz x#y = x #⟨ x#y ⟩ yRz
322 syntax step-#-⟨ x yRz y#x = x #⟨ y#x ⟨ yRz
323
324 -- Don't remove until https://github.com/agda/agda/issues/5617 fixed.
325 infixr 2 step-# step-#˘
326 step-# = step-#-⟩
327 {-# WARNING_ON_USAGE step-#
328 "Warning: step-# was deprecated in v2.0.
329 Please use step-#-⟩ instead."
330 #-}
331 step-#˘ = step-#-⟨
332 {-# WARNING_ON_USAGE step-#˘
333 "Warning: step-#˘ and _#˘⟨_⟩_ was deprecated in v2.0.
334 Please use step-#-⟨ and _#⟨_⟨_ instead."
335 #-}
336 syntax step-#˘ x yRz y#x = x #˘⟨ y#x ⟩ yRz
337
338
339 -- Bijection syntax
340 module ⤖-syntax where
341 infixr 2 step-⤖ step-⬻
342 step-⤖ = forward
343 step-⬻ = backward
344 syntax step-⤖ x yRz x⤖y = x ⤖⟨ x⤖y ⟩ yRz
345 syntax step-⬻ x yRz y⤖x = x ⬻⟨ y⤖x ⟩ yRz
346
347
348 -- Inverse syntax
349 module ↔-syntax where
350 infixr 2 step-↔-⟩ step-↔-⟨
351 step-↔-⟩ = forward
352 step-↔-⟨ = backward
353 syntax step-↔-⟩ x yRz x↔y = x ↔⟨ x↔y ⟩ yRz
354 syntax step-↔-⟨ x yRz y↔x = x ↔⟨ y↔x ⟨ yRz
355
356
357 -- Inverse syntax
358 module ↭-syntax where
359 infixr 2 step-↭-⟩ step-↭-⟨
360 step-↭-⟩ = forward
361 step-↭-⟨ = backward
362 syntax step-↭-⟩ x yRz x↭y = x ↭⟨ x↭y ⟩ yRz
363 syntax step-↭-⟨ x yRz y↭x = x ↭⟨ y↭x ⟨ yRz
364
365
366 -- Don't remove until https://github.com/agda/agda/issues/5617 fixed.
367 infixr 2 step-↭ step-↭˘
368 step-↭ = forward
369 {-# WARNING_ON_USAGE step-↭
370 "Warning: step-↭ was deprecated in v2.0.
371 Please use step-↭-⟩ instead."
372 #-}
373 step-↭˘ = backward
374 {-# WARNING_ON_USAGE step-↭˘
375 "Warning: step-↭˘ and _↭˘⟨_⟩_ was deprecated in v2.0.
376 Please use step-↭-⟨ and _↭⟨_⟨_ instead."
377 #-}
378 syntax step-↭˘ x yRz y↭x = x ↭˘⟨ y↭x ⟩ yRz
379
380 ------------------------------------------------------------------------
381 -- Propositional equality
382
383 -- Crucially often the step function cannot just be `subst` or pattern
384 -- match on `refl` as we often want to compute which constructor the
385 -- relation begins with, in order for the implicit subrelation
386 -- arguments to resolve. See `≡-noncomputable-syntax` below if this
387 -- is not required.
388 module ≡-syntax
389 (R : REL A B ℓ₁)
390 (step : Trans _≡_ R R)
391 where
392
393 infixr 2 step-≡-⟩ step-≡-∣ step-≡-⟨
394 step-≡-⟩ = forward R R step
395
396 step-≡-∣ : ∀ x {y} → R x y → R x y
397 step-≡-∣ x xRy = xRy
398
399 step-≡-⟨ = backward R R step ≡.sym
400
401 syntax step-≡-⟩ x yRz x≡y = x ≡⟨ x≡y ⟩ yRz
402 syntax step-≡-∣ x xRy = x ≡⟨⟩ xRy
403 syntax step-≡-⟨ x yRz y≡x = x ≡⟨ y≡x ⟨ yRz
404
405
406 -- Don't remove until https://github.com/agda/agda/issues/5617 fixed.
407 infixr 2 step-≡ step-≡˘
408 step-≡ = step-≡-⟩
409 {-# WARNING_ON_USAGE step-≡
410 "Warning: step-≡ was deprecated in v2.0.
411 Please use step-≡-⟩ instead."
412 #-}
413 step-≡˘ = step-≡-⟨
414 {-# WARNING_ON_USAGE step-≡˘
415 "Warning: step-≡˘ and _≡˘⟨_⟩_ was deprecated in v2.0.
416 Please use step-≡-⟨ and _≡⟨_⟨_ instead."
417 #-}
418 syntax step-≡˘ x yRz y≡x = x ≡˘⟨ y≡x ⟩ yRz
419
420
421 -- Unlike ≡-syntax above, chains of reasoning using this syntax will not
422 -- reduce when proofs of propositional equality which are not definitionally
423 -- equal to `refl` are passed.
424 module ≡-noncomputing-syntax (R : REL A B ℓ₁) where
425
426 private
427 step : Trans _≡_ R R
428 step ≡.refl xRy = xRy
429
430 open ≡-syntax R step public
431
432 ------------------------------------------------------------------------
433 -- Syntax for ending a chain of reasoning
434 ------------------------------------------------------------------------
435
436 module end-syntax
437 (R : Rel A ℓ₁)
438 (reflexive : Reflexive R)
439 where
440
441 infix 3 _∎
442
443 _∎ : ∀ x → R x x
444 x ∎ = reflexive
445