Debian Codegraph @

src/Relation/Nullary/Decidable/Core.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Operations on and properties of decidable relations
5 --
6 -- This file contains some core definitions which are re-exported by
7 -- Relation.Nullary.Decidable
8 ------------------------------------------------------------------------
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Relation.Nullary.Decidable.Core where
13
14 -- decToMaybe was deprecated in v2.1 #2330/#2336
15 -- this can go through `Data.Maybe.Base` once that deprecation is fully done.
16 open import Agda.Builtin.Equality using (_≡_)
17 open import Agda.Builtin.Maybe using (Maybe; just; nothing)
18 open import Level using (Level)
19 open import Data.Bool.Base using (Bool; T; false; true; not; _∧_; _∨_)
20 open import Data.Unit.Polymorphic.Base using (⊤)
21 open import Data.Empty.Polymorphic using (⊥)
22 open import Data.Product.Base using (_×_)
23 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
24 open import Function.Base using (_∘_; const; _$_; flip)
25 open import Relation.Nullary.Irrelevant using (Irrelevant)
26 open import Relation.Nullary.Recomputable.Core as Recomputable
27 using (Recomputable)
28 open import Relation.Nullary.Reflects as Reflects
29 hiding (recompute; recompute-constant)
30 open import Relation.Nullary.Negation.Core
31 using (¬_; Stable; negated-stable; contradiction; DoubleNegation)
32
33
34 private
35 variable
36 a b : Level
37 A B : Set a
38
39 ------------------------------------------------------------------------
40 -- Definition.
41
42 -- Decidability proofs have two parts: the `does` term which contains
43 -- the boolean result and the `proof` term which contains a proof that
44 -- reflects the boolean result. This definition allows the boolean
45 -- part of the decision procedure to compute independently from the
46 -- proof. This leads to better computational behaviour when we only care
47 -- about the result and not the proof. See README.Design.Decidability
48 -- for further details.
49
50 infix 2 _because_
51
52 record Dec (A : Set a) : Set a where
53 constructor _because_
54 field
55 does : Bool
56 proof : Reflects A does
57
58 open Dec public
59
60 pattern yes a = true because ofʸ a
61 pattern no ¬a = false because ofⁿ ¬a
62
63 ------------------------------------------------------------------------
64 -- Flattening
65
66 module _ {A : Set a} where
67
68 From-yes : Dec A → Set a
69 From-yes (true because _) = A
70 From-yes (false because _) = ⊤
71
72 From-no : Dec A → Set a
73 From-no (false because _) = ¬ A
74 From-no (true because _) = ⊤
75
76 ------------------------------------------------------------------------
77 -- Recompute
78
79 -- Given an irrelevant proof of a decidable type, a proof can
80 -- be recomputed and subsequently used in relevant contexts.
81
82 recompute : Dec A → Recomputable A
83 recompute = Reflects.recompute ∘ proof
84
85 recompute-constant : (a? : Dec A) (p q : A) → recompute a? p ≡ recompute a? q
86 recompute-constant = Recomputable.recompute-constant ∘ recompute
87
88 recompute-irrelevant-id : (a? : Dec A) → Irrelevant A → (a : A) → recompute a? a ≡ a
89 recompute-irrelevant-id = Recomputable.recompute-irrelevant-id ∘ recompute
90
91 ------------------------------------------------------------------------
92 -- Interaction with negation, sum, product etc.
93
94 infixr 1 _⊎-dec_
95 infixr 2 _×-dec_ _→-dec_
96
97 T? : ∀ x → Dec (T x)
98 T? x = x because T-reflects x
99
100 ¬? : Dec A → Dec (¬ A)
101 does (¬? a?) = not (does a?)
102 proof (¬? a?) = ¬-reflects (proof a?)
103
104 ⊤-dec : Dec {a} ⊤
105 does ⊤-dec = true
106 proof ⊤-dec = ⊤-reflects
107
108 _×-dec_ : Dec A → Dec B → Dec (A × B)
109 does (a? ×-dec b?) = does a? ∧ does b?
110 proof (a? ×-dec b?) = proof a? ×-reflects proof b?
111
112 ⊥-dec : Dec {a} ⊥
113 does ⊥-dec = false
114 proof ⊥-dec = ⊥-reflects
115
116 _⊎-dec_ : Dec A → Dec B → Dec (A ⊎ B)
117 does (a? ⊎-dec b?) = does a? ∨ does b?
118 proof (a? ⊎-dec b?) = proof a? ⊎-reflects proof b?
119
120 _→-dec_ : Dec A → Dec B → Dec (A → B)
121 does (a? →-dec b?) = not (does a?) ∨ does b?
122 proof (a? →-dec b?) = proof a? →-reflects proof b?
123
124 ------------------------------------------------------------------------
125 -- Relationship with Maybe
126
127 dec⇒maybe : Dec A → Maybe A
128 dec⇒maybe ( true because [a]) = just (invert [a])
129 dec⇒maybe (false because _ ) = nothing
130
131 ------------------------------------------------------------------------
132 -- Relationship with Sum
133
134 toSum : Dec A → A ⊎ ¬ A
135 toSum ( true because [p]) = inj₁ (invert [p])
136 toSum (false because [¬p]) = inj₂ (invert [¬p])
137
138 fromSum : A ⊎ ¬ A → Dec A
139 fromSum (inj₁ p) = yes p
140 fromSum (inj₂ ¬p) = no ¬p
141
142 ------------------------------------------------------------------------
143 -- Relationship with booleans
144
145 -- `isYes` is a stricter version of `does`. The lack of computation
146 -- means that we can recover the proposition `P` from `isYes a?` by
147 -- unification. This is useful when we are using the decision procedure
148 -- for proof automation.
149
150 isYes : Dec A → Bool
151 isYes (true because _) = true
152 isYes (false because _) = false
153
154 isNo : Dec A → Bool
155 isNo = not ∘ isYes
156
157 True : Dec A → Set
158 True = T ∘ isYes
159
160 False : Dec A → Set
161 False = T ∘ isNo
162
163 -- The traditional name for isYes is ⌊_⌋, indicating the stripping of evidence.
164 ⌊_⌋ = isYes
165
166 ------------------------------------------------------------------------
167 -- Witnesses
168
169 -- Gives a witness to the "truth".
170 toWitness : {a? : Dec A} → True a? → A
171 toWitness {a? = true because [a]} _ = invert [a]
172 toWitness {a? = false because _ } ()
173
174 -- Establishes a "truth", given a witness.
175 fromWitness : {a? : Dec A} → A → True a?
176 fromWitness {a? = true because _ } = const _
177 fromWitness {a? = false because [¬a]} = invert [¬a]
178
179 -- Variants for False.
180 toWitnessFalse : {a? : Dec A} → False a? → ¬ A
181 toWitnessFalse {a? = true because _ } ()
182 toWitnessFalse {a? = false because [¬a]} _ = invert [¬a]
183
184 fromWitnessFalse : {a? : Dec A} → ¬ A → False a?
185 fromWitnessFalse {a? = true because [a]} = flip _$_ (invert [a])
186 fromWitnessFalse {a? = false because _ } = const _
187
188 -- If a decision procedure returns "yes", then we can extract the
189 -- proof using from-yes.
190 from-yes : (a? : Dec A) → From-yes a?
191 from-yes (true because [a]) = invert [a]
192 from-yes (false because _ ) = _
193
194 -- If a decision procedure returns "no", then we can extract the proof
195 -- using from-no.
196 from-no : (a? : Dec A) → From-no a?
197 from-no (false because [¬a]) = invert [¬a]
198 from-no (true because _ ) = _
199
200 ------------------------------------------------------------------------
201 -- Maps
202
203 map′ : (A → B) → (B → A) → Dec A → Dec B
204 does (map′ A→B B→A a?) = does a?
205 proof (map′ A→B B→A (true because [a])) = of (A→B (invert [a]))
206 proof (map′ A→B B→A (false because [¬a])) = of (invert [¬a] ∘ B→A)
207
208 ------------------------------------------------------------------------
209 -- Relationship with double-negation
210
211 -- Decidable predicates are stable.
212
213 decidable-stable : Dec A → Stable A
214 decidable-stable (true because [a]) ¬¬a = invert [a]
215 decidable-stable (false because [¬a]) ¬¬a = contradiction (invert [¬a]) ¬¬a
216
217 ¬-drop-Dec : Dec (¬ ¬ A) → Dec (¬ A)
218 ¬-drop-Dec ¬¬a? = map′ negated-stable contradiction (¬? ¬¬a?)
219
220 -- A double-negation-translated variant of excluded middle (or: every
221 -- nullary relation is decidable in the double-negation monad).
222
223 ¬¬-excluded-middle : DoubleNegation (Dec A)
224 ¬¬-excluded-middle ¬?a = ¬?a (no (λ a → ¬?a (yes a)))
225
226
227 ------------------------------------------------------------------------
228 -- DEPRECATED NAMES
229 ------------------------------------------------------------------------
230 -- Please use the new names as continuing support for the old names is
231 -- not guaranteed.
232
233 -- Version 2.0
234
235 excluded-middle = ¬¬-excluded-middle
236 {-# WARNING_ON_USAGE excluded-middle
237 "Warning: excluded-middle was deprecated in v2.0.
238 Please use ¬¬-excluded-middle instead."
239 #-}
240
241 -- Version 2.1
242
243 decToMaybe = dec⇒maybe
244 {-# WARNING_ON_USAGE decToMaybe
245 "Warning: decToMaybe was deprecated in v2.1.
246 Please use Relation.Nullary.Decidable.Core.dec⇒maybe instead."
247 #-}
248
249 fromDec = toSum
250 {-# WARNING_ON_USAGE fromDec
251 "Warning: fromDec was deprecated in v2.1.
252 Please use Relation.Nullary.Decidable.Core.toSum instead."
253 #-}
254
255 toDec = fromSum
256 {-# WARNING_ON_USAGE toDec
257 "Warning: toDec was deprecated in v2.1.
258 Please use Relation.Nullary.Decidable.Core.fromSum instead."
259 #-}