Skip to content

Commit 64686b6

Browse files
MatthewDaggittandreasabel
authored andcommitted
Modernise Relation.Nullary code (#2110)
1 parent 6c06895 commit 64686b6

File tree

9 files changed

+220
-225
lines changed

9 files changed

+220
-225
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,11 @@ Deprecated names
15551555
invPreorder ↦ converse-preorder
15561556
```
15571557

1558+
* In `Relation.Nullary.Decidable.Core`:
1559+
```
1560+
excluded-middle ↦ ¬¬-excluded-middle
1561+
```
1562+
15581563
### Renamed Data.Erased to Data.Irrelevant
15591564

15601565
* This fixes the fact we had picked the wrong name originally. The erased modality

src/Codata/Musical/Colist.agda

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ take (suc n) [] = Vec≤.[]
6767
take (suc n) (x ∷ xs) = x Vec≤.∷ take n (♭ xs)
6868

6969

70-
module ¬¬Monad {p} where
71-
open RawMonad (¬¬-Monad {p}) public
70+
module ¬¬Monad {a} where
71+
open RawMonad (¬¬-Monad {a}) public
7272
open ¬¬Monad -- we don't want the RawMonad content to be opened publicly
7373

7474
------------------------------------------------------------------------

src/Data/Nat/InfinitelyOften.agda

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ open import Relation.Binary.PropositionalEquality
2020
open import Relation.Nullary.Negation using (¬_)
2121
open import Relation.Nullary.Negation using (¬¬-Monad; call/cc)
2222
open import Relation.Unary using (Pred; _∪_; _⊆_)
23-
open RawMonad (¬¬-Monad {p = 0ℓ})
23+
24+
open RawMonad (¬¬-Monad {a = 0ℓ})
2425

2526
private
2627
variable

src/Relation/Nullary/Decidable.agda

+32-41
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ module Relation.Nullary.Decidable where
1111
open import Level using (Level)
1212
open import Data.Bool.Base using (true; false; if_then_else_)
1313
open import Data.Empty using (⊥-elim)
14-
open import Data.Product.Base as Prod hiding (map)
15-
open import Data.Sum.Base as Sum hiding (map)
14+
open import Data.Product.Base using (∃; _,_)
1615
open import Function.Base
1716
open import Function.Bundles using
1817
(Injection; module Injection; module Equivalence; _⇔_; _↔_; mk↔ₛ′)
1918
open import Relation.Binary.Bundles using (Setoid; module Setoid)
2019
open import Relation.Binary.Definitions using (Decidable)
2120
open import Relation.Nullary
22-
open import Relation.Nullary.Negation
2321
open import Relation.Nullary.Reflects using (invert)
2422
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong′)
2523

2624
private
2725
variable
28-
p q r : Level
29-
P Q R : Set p
26+
a b ℓ₁ ℓ₂ : Level
27+
A B : Set a
3028

3129
------------------------------------------------------------------------
3230
-- Re-exporting the core definitions
@@ -36,56 +34,49 @@ open import Relation.Nullary.Decidable.Core public
3634
------------------------------------------------------------------------
3735
-- Maps
3836

39-
map : PQ Dec P Dec Q
40-
map P⇔Q = map′ to from
41-
where open Equivalence P⇔Q
37+
map : AB Dec A Dec B
38+
map A⇔B = map′ to from
39+
where open Equivalence A⇔B
4240

43-
module _ {a₁ a₂ b₁ b₂} {A : Setoid a₁ a₂} {B : Setoid b₁ b₂}
44-
(inj : Injection A B)
45-
where
46-
47-
open Injection inj
48-
open Setoid A using () renaming (_≈_ to _≈A_)
49-
open Setoid B using () renaming (_≈_ to _≈B_)
50-
51-
-- If there is an injection from one setoid to another, and the
52-
-- latter's equivalence relation is decidable, then the former's
53-
-- equivalence relation is also decidable.
54-
55-
via-injection : Decidable _≈B_ Decidable _≈A_
56-
via-injection dec x y =
57-
map′ injective cong (dec (to x) (to y))
41+
-- If there is an injection from one setoid to another, and the
42+
-- latter's equivalence relation is decidable, then the former's
43+
-- equivalence relation is also decidable.
44+
via-injection : {S : Setoid a ℓ₁} {T : Setoid b ℓ₂}
45+
(inj : Injection S T) (open Injection inj)
46+
Decidable Eq₂._≈_ Decidable Eq₁._≈_
47+
via-injection inj _≟_ x y = map′ injective cong (to x ≟ to y)
48+
where open Injection inj
5849

5950
------------------------------------------------------------------------
6051
-- A lemma relating True and Dec
6152

62-
True-↔ : (dec : Dec P) Irrelevant P True decP
63-
True-↔ (true because [p]) irr = mk↔ₛ′ (λ _ invert [p]) _ (irr (invert [p])) cong′
64-
True-↔ (false because ofⁿ ¬p) _ = mk↔ₛ′ (λ ()) (invert (ofⁿ ¬p)) (⊥-elim ∘ ¬p) λ ()
53+
True-↔ : (a? : Dec A) Irrelevant A True a?A
54+
True-↔ (true because [a]) irr = mk↔ₛ′ (λ _ invert [a]) _ (irr (invert [a])) cong′
55+
True-↔ (false because ofⁿ ¬a) _ = mk↔ₛ′ (λ ()) (invert (ofⁿ ¬a)) (⊥-elim ∘ ¬a) λ ()
6556

6657
------------------------------------------------------------------------
6758
-- Result of decidability
6859

69-
isYes≗does : (P? : Dec P) isYes P? ≡ does P?
60+
isYes≗does : (a? : Dec A) isYes a? ≡ does a?
7061
isYes≗does (true because _) = refl
7162
isYes≗does (false because _) = refl
7263

73-
dec-true : (p? : Dec P) P does p? ≡ true
74-
dec-true (true because _ ) p = refl
75-
dec-true (false because [¬p]) p = ⊥-elim (invert [¬p] p)
64+
dec-true : (a? : Dec A) A does a? ≡ true
65+
dec-true (true because _ ) a = refl
66+
dec-true (false because [¬a]) a = ⊥-elim (invert [¬a] a)
7667

77-
dec-false : (p? : Dec P) ¬ P does p? ≡ false
78-
dec-false (false because _ ) ¬p = refl
79-
dec-false (true because [p]) ¬p = ⊥-elim (¬p (invert [p]))
68+
dec-false : (a? : Dec A) ¬ A does a? ≡ false
69+
dec-false (false because _ ) ¬a = refl
70+
dec-false (true because [a]) ¬a = ⊥-elim (¬a (invert [a]))
8071

81-
dec-yes : (p? : Dec P) P λ p′ p? ≡ yes p′
82-
dec-yes p? p with dec-true p? p
83-
dec-yes (yes p′) p | refl = p′ , refl
72+
dec-yes : (a? : Dec A) A λ a a? ≡ yes a
73+
dec-yes a? a with dec-true a? a
74+
dec-yes (yes a′) a | refl = a′ , refl
8475

85-
dec-no : (p? : Dec P) (¬p : ¬ P) p? ≡ no ¬p
86-
dec-no p? ¬p with dec-false p? ¬p
76+
dec-no : (a? : Dec A) (¬a : ¬ A) a? ≡ no ¬a
77+
dec-no a? ¬a with dec-false a? ¬a
8778
dec-no (no _) _ | refl = refl
8879

89-
dec-yes-irr : (p? : Dec P) Irrelevant P (p : P) p? ≡ yes p
90-
dec-yes-irr p? irr p with dec-yes p? p
91-
... | p′ , eq rewrite irr p p= eq
80+
dec-yes-irr : (a? : Dec A) Irrelevant A (a : A) a? ≡ yes a
81+
dec-yes-irr a? irr a with dec-yes a? a
82+
... | a′ , eq rewrite irr a a= eq

src/Relation/Nullary/Decidable/Core.agda

+79-72
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ open import Relation.Nullary.Negation.Core
2424

2525
private
2626
variable
27-
p q : Level
28-
P : Set p
29-
Q : Set q
27+
a b : Level
28+
A B : Set a
3029

3130
------------------------------------------------------------------------
3231
-- Definition.
@@ -41,68 +40,81 @@ private
4140

4241
infix 2 _because_
4342

44-
record Dec {p} (P : Set p) : Set p where
43+
record Dec (A : Set a) : Set a where
4544
constructor _because_
4645
field
4746
does : Bool
48-
proof : Reflects P does
47+
proof : Reflects A does
4948

5049
open Dec public
5150

52-
pattern yes p = true because ofʸ p
53-
pattern no ¬p = false because ofⁿ ¬p
51+
pattern yes a = true because ofʸ a
52+
pattern no ¬a = false because ofⁿ ¬a
53+
54+
------------------------------------------------------------------------
55+
-- Flattening
56+
57+
module _ {A : Set a} where
58+
59+
From-yes : Dec A Set a
60+
From-yes (true because _) = A
61+
From-yes (false because _) = Lift a ⊤
62+
63+
From-no : Dec A Set a
64+
From-no (false because _) = ¬ A
65+
From-no (true because _) = Lift a ⊤
5466

5567
------------------------------------------------------------------------
5668
-- Recompute
5769

5870
-- Given an irrelevant proof of a decidable type, a proof can
5971
-- be recomputed and subsequently used in relevant contexts.
60-
recompute : {a} {A : Set a} Dec A .A A
61-
recompute (yes x) _ = x
62-
recompute (no ¬p) x = ⊥-elim (¬p x)
72+
recompute : Dec A .A A
73+
recompute (yes a) _ = a
74+
recompute (no ¬a) a = ⊥-elim (¬a a)
6375

6476
------------------------------------------------------------------------
6577
-- Interaction with negation, sum, product etc.
6678

6779
infixr 1 _⊎-dec_
6880
infixr 2 _×-dec_ _→-dec_
6981

70-
¬? : Dec P Dec (¬ P)
71-
does (¬? p?) = not (does p?)
72-
proof (¬? p?) = ¬-reflects (proof p?)
82+
¬? : Dec A Dec (¬ A)
83+
does (¬? a?) = not (does a?)
84+
proof (¬? a?) = ¬-reflects (proof a?)
7385

74-
_×-dec_ : Dec P Dec Q Dec (P × Q)
75-
does (p? ×-dec q?) = does p? ∧ does q?
76-
proof (p? ×-dec q?) = proof p? ×-reflects proof q?
86+
_×-dec_ : Dec A Dec B Dec (A × B)
87+
does (a? ×-dec b?) = does a? ∧ does b?
88+
proof (a? ×-dec b?) = proof a? ×-reflects proof b?
7789

78-
_⊎-dec_ : Dec P Dec Q Dec (PQ)
79-
does (p? ⊎-dec q?) = does p? ∨ does q?
80-
proof (p? ⊎-dec q?) = proof p? ⊎-reflects proof q?
90+
_⊎-dec_ : Dec A Dec B Dec (AB)
91+
does (a? ⊎-dec b?) = does a? ∨ does b?
92+
proof (a? ⊎-dec b?) = proof a? ⊎-reflects proof b?
8193

82-
_→-dec_ : Dec P Dec Q Dec (P Q)
83-
does (p? →-dec q?) = not (does p?) ∨ does q?
84-
proof (p? →-dec q?) = proof p? →-reflects proof q?
94+
_→-dec_ : Dec A Dec B Dec (A B)
95+
does (a? →-dec b?) = not (does a?) ∨ does b?
96+
proof (a? →-dec b?) = proof a? →-reflects proof b?
8597

8698
------------------------------------------------------------------------
8799
-- Relationship with booleans
88100

89101
-- `isYes` is a stricter version of `does`. The lack of computation
90-
-- means that we can recover the proposition `P` from `isYes P?` by
102+
-- means that we can recover the proposition `P` from `isYes a?` by
91103
-- unification. This is useful when we are using the decision procedure
92104
-- for proof automation.
93105

94-
isYes : Dec P Bool
106+
isYes : Dec A Bool
95107
isYes (true because _) = true
96108
isYes (false because _) = false
97109

98-
isNo : Dec P Bool
110+
isNo : Dec A Bool
99111
isNo = not ∘ isYes
100112

101-
True : Dec P Set
102-
True Q = T (isYes Q)
113+
True : Dec A Set
114+
True = T ∘ isYes
103115

104-
False : Dec P Set
105-
False Q = T (isNo Q)
116+
False : Dec A Set
117+
False = T ∘ isNo
106118

107119
-- The traditional name for isYes is ⌊_⌋, indicating the stripping of evidence.
108120
⌊_⌋ = isYes
@@ -111,77 +123,72 @@ False Q = T (isNo Q)
111123
-- Witnesses
112124

113125
-- Gives a witness to the "truth".
114-
toWitness : {Q : Dec P} True Q P
115-
toWitness {Q = true because [p]} _ = invert [p]
116-
toWitness {Q = false because _ } ()
126+
toWitness : {a? : Dec A} True a? A
127+
toWitness {a? = true because [a]} _ = invert [a]
128+
toWitness {a? = false because _ } ()
117129

118130
-- Establishes a "truth", given a witness.
119-
fromWitness : {Q : Dec P} P True Q
120-
fromWitness {Q = true because _ } = const _
121-
fromWitness {Q = false because [¬p]} = invert [¬p]
131+
fromWitness : {a? : Dec A} A True a?
132+
fromWitness {a? = true because _ } = const _
133+
fromWitness {a? = false because [¬a]} = invert [¬a]
122134

123135
-- Variants for False.
124-
toWitnessFalse : {Q : Dec P} False Q ¬ P
125-
toWitnessFalse {Q = true because _ } ()
126-
toWitnessFalse {Q = false because [¬p]} _ = invert [¬p]
127-
128-
fromWitnessFalse : {Q : Dec P} ¬ P False Q
129-
fromWitnessFalse {Q = true because [p]} = flip _$_ (invert [p])
130-
fromWitnessFalse {Q = false because _ } = const _
136+
toWitnessFalse : {a? : Dec A} False a? ¬ A
137+
toWitnessFalse {a? = true because _ } ()
138+
toWitnessFalse {a? = false because [¬a]} _ = invert [¬a]
131139

132-
module _ {p} {P : Set p} where
140+
fromWitnessFalse : {a? : Dec A} ¬ A False a?
141+
fromWitnessFalse {a? = true because [a]} = flip _$_ (invert [a])
142+
fromWitnessFalse {a? = false because _ } = const _
133143

134144
-- If a decision procedure returns "yes", then we can extract the
135145
-- proof using from-yes.
136-
137-
From-yes : Dec P Set p
138-
From-yes (true because _) = P
139-
From-yes (false because _) = Lift p ⊤
140-
141-
from-yes : (p : Dec P) From-yes p
142-
from-yes (true because [p]) = invert [p]
143-
from-yes (false because _ ) = _
146+
from-yes : (a? : Dec A) From-yes a?
147+
from-yes (true because [a]) = invert [a]
148+
from-yes (false because _ ) = _
144149

145150
-- If a decision procedure returns "no", then we can extract the proof
146151
-- using from-no.
147-
148-
From-no : Dec P Set p
149-
From-no (false because _) = ¬ P
150-
From-no (true because _) = Lift p ⊤
151-
152-
from-no : (p : Dec P) From-no p
153-
from-no (false because [¬p]) = invert [¬p]
154-
from-no (true because _ ) = _
152+
from-no : (a? : Dec A) From-no a?
153+
from-no (false because [¬a]) = invert [¬a]
154+
from-no (true because _ ) = _
155155

156156
------------------------------------------------------------------------
157157
-- Maps
158158

159-
map′ : (P Q) (Q P) Dec P Dec Q
160-
does (map′ P→Q Q→P p?) = does p?
161-
proof (map′ P→Q Q→P (true because [p])) = ofʸ (P→Q (invert [p]))
162-
proof (map′ P→Q Q→P (false because [¬p])) = ofⁿ (invert [¬p] ∘ Q→P)
159+
map′ : (A B) (B A) Dec A Dec B
160+
does (map′ A→B B→A a?) = does a?
161+
proof (map′ A→B B→A (true because [a])) = ofʸ (A→B (invert [a]))
162+
proof (map′ A→B B→A (false because [¬a])) = ofⁿ (invert [¬a] ∘ B→A)
163163

164164
------------------------------------------------------------------------
165165
-- Relationship with double-negation
166166

167167
-- Decidable predicates are stable.
168168

169-
decidable-stable : Dec P Stable P
170-
decidable-stable (yes p) ¬¬p = p
171-
decidable-stable (no ¬p) ¬¬p = ⊥-elim (¬¬p ¬p)
169+
decidable-stable : Dec A Stable A
170+
decidable-stable (yes a) ¬¬a = a
171+
decidable-stable (no ¬a) ¬¬a = ⊥-elim (¬¬a ¬a)
172172

173-
¬-drop-Dec : Dec (¬ ¬ P) Dec (¬ P)
174-
¬-drop-Dec ¬¬p? = map′ negated-stable contradiction (¬? ¬¬p?)
173+
¬-drop-Dec : Dec (¬ ¬ A) Dec (¬ A)
174+
¬-drop-Dec ¬¬a? = map′ negated-stable contradiction (¬? ¬¬a?)
175175

176176
-- A double-negation-translated variant of excluded middle (or: every
177177
-- nullary relation is decidable in the double-negation monad).
178178

179-
¬¬-excluded-middle : DoubleNegation (Dec P)
180-
¬¬-excluded-middle ¬h = ¬h (no (λ p ¬h (yes p)))
179+
¬¬-excluded-middle : DoubleNegation (Dec A)
180+
¬¬-excluded-middle ¬?a = ¬?a (no (λ a ¬?a (yes a)))
181181

182-
excluded-middle : DoubleNegation (Dec P)
183-
excluded-middle = ¬¬-excluded-middle
184182

183+
------------------------------------------------------------------------
184+
-- DEPRECATED NAMES
185+
------------------------------------------------------------------------
186+
-- Please use the new names as continuing support for the old names is
187+
-- not guaranteed.
188+
189+
-- Version 2.0
190+
191+
excluded-middle = ¬¬-excluded-middle
185192
{-# WARNING_ON_USAGE excluded-middle
186193
"Warning: excluded-middle was deprecated in v2.0.
187194
Please use ¬¬-excluded-middle instead."

0 commit comments

Comments
 (0)