| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Operations on and properties of decidable relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Relation.Nullary.Decidable where |
| 10 |
|
| 11 |
open import Level using (Level) |
| 12 |
open import Data.Bool.Base using (true; false) |
| 13 |
open import Data.Product.Base using (∃; _,_) |
| 14 |
open import Function.Bundles |
| 15 |
using (Injection; module Injection; module Equivalence; _⇔_; _↔_; mk↔ₛ′) |
| 16 |
open import Relation.Binary.Bundles using (Setoid; module Setoid) |
| 17 |
open import Relation.Binary.Definitions using (Decidable) |
| 18 |
open import Relation.Nullary.Irrelevant using (Irrelevant) |
| 19 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 20 |
open import Relation.Nullary.Reflects using (invert) |
| 21 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 22 |
using (_≡_; refl; sym; trans) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
a b ℓ₁ ℓ₂ : Level |
| 27 |
A B : Set a |
| 28 |
|
| 29 |
------------------------------------------------------------------------ |
| 30 |
-- Re-exporting the core definitions |
| 31 |
|
| 32 |
open import Relation.Nullary.Decidable.Core public |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Maps |
| 36 |
|
| 37 |
map : A ⇔ B → Dec A → Dec B |
| 38 |
map A⇔B = map′ to from |
| 39 |
where open Equivalence A⇔B |
| 40 |
|
| 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 |
|
| 45 |
module _ {S : Setoid a ℓ₁} {T : Setoid b ℓ₂} (injection : Injection S T) where |
| 46 |
|
| 47 |
open Injection injection |
| 48 |
|
| 49 |
via-injection : Decidable Eq₂._≈_ → Decidable Eq₁._≈_ |
| 50 |
via-injection _≟_ x y = map′ injective cong (to x ≟ to y) |
| 51 |
|
| 52 |
------------------------------------------------------------------------ |
| 53 |
-- A lemma relating True and Dec |
| 54 |
|
| 55 |
True-↔ : (a? : Dec A) → Irrelevant A → True a? ↔ A |
| 56 |
True-↔ a? irr = mk↔ₛ′ to from to-from (from-to a?) |
| 57 |
where |
| 58 |
to = toWitness {a? = a?} |
| 59 |
from = fromWitness {a? = a?} |
| 60 |
to-from : ∀ a → to (from a) ≡ a |
| 61 |
to-from a = irr _ a |
| 62 |
from-to : ∀ a? (x : True a?) → fromWitness (toWitness x) ≡ x |
| 63 |
from-to (yes _) _ = refl |
| 64 |
|
| 65 |
------------------------------------------------------------------------ |
| 66 |
-- Result of decidability |
| 67 |
|
| 68 |
isYes≗does : (a? : Dec A) → isYes a? ≡ does a? |
| 69 |
isYes≗does (true because _) = refl |
| 70 |
isYes≗does (false because _) = refl |
| 71 |
|
| 72 |
dec-true : (a? : Dec A) → A → does a? ≡ true |
| 73 |
dec-true (true because _ ) a = refl |
| 74 |
dec-true (false because [¬a]) a = contradiction a (invert [¬a]) |
| 75 |
|
| 76 |
dec-false : (a? : Dec A) → ¬ A → does a? ≡ false |
| 77 |
dec-false (false because _ ) ¬a = refl |
| 78 |
dec-false (true because [a]) ¬a = contradiction (invert [a]) ¬a |
| 79 |
|
| 80 |
dec-yes-recompute : (a? : Dec A) → .(a : A) → a? ≡ yes (recompute a? a) |
| 81 |
dec-yes-recompute a? a with yes _ ← a? | refl ← dec-true a? (recompute a? a) = refl |
| 82 |
|
| 83 |
dec-yes-irr : (a? : Dec A) → Irrelevant A → (a : A) → a? ≡ yes a |
| 84 |
dec-yes-irr a? irr a = |
| 85 |
trans (dec-yes-recompute a? a) (≡.cong yes (recompute-irrelevant-id a? irr a)) |
| 86 |
|
| 87 |
dec-yes : (a? : Dec A) → A → ∃ λ a → a? ≡ yes a |
| 88 |
dec-yes a? a = _ , dec-yes-recompute a? a |
| 89 |
|
| 90 |
dec-no : (a? : Dec A) (¬a : ¬ A) → a? ≡ no ¬a |
| 91 |
dec-no a? ¬a with no _ ← a? | refl ← dec-false a? ¬a = refl |
| 92 |
|
| 93 |
⌊⌋-map′ : ∀ t f (a? : Dec A) → ⌊ map′ {B = B} t f a? ⌋ ≡ ⌊ a? ⌋ |
| 94 |
⌊⌋-map′ t f a? = trans (isYes≗does (map′ t f a?)) (sym (isYes≗does a?)) |
| 95 |
|
| 96 |
does-≡ : (a? b? : Dec A) → does a? ≡ does b? |
| 97 |
does-≡ a? (yes a) = dec-true a? a |
| 98 |
does-≡ a? (no ¬a) = dec-false a? ¬a |
| 99 |
|
| 100 |
does-⇔ : A ⇔ B → (a? : Dec A) → (b? : Dec B) → does a? ≡ does b? |
| 101 |
does-⇔ A⇔B a? = does-≡ (map A⇔B a?) |