Debian Codegraph @

src/Data/Bool/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A bunch of properties
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Bool.Properties where
10 open import Algebra.Bundles
11 using (Magma; Semigroup; Band; CommutativeMonoid
12 ; IdempotentCommutativeMonoid; CommutativeSemiring; CommutativeRing)
13 open import Algebra.Lattice.Bundles
14 using (Lattice; DistributiveLattice; BooleanAlgebra; Semilattice)
15 import Algebra.Lattice.Properties.BooleanAlgebra as BooleanAlgebraProperties
16 open import Data.Bool.Base
17 using (Bool; true; false; not; _∧_; _∨_; _xor_ ; if_then_else_; T; _≤_; _<_
18 ; b≤b; f≤t; f<t)
19 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
20 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_])
21 open import Function.Base using (_⟨_⟩_; const; id)
22 open import Function.Bundles hiding (Inverse; LeftInverse; RightInverse)
23 open import Induction.WellFounded using (Acc; WellFounded; acc)
24 open import Level using (0ℓ; Level)
25 open import Relation.Binary.Bundles using (DecSetoid; DecTotalOrder; Poset;
26 Preorder; Setoid; StrictPartialOrder; StrictTotalOrder; TotalOrder)
27 open import Relation.Binary.Core using (_⇒_)
28 open import Relation.Binary.Structures
29 using (IsPreorder; IsPartialOrder; IsTotalOrder; IsDecTotalOrder
30 ; IsStrictPartialOrder; IsStrictTotalOrder)
31 open import Relation.Binary.Bundles
32 using (Setoid; DecSetoid; Poset; Preorder; TotalOrder; DecTotalOrder
33 ; StrictPartialOrder; StrictTotalOrder)
34 open import Relation.Binary.Definitions
35 using (Decidable; DecidableEquality ; Reflexive; Transitive; Antisymmetric
36 ; Minimum; Maximum; Total; Irrelevant ; Irreflexive; Asymmetric; Trans
37 ; Trichotomous; tri≈; tri<; tri>; _Respects₂_)
38 open import Relation.Binary.PropositionalEquality.Core
39 using (_≡_; refl; sym; cong; cong₂; subst; trans; _≢_)
40 open import Relation.Binary.PropositionalEquality.Properties
41 using (module ≡-Reasoning; setoid; decSetoid; isEquivalence)
42 open import Relation.Nullary.Decidable.Core
43 using (True; yes; no; fromWitness ; toWitness)
44 open import Relation.Nullary.Negation.Core using (contradiction)
45 import Relation.Unary as U
46
47 open import Algebra.Definitions {A = Bool} _≡_
48 open import Algebra.Structures {A = Bool} _≡_
49 open import Algebra.Lattice.Structures {A = Bool} _≡_
50
51 open ≡-Reasoning
52
53 private
54 variable
55 a b : Level
56 A : Set a
57 B : Set b
58
59 ------------------------------------------------------------------------
60 -- Properties of _≡_
61
62 infix 4 _≟_
63
64 _≟_ : DecidableEquality Bool
65 true ≟ true = yes refl
66 false ≟ false = yes refl
67 true ≟ false = no λ()
68 false ≟ true = no λ()
69
70 ≡-setoid : Setoid 0ℓ 0ℓ
71 ≡-setoid = setoid Bool
72
73 ≡-decSetoid : DecSetoid 0ℓ 0ℓ
74 ≡-decSetoid = decSetoid _≟_
75
76 ------------------------------------------------------------------------
77 -- Properties of _≤_
78
79 -- Relational properties
80
81 ≤-reflexive : _≡_ ⇒ _≤_
82 ≤-reflexive refl = b≤b
83
84 ≤-refl : Reflexive _≤_
85 ≤-refl = ≤-reflexive refl
86
87 ≤-trans : Transitive _≤_
88 ≤-trans b≤b p = p
89 ≤-trans f≤t b≤b = f≤t
90
91 ≤-antisym : Antisymmetric _≡_ _≤_
92 ≤-antisym b≤b _ = refl
93
94 ≤-minimum : Minimum _≤_ false
95 ≤-minimum false = b≤b
96 ≤-minimum true = f≤t
97
98 ≤-maximum : Maximum _≤_ true
99 ≤-maximum false = f≤t
100 ≤-maximum true = b≤b
101
102 ≤-total : Total _≤_
103 ≤-total false b = inj₁ (≤-minimum b)
104 ≤-total true b = inj₂ (≤-maximum b)
105
106 infix 4 _≤?_
107
108 _≤?_ : Decidable _≤_
109 false ≤? b = yes (≤-minimum b)
110 true ≤? false = no λ ()
111 true ≤? true = yes b≤b
112
113 ≤-irrelevant : Irrelevant _≤_
114 ≤-irrelevant {_} f≤t f≤t = refl
115 ≤-irrelevant {false} b≤b b≤b = refl
116 ≤-irrelevant {true} b≤b b≤b = refl
117
118 -- Structures
119
120 ≤-isPreorder : IsPreorder _≡_ _≤_
121 ≤-isPreorder = record
122 { isEquivalence = isEquivalence
123 ; reflexive = ≤-reflexive
124 ; trans = ≤-trans
125 }
126
127 ≤-isPartialOrder : IsPartialOrder _≡_ _≤_
128 ≤-isPartialOrder = record
129 { isPreorder = ≤-isPreorder
130 ; antisym = ≤-antisym
131 }
132
133 ≤-isTotalOrder : IsTotalOrder _≡_ _≤_
134 ≤-isTotalOrder = record
135 { isPartialOrder = ≤-isPartialOrder
136 ; total = ≤-total
137 }
138
139 ≤-isDecTotalOrder : IsDecTotalOrder _≡_ _≤_
140 ≤-isDecTotalOrder = record
141 { isTotalOrder = ≤-isTotalOrder
142 ; _≟_ = _≟_
143 ; _≤?_ = _≤?_
144 }
145
146 -- Bundles
147
148 ≤-poset : Poset 0ℓ 0ℓ 0ℓ
149 ≤-poset = record
150 { isPartialOrder = ≤-isPartialOrder
151 }
152
153 ≤-preorder : Preorder 0ℓ 0ℓ 0ℓ
154 ≤-preorder = record
155 { isPreorder = ≤-isPreorder
156 }
157
158 ≤-totalOrder : TotalOrder 0ℓ 0ℓ 0ℓ
159 ≤-totalOrder = record
160 { isTotalOrder = ≤-isTotalOrder
161 }
162
163 ≤-decTotalOrder : DecTotalOrder 0ℓ 0ℓ 0ℓ
164 ≤-decTotalOrder = record
165 { isDecTotalOrder = ≤-isDecTotalOrder
166 }
167
168 ------------------------------------------------------------------------
169 -- Properties of _<_
170
171 -- Relational properties
172
173 <-irrefl : Irreflexive _≡_ _<_
174 <-irrefl refl ()
175
176 <-asym : Asymmetric _<_
177 <-asym f<t ()
178
179 <-trans : Transitive _<_
180 <-trans f<t ()
181
182 <-transʳ : Trans _≤_ _<_ _<_
183 <-transʳ b≤b f<t = f<t
184
185 <-transˡ : Trans _<_ _≤_ _<_
186 <-transˡ f<t b≤b = f<t
187
188 <-cmp : Trichotomous _≡_ _<_
189 <-cmp false false = tri≈ (λ()) refl (λ())
190 <-cmp false true = tri< f<t (λ()) (λ())
191 <-cmp true false = tri> (λ()) (λ()) f<t
192 <-cmp true true = tri≈ (λ()) refl (λ())
193
194 infix 4 _<?_
195
196 _<?_ : Decidable _<_
197 false <? false = no (λ())
198 false <? true = yes f<t
199 true <? _ = no (λ())
200
201 <-resp₂-≡ : _<_ Respects₂ _≡_
202 <-resp₂-≡ = subst (_ <_) , subst (_< _)
203
204 <-irrelevant : Irrelevant _<_
205 <-irrelevant f<t f<t = refl
206
207 <-wellFounded : WellFounded _<_
208 <-wellFounded _ = acc <-acc
209 where
210 <-acc : ∀ {x y} → y < x → Acc _<_ y
211 <-acc f<t = acc λ ()
212
213 -- Structures
214
215 <-isStrictPartialOrder : IsStrictPartialOrder _≡_ _<_
216 <-isStrictPartialOrder = record
217 { isEquivalence = isEquivalence
218 ; irrefl = <-irrefl
219 ; trans = <-trans
220 ; <-resp-≈ = <-resp₂-≡
221 }
222
223 <-isStrictTotalOrder : IsStrictTotalOrder _≡_ _<_
224 <-isStrictTotalOrder = record
225 { isStrictPartialOrder = <-isStrictPartialOrder
226 ; compare = <-cmp
227 }
228
229 -- Bundles
230
231 <-strictPartialOrder : StrictPartialOrder 0ℓ 0ℓ 0ℓ
232 <-strictPartialOrder = record
233 { isStrictPartialOrder = <-isStrictPartialOrder
234 }
235
236 <-strictTotalOrder : StrictTotalOrder 0ℓ 0ℓ 0ℓ
237 <-strictTotalOrder = record
238 { isStrictTotalOrder = <-isStrictTotalOrder
239 }
240
241 ------------------------------------------------------------------------
242 -- Properties of _∨_
243
244 ∨-assoc : Associative _∨_
245 ∨-assoc true y z = refl
246 ∨-assoc false y z = refl
247
248 ∨-comm : Commutative _∨_
249 ∨-comm true true = refl
250 ∨-comm true false = refl
251 ∨-comm false true = refl
252 ∨-comm false false = refl
253
254 ∨-identityˡ : LeftIdentity false _∨_
255 ∨-identityˡ _ = refl
256
257 ∨-identityʳ : RightIdentity false _∨_
258 ∨-identityʳ false = refl
259 ∨-identityʳ true = refl
260
261 ∨-identity : Identity false _∨_
262 ∨-identity = ∨-identityˡ , ∨-identityʳ
263
264 ∨-zeroˡ : LeftZero true _∨_
265 ∨-zeroˡ _ = refl
266
267 ∨-zeroʳ : RightZero true _∨_
268 ∨-zeroʳ false = refl
269 ∨-zeroʳ true = refl
270
271 ∨-zero : Zero true _∨_
272 ∨-zero = ∨-zeroˡ , ∨-zeroʳ
273
274 ∨-inverseˡ : LeftInverse true not _∨_
275 ∨-inverseˡ false = refl
276 ∨-inverseˡ true = refl
277
278 ∨-inverseʳ : RightInverse true not _∨_
279 ∨-inverseʳ x = ∨-comm x (not x) ⟨ trans ⟩ ∨-inverseˡ x
280
281 ∨-inverse : Inverse true not _∨_
282 ∨-inverse = ∨-inverseˡ , ∨-inverseʳ
283
284 ∨-idem : Idempotent _∨_
285 ∨-idem false = refl
286 ∨-idem true = refl
287
288 ∨-sel : Selective _∨_
289 ∨-sel false y = inj₂ refl
290 ∨-sel true y = inj₁ refl
291
292 ∨-conicalˡ : LeftConical false _∨_
293 ∨-conicalˡ false false _ = refl
294
295 ∨-conicalʳ : RightConical false _∨_
296 ∨-conicalʳ false false _ = refl
297
298 ∨-conical : Conical false _∨_
299 ∨-conical = ∨-conicalˡ , ∨-conicalʳ
300
301 ∨-isMagma : IsMagma _∨_
302 ∨-isMagma = record
303 { isEquivalence = isEquivalence
304 ; ∙-cong = cong₂ _∨_
305 }
306
307 ∨-magma : Magma 0ℓ 0ℓ
308 ∨-magma = record
309 { isMagma = ∨-isMagma
310 }
311
312 ∨-isSemigroup : IsSemigroup _∨_
313 ∨-isSemigroup = record
314 { isMagma = ∨-isMagma
315 ; assoc = ∨-assoc
316 }
317
318 ∨-semigroup : Semigroup 0ℓ 0ℓ
319 ∨-semigroup = record
320 { isSemigroup = ∨-isSemigroup
321 }
322
323 ∨-isBand : IsBand _∨_
324 ∨-isBand = record
325 { isSemigroup = ∨-isSemigroup
326 ; idem = ∨-idem
327 }
328
329 ∨-band : Band 0ℓ 0ℓ
330 ∨-band = record
331 { isBand = ∨-isBand
332 }
333
334 ∨-isSemilattice : IsSemilattice _∨_
335 ∨-isSemilattice = record
336 { isBand = ∨-isBand
337 ; comm = ∨-comm
338 }
339
340 ∨-semilattice : Semilattice 0ℓ 0ℓ
341 ∨-semilattice = record
342 { isSemilattice = ∨-isSemilattice
343 }
344
345 ∨-isMonoid : IsMonoid _∨_ false
346 ∨-isMonoid = record
347 { isSemigroup = ∨-isSemigroup
348 ; identity = ∨-identity
349 }
350
351 ∨-isCommutativeMonoid : IsCommutativeMonoid _∨_ false
352 ∨-isCommutativeMonoid = record
353 { isMonoid = ∨-isMonoid
354 ; comm = ∨-comm
355 }
356
357 ∨-commutativeMonoid : CommutativeMonoid 0ℓ 0ℓ
358 ∨-commutativeMonoid = record
359 { isCommutativeMonoid = ∨-isCommutativeMonoid
360 }
361
362 ∨-isIdempotentCommutativeMonoid :
363 IsIdempotentCommutativeMonoid _∨_ false
364 ∨-isIdempotentCommutativeMonoid = record
365 { isCommutativeMonoid = ∨-isCommutativeMonoid
366 ; idem = ∨-idem
367 }
368
369 ∨-idempotentCommutativeMonoid : IdempotentCommutativeMonoid 0ℓ 0ℓ
370 ∨-idempotentCommutativeMonoid = record
371 { isIdempotentCommutativeMonoid = ∨-isIdempotentCommutativeMonoid
372 }
373
374 ------------------------------------------------------------------------
375 -- Properties of _∧_
376
377 ∧-assoc : Associative _∧_
378 ∧-assoc true y z = refl
379 ∧-assoc false y z = refl
380
381 ∧-comm : Commutative _∧_
382 ∧-comm true true = refl
383 ∧-comm true false = refl
384 ∧-comm false true = refl
385 ∧-comm false false = refl
386
387 ∧-identityˡ : LeftIdentity true _∧_
388 ∧-identityˡ _ = refl
389
390 ∧-identityʳ : RightIdentity true _∧_
391 ∧-identityʳ false = refl
392 ∧-identityʳ true = refl
393
394 ∧-identity : Identity true _∧_
395 ∧-identity = ∧-identityˡ , ∧-identityʳ
396
397 ∧-zeroˡ : LeftZero false _∧_
398 ∧-zeroˡ _ = refl
399
400 ∧-zeroʳ : RightZero false _∧_
401 ∧-zeroʳ false = refl
402 ∧-zeroʳ true = refl
403
404 ∧-zero : Zero false _∧_
405 ∧-zero = ∧-zeroˡ , ∧-zeroʳ
406
407 ∧-inverseˡ : LeftInverse false not _∧_
408 ∧-inverseˡ false = refl
409 ∧-inverseˡ true = refl
410
411 ∧-inverseʳ : RightInverse false not _∧_
412 ∧-inverseʳ x = ∧-comm x (not x) ⟨ trans ⟩ ∧-inverseˡ x
413
414 ∧-inverse : Inverse false not _∧_
415 ∧-inverse = ∧-inverseˡ , ∧-inverseʳ
416
417 ∧-idem : Idempotent _∧_
418 ∧-idem false = refl
419 ∧-idem true = refl
420
421 ∧-sel : Selective _∧_
422 ∧-sel false y = inj₁ refl
423 ∧-sel true y = inj₂ refl
424
425 ∧-conicalˡ : LeftConical true _∧_
426 ∧-conicalˡ true true _ = refl
427
428 ∧-conicalʳ : RightConical true _∧_
429 ∧-conicalʳ true true _ = refl
430
431 ∧-conical : Conical true _∧_
432 ∧-conical = ∧-conicalˡ , ∧-conicalʳ
433
434 ∧-distribˡ-∨ : _∧_ DistributesOverˡ _∨_
435 ∧-distribˡ-∨ true y z = refl
436 ∧-distribˡ-∨ false y z = refl
437
438 ∧-distribʳ-∨ : _∧_ DistributesOverʳ _∨_
439 ∧-distribʳ-∨ x y z = begin
440 (y ∨ z) ∧ x ≡⟨ ∧-comm (y ∨ z) x ⟩
441 x ∧ (y ∨ z) ≡⟨ ∧-distribˡ-∨ x y z ⟩
442 x ∧ y ∨ x ∧ z ≡⟨ cong₂ _∨_ (∧-comm x y) (∧-comm x z) ⟩
443 y ∧ x ∨ z ∧ x ∎
444
445 ∧-distrib-∨ : _∧_ DistributesOver _∨_
446 ∧-distrib-∨ = ∧-distribˡ-∨ , ∧-distribʳ-∨
447
448 ∨-distribˡ-∧ : _∨_ DistributesOverˡ _∧_
449 ∨-distribˡ-∧ true y z = refl
450 ∨-distribˡ-∧ false y z = refl
451
452 ∨-distribʳ-∧ : _∨_ DistributesOverʳ _∧_
453 ∨-distribʳ-∧ x y z = begin
454 (y ∧ z) ∨ x ≡⟨ ∨-comm (y ∧ z) x ⟩
455 x ∨ (y ∧ z) ≡⟨ ∨-distribˡ-∧ x y z ⟩
456 (x ∨ y) ∧ (x ∨ z) ≡⟨ cong₂ _∧_ (∨-comm x y) (∨-comm x z) ⟩
457 (y ∨ x) ∧ (z ∨ x) ∎
458
459 ∨-distrib-∧ : _∨_ DistributesOver _∧_
460 ∨-distrib-∧ = ∨-distribˡ-∧ , ∨-distribʳ-∧
461
462 ∧-abs-∨ : _∧_ Absorbs _∨_
463 ∧-abs-∨ true y = refl
464 ∧-abs-∨ false y = refl
465
466 ∨-abs-∧ : _∨_ Absorbs _∧_
467 ∨-abs-∧ true y = refl
468 ∨-abs-∧ false y = refl
469
470 ∨-∧-absorptive : Absorptive _∨_ _∧_
471 ∨-∧-absorptive = ∨-abs-∧ , ∧-abs-∨
472
473 ∧-isMagma : IsMagma _∧_
474 ∧-isMagma = record
475 { isEquivalence = isEquivalence
476 ; ∙-cong = cong₂ _∧_
477 }
478
479 ∧-magma : Magma 0ℓ 0ℓ
480 ∧-magma = record
481 { isMagma = ∧-isMagma
482 }
483
484 ∧-isSemigroup : IsSemigroup _∧_
485 ∧-isSemigroup = record
486 { isMagma = ∧-isMagma
487 ; assoc = ∧-assoc
488 }
489
490 ∧-semigroup : Semigroup 0ℓ 0ℓ
491 ∧-semigroup = record
492 { isSemigroup = ∧-isSemigroup
493 }
494
495 ∧-isBand : IsBand _∧_
496 ∧-isBand = record
497 { isSemigroup = ∧-isSemigroup
498 ; idem = ∧-idem
499 }
500
501 ∧-band : Band 0ℓ 0ℓ
502 ∧-band = record
503 { isBand = ∧-isBand
504 }
505
506 ∧-isSemilattice : IsSemilattice _∧_
507 ∧-isSemilattice = record
508 { isBand = ∧-isBand
509 ; comm = ∧-comm
510 }
511
512 ∧-semilattice : Semilattice 0ℓ 0ℓ
513 ∧-semilattice = record
514 { isSemilattice = ∧-isSemilattice
515 }
516
517 ∧-isMonoid : IsMonoid _∧_ true
518 ∧-isMonoid = record
519 { isSemigroup = ∧-isSemigroup
520 ; identity = ∧-identity
521 }
522
523 ∧-isCommutativeMonoid : IsCommutativeMonoid _∧_ true
524 ∧-isCommutativeMonoid = record
525 { isMonoid = ∧-isMonoid
526 ; comm = ∧-comm
527 }
528
529 ∧-commutativeMonoid : CommutativeMonoid 0ℓ 0ℓ
530 ∧-commutativeMonoid = record
531 { isCommutativeMonoid = ∧-isCommutativeMonoid
532 }
533
534 ∧-isIdempotentCommutativeMonoid :
535 IsIdempotentCommutativeMonoid _∧_ true
536 ∧-isIdempotentCommutativeMonoid = record
537 { isCommutativeMonoid = ∧-isCommutativeMonoid
538 ; idem = ∧-idem
539 }
540
541 ∧-idempotentCommutativeMonoid : IdempotentCommutativeMonoid 0ℓ 0ℓ
542 ∧-idempotentCommutativeMonoid = record
543 { isIdempotentCommutativeMonoid = ∧-isIdempotentCommutativeMonoid
544 }
545
546 ∨-∧-isSemiring : IsSemiring _∨_ _∧_ false true
547 ∨-∧-isSemiring = record
548 { isSemiringWithoutAnnihilatingZero = record
549 { +-isCommutativeMonoid = ∨-isCommutativeMonoid
550 ; *-cong = cong₂ _∧_
551 ; *-assoc = ∧-assoc
552 ; *-identity = ∧-identity
553 ; distrib = ∧-distrib-∨
554 }
555 ; zero = ∧-zero
556 }
557
558 ∨-∧-isCommutativeSemiring
559 : IsCommutativeSemiring _∨_ _∧_ false true
560 ∨-∧-isCommutativeSemiring = record
561 { isSemiring = ∨-∧-isSemiring
562 ; *-comm = ∧-comm
563 }
564
565 ∨-∧-commutativeSemiring : CommutativeSemiring 0ℓ 0ℓ
566 ∨-∧-commutativeSemiring = record
567 { _+_ = _∨_
568 ; _*_ = _∧_
569 ; 0# = false
570 ; 1# = true
571 ; isCommutativeSemiring = ∨-∧-isCommutativeSemiring
572 }
573
574 ∧-∨-isSemiring : IsSemiring _∧_ _∨_ true false
575 ∧-∨-isSemiring = record
576 { isSemiringWithoutAnnihilatingZero = record
577 { +-isCommutativeMonoid = ∧-isCommutativeMonoid
578 ; *-cong = cong₂ _∨_
579 ; *-assoc = ∨-assoc
580 ; *-identity = ∨-identity
581 ; distrib = ∨-distrib-∧
582 }
583 ; zero = ∨-zero
584 }
585
586 ∧-∨-isCommutativeSemiring
587 : IsCommutativeSemiring _∧_ _∨_ true false
588 ∧-∨-isCommutativeSemiring = record
589 { isSemiring = ∧-∨-isSemiring
590 ; *-comm = ∨-comm
591 }
592
593 ∧-∨-commutativeSemiring : CommutativeSemiring 0ℓ 0ℓ
594 ∧-∨-commutativeSemiring = record
595 { _+_ = _∧_
596 ; _*_ = _∨_
597 ; 0# = true
598 ; 1# = false
599 ; isCommutativeSemiring = ∧-∨-isCommutativeSemiring
600 }
601
602 ∨-∧-isLattice : IsLattice _∨_ _∧_
603 ∨-∧-isLattice = record
604 { isEquivalence = isEquivalence
605 ; ∨-comm = ∨-comm
606 ; ∨-assoc = ∨-assoc
607 ; ∨-cong = cong₂ _∨_
608 ; ∧-comm = ∧-comm
609 ; ∧-assoc = ∧-assoc
610 ; ∧-cong = cong₂ _∧_
611 ; absorptive = ∨-∧-absorptive
612 }
613
614 ∨-∧-lattice : Lattice 0ℓ 0ℓ
615 ∨-∧-lattice = record
616 { isLattice = ∨-∧-isLattice
617 }
618
619 ∨-∧-isDistributiveLattice : IsDistributiveLattice _∨_ _∧_
620 ∨-∧-isDistributiveLattice = record
621 { isLattice = ∨-∧-isLattice
622 ; ∨-distrib-∧ = ∨-distrib-∧
623 ; ∧-distrib-∨ = ∧-distrib-∨
624 }
625
626 ∨-∧-distributiveLattice : DistributiveLattice 0ℓ 0ℓ
627 ∨-∧-distributiveLattice = record
628 { isDistributiveLattice = ∨-∧-isDistributiveLattice
629 }
630
631 ∨-∧-isBooleanAlgebra : IsBooleanAlgebra _∨_ _∧_ not true false
632 ∨-∧-isBooleanAlgebra = record
633 { isDistributiveLattice = ∨-∧-isDistributiveLattice
634 ; ∨-complement = ∨-inverse
635 ; ∧-complement = ∧-inverse
636 ; ¬-cong = cong not
637 }
638
639 ∨-∧-booleanAlgebra : BooleanAlgebra 0ℓ 0ℓ
640 ∨-∧-booleanAlgebra = record
641 { isBooleanAlgebra = ∨-∧-isBooleanAlgebra
642 }
643
644 ------------------------------------------------------------------------
645 -- Properties of not
646
647 not-involutive : Involutive not
648 not-involutive true = refl
649 not-involutive false = refl
650
651 not-injective : ∀ {x y} → not x ≡ not y → x ≡ y
652 not-injective {false} {false} nx≢ny = refl
653 not-injective {true} {true} nx≢ny = refl
654
655 not-¬ : ∀ {x y} → x ≡ y → x ≢ not y
656 not-¬ {true} refl ()
657 not-¬ {false} refl ()
658
659 ¬-not : ∀ {x y} → x ≢ y → x ≡ not y
660 ¬-not {true} {true} x≢y = contradiction refl x≢y
661 ¬-not {true} {false} _ = refl
662 ¬-not {false} {true} _ = refl
663 ¬-not {false} {false} x≢y = contradiction refl x≢y
664
665 ------------------------------------------------------------------------
666 -- Properties of _xor_
667
668 xor-is-ok : ∀ x y → x xor y ≡ (x ∨ y) ∧ not (x ∧ y)
669 xor-is-ok true y = refl
670 xor-is-ok false y = sym (∧-identityʳ _)
671
672 true-xor : ∀ x → true xor x ≡ not x
673 true-xor false = refl
674 true-xor true = refl
675
676 xor-same : ∀ x → x xor x ≡ false
677 xor-same false = refl
678 xor-same true = refl
679
680 not-distribˡ-xor : ∀ x y → not (x xor y) ≡ (not x) xor y
681 not-distribˡ-xor false y = refl
682 not-distribˡ-xor true y = not-involutive _
683
684 not-distribʳ-xor : ∀ x y → not (x xor y) ≡ x xor (not y)
685 not-distribʳ-xor false y = refl
686 not-distribʳ-xor true y = refl
687
688 xor-assoc : Associative _xor_
689 xor-assoc true y z = sym (not-distribˡ-xor y z)
690 xor-assoc false y z = refl
691
692 xor-comm : Commutative _xor_
693 xor-comm false false = refl
694 xor-comm false true = refl
695 xor-comm true false = refl
696 xor-comm true true = refl
697
698 xor-identityˡ : LeftIdentity false _xor_
699 xor-identityˡ _ = refl
700
701 xor-identityʳ : RightIdentity false _xor_
702 xor-identityʳ false = refl
703 xor-identityʳ true = refl
704
705 xor-identity : Identity false _xor_
706 xor-identity = xor-identityˡ , xor-identityʳ
707
708 xor-inverseˡ : LeftInverse true not _xor_
709 xor-inverseˡ false = refl
710 xor-inverseˡ true = refl
711
712 xor-inverseʳ : RightInverse true not _xor_
713 xor-inverseʳ x = xor-comm x (not x) ⟨ trans ⟩ xor-inverseˡ x
714
715 xor-inverse : Inverse true not _xor_
716 xor-inverse = xor-inverseˡ , xor-inverseʳ
717
718 ∧-distribˡ-xor : _∧_ DistributesOverˡ _xor_
719 ∧-distribˡ-xor false y z = refl
720 ∧-distribˡ-xor true y z = refl
721
722 ∧-distribʳ-xor : _∧_ DistributesOverʳ _xor_
723 ∧-distribʳ-xor x false z = refl
724 ∧-distribʳ-xor x true false = sym (xor-identityʳ x)
725 ∧-distribʳ-xor x true true = sym (xor-same x)
726
727 ∧-distrib-xor : _∧_ DistributesOver _xor_
728 ∧-distrib-xor = ∧-distribˡ-xor , ∧-distribʳ-xor
729
730 xor-annihilates-not : ∀ x y → (not x) xor (not y) ≡ x xor y
731 xor-annihilates-not false y = not-involutive _
732 xor-annihilates-not true y = refl
733
734 xor-∧-commutativeRing : CommutativeRing 0ℓ 0ℓ
735 xor-∧-commutativeRing = ⊕-∧-commutativeRing
736 where
737 open BooleanAlgebraProperties ∨-∧-booleanAlgebra
738 open XorRing _xor_ xor-is-ok
739
740 ------------------------------------------------------------------------
741 -- Properties of if_then_else_
742
743 if-float : ∀ (f : A → B) b {x y} →
744 f (if b then x else y) ≡ (if b then f x else f y)
745 if-float _ true = refl
746 if-float _ false = refl
747
748 if-eta : ∀ b {x : A} →
749 (if b then x else x) ≡ x
750 if-eta false = refl
751 if-eta true = refl
752
753 if-idem-then : ∀ b {x y : A} →
754 (if b then (if b then x else y) else y)
755 ≡ (if b then x else y)
756 if-idem-then false = refl
757 if-idem-then true = refl
758
759 if-idem-else : ∀ b {x y : A} →
760 (if b then x else (if b then x else y))
761 ≡ (if b then x else y)
762 if-idem-else false = refl
763 if-idem-else true = refl
764
765 if-swap-then : ∀ b c {x y : A} →
766 (if b then (if c then x else y) else y)
767 ≡ (if c then (if b then x else y) else y)
768 if-swap-then false false = refl
769 if-swap-then false true = refl
770 if-swap-then true _ = refl
771
772 if-swap-else : ∀ b c {x y : A} →
773 (if b then x else (if c then x else y))
774 ≡ (if c then x else (if b then x else y))
775 if-swap-else false _ = refl
776 if-swap-else true false = refl
777 if-swap-else true true = refl
778
779 if-not : ∀ b {x y : A} →
780 (if not b then x else y) ≡ (if b then y else x)
781 if-not false = refl
782 if-not true = refl
783
784 if-∧ : ∀ b {c} {x y : A} →
785 (if b ∧ c then x else y) ≡ (if b then (if c then x else y) else y)
786 if-∧ false = refl
787 if-∧ true = refl
788
789 if-∨ : ∀ b {c} {x y : A} →
790 (if b ∨ c then x else y) ≡ (if b then x else (if c then x else y))
791 if-∨ false = refl
792 if-∨ true = refl
793
794 if-xor : ∀ b {c} {x y : A} →
795 (if b xor c then x else y)
796 ≡ (if b then (if c then y else x) else (if c then x else y))
797 if-xor false = refl
798 if-xor true {false} = refl
799 if-xor true {true } = refl
800
801 -- The following congruence lemmas are short hands for
802 -- cong (if_then x else y)
803 -- cong (if b then_else y)
804 -- cong (if b then x else_)
805 -- cong (if b then_else_)
806 -- on the different sub-terms in an if_then_else_ expression.
807 -- With these short hands, the branches x and y can be inferred
808 -- automatically (i.e., they are implicit arguments) whereas
809 -- the branches have to be spelled out explicitly when using cong.
810 -- (Using underscores as in "cong (if b then _ else_)"
811 -- unfortunately fails to resolve the omitted argument.)
812
813 if-cong : ∀ {b c} {x y : A} → b ≡ c →
814 (if b then x else y)
815 ≡ (if c then x else y)
816 if-cong refl = refl
817
818 if-cong-then : ∀ b {x y z : A} → x ≡ z →
819 (if b then x else y)
820 ≡ (if b then z else y)
821 if-cong-then _ refl = refl
822
823 if-cong-else : ∀ b {x y z : A} → y ≡ z →
824 (if b then x else y)
825 ≡ (if b then x else z)
826 if-cong-else _ refl = refl
827
828 if-cong₂ : ∀ b {x y z w : A} → x ≡ z → y ≡ w →
829 (if b then x else y)
830 ≡ (if b then z else w)
831 if-cong₂ _ refl refl = refl
832
833 ------------------------------------------------------------------------
834 -- Properties of T
835
836 open Relation.Nullary.Decidable.Core public using (T?)
837
838 T-≡ : ∀ {x} → T x ⇔ x ≡ true
839 T-≡ {false} = mk⇔ (λ ()) (λ ())
840 T-≡ {true} = mk⇔ (const refl) (const _)
841
842 T-not-≡ : ∀ {x} → T (not x) ⇔ x ≡ false
843 T-not-≡ {false} = mk⇔ (const refl) (const _)
844 T-not-≡ {true} = mk⇔ (λ ()) (λ ())
845
846 T-∧ : ∀ {x y} → T (x ∧ y) ⇔ (T x × T y)
847 T-∧ {true} {true} = mk⇔ (const (_ , _)) (const _)
848 T-∧ {true} {false} = mk⇔ (λ ()) proj₂
849 T-∧ {false} {_} = mk⇔ (λ ()) proj₁
850
851 T-∨ : ∀ {x y} → T (x ∨ y) ⇔ (T x ⊎ T y)
852 T-∨ {true} {_} = mk⇔ inj₁ (const _)
853 T-∨ {false} {true} = mk⇔ inj₂ (const _)
854 T-∨ {false} {false} = mk⇔ inj₁ [ id , id ]
855
856 T-irrelevant : U.Irrelevant T
857 T-irrelevant {true} _ _ = refl
858
859 T?-diag : ∀ b → T b → True (T? b)
860 T?-diag b = fromWitness
861
862 ------------------------------------------------------------------------
863 -- Miscellaneous other properties
864
865 ⇔→≡ : {x y z : Bool} → x ≡ z ⇔ y ≡ z → x ≡ y
866 ⇔→≡ {true } {true } hyp = refl
867 ⇔→≡ {true } {false} {true } hyp = sym (Equivalence.to hyp refl)
868 ⇔→≡ {true } {false} {false} hyp = Equivalence.from hyp refl
869 ⇔→≡ {false} {true } {true } hyp = Equivalence.from hyp refl
870 ⇔→≡ {false} {true } {false} hyp = sym (Equivalence.to hyp refl)
871 ⇔→≡ {false} {false} hyp = refl
872
873
874 ------------------------------------------------------------------------
875 -- DEPRECATED NAMES
876 ------------------------------------------------------------------------
877 -- Please use the new names as continuing support for the old names is
878 -- not guaranteed.
879
880 -- Version 2.0
881
882 push-function-into-if = if-float
883 {-# WARNING_ON_USAGE push-function-into-if
884 "Warning: push-function-into-if was deprecated in v2.0.
885 Please use if-float instead."
886 #-}