-
Notifications
You must be signed in to change notification settings - Fork 267
Enhancement to Relation.Nullary.Reflects etc.
#2149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
a0db97c
ea024ec
9d92dd5
3c41be8
9df29e6
259e966
35bb709
a68ab46
8fba674
d8fe713
856d8d3
af83f0a
67f75a3
964b973
2410d81
1308e1d
507d3b1
7512da6
13f196a
edc184a
f5329db
d1ee514
40558fe
a9b8c24
66dbe56
3e174d6
9ec8399
ec880fb
8090a06
6d473aa
cb52c08
27234f7
13c3693
b6abf30
2a8b773
16776b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,19 +12,19 @@ | |
| module Relation.Nullary.Decidable.Core where | ||
|
|
||
| open import Level using (Level; Lift) | ||
| open import Data.Bool.Base using (Bool; false; true; not; T; _∧_; _∨_) | ||
| open import Data.Bool.Base | ||
| open import Data.Unit.Base using (⊤) | ||
| open import Data.Empty using (⊥) | ||
| open import Data.Empty.Irrelevant using (⊥-elim) | ||
| open import Data.Product.Base using (_×_) | ||
| open import Data.Sum.Base using (_⊎_) | ||
| open import Function.Base using (_∘_; const; _$_; flip) | ||
| open import Relation.Nullary.Reflects | ||
| open import Relation.Nullary.Reflects as Reflects | ||
| using (Reflects; ofʸ; ofⁿ; of; invert; Recomputable) | ||
| open import Relation.Nullary.Negation.Core | ||
| using (¬_; contradiction; Stable; DoubleNegation; negated-stable) | ||
|
|
||
| private | ||
| variable | ||
| a b : Level | ||
| a : Level | ||
| A B : Set a | ||
|
|
||
| ------------------------------------------------------------------------ | ||
|
|
@@ -35,8 +35,8 @@ private | |
| -- reflects the boolean result. This definition allows the boolean | ||
| -- part of the decision procedure to compute independently from the | ||
| -- proof. This leads to better computational behaviour when we only care | ||
| -- about the result and not the proof. See README.Decidability for | ||
| -- further details. | ||
| -- about the result and not the proof. See README.Design.Decidability | ||
| -- for further details. | ||
|
|
||
| infix 2 _because_ | ||
|
|
||
|
|
@@ -48,30 +48,32 @@ record Dec (A : Set a) : Set a where | |
|
|
||
| open Dec public | ||
|
|
||
| pattern yes a = true because ofʸ a | ||
| pattern no ¬a = false because ofⁿ ¬a | ||
| -- lazier versions of `yes` and `no` | ||
| pattern yes′ [a] = true because [a] | ||
| pattern no′ [¬a] = false because [¬a] | ||
|
|
||
| -- now these are derived patterns, but could be re-expressed using `of` | ||
| pattern yes a = yes′ (ofʸ a) | ||
| pattern no [¬a] = no′ (ofⁿ [¬a]) | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Flattening | ||
|
|
||
| module _ {A : Set a} where | ||
|
|
||
| From-yes : Dec A → Set a | ||
| From-yes (true because _) = A | ||
| From-yes (false because _) = Lift a ⊤ | ||
| From-yes a? = if (does a?) then A else Lift a ⊤ | ||
|
|
||
| From-no : Dec A → Set a | ||
| From-no (false because _) = ¬ A | ||
| From-no (true because _) = Lift a ⊤ | ||
| From-no a? = if (does a?) then Lift a ⊤ else ¬ A | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Recompute | ||
|
|
||
| -- Given an irrelevant proof of a decidable type, a proof can | ||
| -- be recomputed and subsequently used in relevant contexts. | ||
| recompute : Dec A → .A → A | ||
| recompute (yes a) _ = a | ||
| recompute (no ¬a) a = ⊥-elim (¬a a) | ||
| recompute = Reflects.recompute ∘ proof | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Interaction with negation, sum, product etc. | ||
|
|
@@ -81,19 +83,19 @@ infixr 2 _×-dec_ _→-dec_ | |
|
|
||
| ¬? : Dec A → Dec (¬ A) | ||
| does (¬? a?) = not (does a?) | ||
| proof (¬? a?) = ¬-reflects (proof a?) | ||
| proof (¬? a?) = Reflects.¬-reflects (proof a?) | ||
|
|
||
| _×-dec_ : Dec A → Dec B → Dec (A × B) | ||
| does (a? ×-dec b?) = does a? ∧ does b? | ||
| proof (a? ×-dec b?) = proof a? ×-reflects proof b? | ||
| proof (a? ×-dec b?) = proof a? Reflects.×-reflects proof b? | ||
|
|
||
| _⊎-dec_ : Dec A → Dec B → Dec (A ⊎ B) | ||
| does (a? ⊎-dec b?) = does a? ∨ does b? | ||
| proof (a? ⊎-dec b?) = proof a? ⊎-reflects proof b? | ||
| proof (a? ⊎-dec b?) = proof a? Reflects.⊎-reflects proof b? | ||
|
|
||
| _→-dec_ : Dec A → Dec B → Dec (A → B) | ||
| does (a? →-dec b?) = not (does a?) ∨ does b? | ||
| proof (a? →-dec b?) = proof a? →-reflects proof b? | ||
| proof (a? →-dec b?) = proof a? Reflects.→-reflects proof b? | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Relationship with booleans | ||
|
|
@@ -104,8 +106,8 @@ proof (a? →-dec b?) = proof a? →-reflects proof b? | |
| -- for proof automation. | ||
|
|
||
| isYes : Dec A → Bool | ||
| isYes (true because _) = true | ||
| isYes (false because _) = false | ||
| isYes (yes′ _) = true | ||
| isYes (no′ _) = false | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So... this is claimed as a more strict version of
strictify : Bool → Bool
strictify b = if b then true else false
Would that be preferable?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I certainly find that idiom appealing.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps that's worth considering for
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, ought to consider strictification of various |
||
|
|
||
| isNo : Dec A → Bool | ||
| isNo = not ∘ isYes | ||
|
|
@@ -124,51 +126,51 @@ False = T ∘ isNo | |
|
|
||
| -- Gives a witness to the "truth". | ||
| toWitness : {a? : Dec A} → True a? → A | ||
| toWitness {a? = true because [a]} _ = invert [a] | ||
| toWitness {a? = false because _ } () | ||
| toWitness {a? = yes′ [a]} _ = invert [a] | ||
| toWitness {a? = no′ _ } () | ||
|
|
||
| -- Establishes a "truth", given a witness. | ||
| fromWitness : {a? : Dec A} → A → True a? | ||
| fromWitness {a? = true because _ } = const _ | ||
| fromWitness {a? = false because [¬a]} = invert [¬a] | ||
| fromWitness {a? = yes′ _ } = const _ | ||
| fromWitness {a? = no′ [¬a]} = invert [¬a] | ||
|
|
||
| -- Variants for False. | ||
| toWitnessFalse : {a? : Dec A} → False a? → ¬ A | ||
| toWitnessFalse {a? = true because _ } () | ||
| toWitnessFalse {a? = false because [¬a]} _ = invert [¬a] | ||
| toWitnessFalse {a? = yes′ _ } () | ||
| toWitnessFalse {a? = no′ [¬a]} _ = invert [¬a] | ||
|
|
||
| fromWitnessFalse : {a? : Dec A} → ¬ A → False a? | ||
| fromWitnessFalse {a? = true because [a]} = flip _$_ (invert [a]) | ||
| fromWitnessFalse {a? = false because _ } = const _ | ||
| fromWitnessFalse {a? = yes′ [a]} = flip _$_ (invert [a]) | ||
| fromWitnessFalse {a? = no′ _ } = const _ | ||
|
|
||
| -- If a decision procedure returns "yes", then we can extract the | ||
| -- proof using from-yes. | ||
| from-yes : (a? : Dec A) → From-yes a? | ||
| from-yes (true because [a]) = invert [a] | ||
| from-yes (false because _ ) = _ | ||
| from-yes (yes′ [a]) = invert [a] | ||
| from-yes (no′ _ ) = _ | ||
|
|
||
| -- If a decision procedure returns "no", then we can extract the proof | ||
| -- using from-no. | ||
| from-no : (a? : Dec A) → From-no a? | ||
| from-no (false because [¬a]) = invert [¬a] | ||
| from-no (true because _ ) = _ | ||
| from-no (no′ [¬a]) = invert [¬a] | ||
| from-no (yes′ _ ) = _ | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Maps | ||
|
|
||
| map′ : (A → B) → (B → A) → Dec A → Dec B | ||
| does (map′ A→B B→A a?) = does a? | ||
| proof (map′ A→B B→A (true because [a])) = ofʸ (A→B (invert [a])) | ||
| proof (map′ A→B B→A (false because [¬a])) = ofⁿ (invert [¬a] ∘ B→A) | ||
| does (map′ A→B B→A a?) = does a? | ||
| proof (map′ A→B B→A (yes′ [a])) = of (A→B (invert [a])) | ||
| proof (map′ A→B B→A (no′ [¬a])) = of (invert [¬a] ∘ B→A) | ||
|
|
||
| ------------------------------------------------------------------------ | ||
| -- Relationship with double-negation | ||
|
|
||
| -- Decidable predicates are stable. | ||
|
|
||
| decidable-stable : Dec A → Stable A | ||
| decidable-stable (yes a) ¬¬a = a | ||
| decidable-stable (no ¬a) ¬¬a = ⊥-elim (¬¬a ¬a) | ||
| decidable-stable (yes′ [a]) ¬¬a = invert [a] | ||
| decidable-stable (no′ [¬a]) ¬¬a = contradiction (invert [¬a]) ¬¬a | ||
|
|
||
| ¬-drop-Dec : Dec (¬ ¬ A) → Dec (¬ A) | ||
| ¬-drop-Dec ¬¬a? = map′ negated-stable contradiction (¬? ¬¬a?) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.