Skip to content
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

Enhancement to Relation.Nullary.Reflects etc. #2149

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a0db97c
added dependent eliminator; refactored existing definitions; added `r…
jamesmckinna Oct 13, 2023
ea024ec
knock-on changes; refactored `recompute`; made pattern synonyms `yes`…
jamesmckinna Oct 13, 2023
9d92dd5
added `of`, `invert`, `Recomputable` to exports from `Reflects`
jamesmckinna Oct 13, 2023
3c41be8
retained use of `invert`
jamesmckinna Oct 13, 2023
9df29e6
knock-on consequences; tightened `imports`
jamesmckinna Oct 13, 2023
259e966
added `reflects`, `reflects′`, `yes′`, `no′` to exports from `Reflects`
jamesmckinna Oct 13, 2023
35bb709
exemplary refactoring
jamesmckinna Oct 13, 2023
a68ab46
corrected reference to `README.Design.Decidability`
jamesmckinna Oct 14, 2023
8fba674
uniformity of `of`
jamesmckinna Oct 14, 2023
d8fe713
further uniformity wrt `of`
jamesmckinna Oct 18, 2023
856d8d3
uniformity of `of`
jamesmckinna Oct 19, 2023
af83f0a
tightened `recompute`; moved `invert` towards deprecation
jamesmckinna Oct 20, 2023
67f75a3
Merge branch 'master' into reflects-bis
jamesmckinna Nov 4, 2023
964b973
fixed buggy merge conflict
jamesmckinna Nov 4, 2023
2410d81
fixed buggy merge conflict
jamesmckinna Nov 4, 2023
1308e1d
fixed buggy merge conflict: hides `Reflects.recompute`
jamesmckinna Nov 4, 2023
507d3b1
tightened up
jamesmckinna Nov 4, 2023
7512da6
tightened up; added characterisation lemmas
jamesmckinna Nov 4, 2023
13f196a
hides `Reflects.fromEquivalence`
jamesmckinna Nov 4, 2023
edc184a
added characterisation lemmas
jamesmckinna Nov 4, 2023
f5329db
knock-on consequences for `import`s
jamesmckinna Nov 4, 2023
d1ee514
knock-on consequences for `import`s
jamesmckinna Nov 4, 2023
40558fe
narrower `import`s
jamesmckinna Nov 4, 2023
a9b8c24
final tweaks
jamesmckinna Nov 4, 2023
66dbe56
recitfy variable names
jamesmckinna Nov 7, 2023
3e174d6
refactoring: names
jamesmckinna Nov 7, 2023
9ec8399
knock-on consequences: implicit to explicit
jamesmckinna Nov 7, 2023
ec880fb
reverted changes to `README.Design.Decidability` in an effort to reso…
jamesmckinna Mar 8, 2024
8090a06
Merge branch 'master' into reflects-bis
jamesmckinna Mar 8, 2024
6d473aa
restored changes to `README.Design.Decidability` after resolving merg…
jamesmckinna Mar 8, 2024
cb52c08
fix imports
jamesmckinna Mar 8, 2024
27234f7
temporary fix to resolve multiple definitions
jamesmckinna Mar 8, 2024
13c3693
Merge branch 'master' into reflects-bis
jamesmckinna Mar 27, 2024
b6abf30
`fix-whitespace`
jamesmckinna Mar 27, 2024
2a8b773
use `recompute` directly
jamesmckinna Apr 8, 2024
16776b5
`UIP` in terms of `recompute`
jamesmckinna Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions doc/README/Design/Decidability.agda
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ infix 4 _≟₀_ _≟₁_ _≟₂_
-- a proof of `Reflects P false` amounts to a refutation of `P`.

ex₀ : (n : ℕ) → Reflects (n ≡ n) true
ex₀ n = ofʸ refl
ex₀ n = of refl

ex₁ : (n : ℕ) → Reflects (zero ≡ suc n) false
ex₁ n = ofⁿ λ ()
ex₁ n = of λ ()

ex₂ : (b : Bool) → Reflects (T b) b
ex₂ false = ofⁿ id
ex₂ true = ofʸ tt
ex₂ false = of id
ex₂ true = of tt

------------------------------------------------------------------------
-- Dec
Expand Down Expand Up @@ -85,8 +85,8 @@ zero ≟₁ suc n = no λ ()
suc m ≟₁ zero = no λ ()
does (suc m ≟₁ suc n) = does (m ≟₁ n)
proof (suc m ≟₁ suc n) with m ≟₁ n
... | yes p = ofʸ (cong suc p)
... | no ¬p = ofⁿ (¬p ∘ suc-injective)
... | yes p = of (cong suc p)
... | no ¬p = of (¬p ∘ suc-injective)

-- We now get definitional equalities such as the following.

Expand Down
38 changes: 18 additions & 20 deletions src/Axiom/UniquenessOfIdentityProofs.agda
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@

module Axiom.UniquenessOfIdentityProofs where

open import Data.Bool.Base using (true; false)
open import Data.Empty
open import Relation.Nullary.Reflects using (invert)
open import Relation.Nullary hiding (Irrelevant)
open import Function.Base using (id; const; flip)
open import Level using (Level)
open import Relation.Nullary using (contradiction; reflects′; proof)
open import Relation.Binary.Core
open import Relation.Binary.Definitions
open import Relation.Binary.PropositionalEquality.Core
open import Relation.Binary.PropositionalEquality.Properties

private
variable
a : Level
A : Set a

------------------------------------------------------------------------
-- Definition
--
-- Uniqueness of Identity Proofs (UIP) states that all proofs of
-- equality are themselves equal. In other words, the equality relation
-- is irrelevant. Here we define UIP relative to a given type.

UIP : ∀ {a} (A : Set a) → Set a
UIP : (A : Set a) → Set a
UIP A = Irrelevant {A = A} _≡_

------------------------------------------------------------------------
Expand All @@ -38,12 +42,11 @@ UIP A = Irrelevant {A = A} _≡_
-- proof to its image via this function which we then know is equal to
-- the image of any other proof.

module Constant⇒UIP
{a} {A : Set a} (f : _≡_ {A = A} ⇒ _≡_)
(f-constant : ∀ {a b} (p q : a ≡ b) → f p ≡ f q)
module Constant⇒UIP (f : _≡_ {A = A} ⇒ _≡_)
(f-constant : ∀ {x y} (p q : x ≡ y) → f p ≡ f q)
where

≡-canonical : ∀ {a b} (p : ab) → trans (sym (f refl)) (f p) ≡ p
≡-canonical : ∀ {x y} (p : xy) → trans (sym (f refl)) (f p) ≡ p
≡-canonical refl = trans-symˡ (f refl)

≡-irrelevant : UIP A
Expand All @@ -59,19 +62,14 @@ module Constant⇒UIP
-- function over proofs of equality which is constant: it returns the
-- proof produced by the decision procedure.

module Decidable⇒UIP
{a} {A : Set a} (_≟_ : Decidable {A = A} _≡_)
where
module Decidable⇒UIP (_≟_ : Decidable {A = A} _≡_) where

≡-normalise : _≡_ {A = A} ⇒ _≡_
jamesmckinna marked this conversation as resolved.
Show resolved Hide resolved
≡-normalise {a} {b} a≡b with a ≟ b
... | true because [p] = invert [p]
... | false because [¬p] = ⊥-elim (invert [¬p] a≡b)

≡-normalise-constant : ∀ {a b} (p q : a ≡ b) → ≡-normalise p ≡ ≡-normalise q
≡-normalise-constant {a} {b} p q with a ≟ b
... | true because _ = refl
... | false because [¬p] = ⊥-elim (invert [¬p] p)
≡-normalise {x} {y} x≡y = reflects′ (x ≡ y) id (contradiction x≡y) (proof (x ≟ y))

≡-normalise-constant : ∀ {x y} (p q : x ≡ y) → ≡-normalise p ≡ ≡-normalise q
≡-normalise-constant {x} {y} x≡y _
= reflects′ _ (λ _ → refl) (contradiction x≡y) (proof (x ≟ y))

≡-irrelevant : UIP A
≡-irrelevant = Constant⇒UIP.≡-irrelevant ≡-normalise ≡-normalise-constant
11 changes: 5 additions & 6 deletions src/Data/Nat/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Algebra.Construct.NaturalChoice.MinMaxOp as MinMaxOp
import Algebra.Lattice.Construct.NaturalChoice.MinMaxOp as LatticeMinMaxOp
import Algebra.Properties.CommutativeSemigroup as CommSemigroupProperties
open import Data.Bool.Base using (Bool; false; true; T)
open import Data.Bool.Properties using (T?)
open import Data.Nat.Base
open import Data.Product.Base using (∃; _×_; _,_)
open import Data.Sum.Base as Sum
Expand All @@ -35,10 +34,10 @@ open import Relation.Binary.Core
open import Relation.Binary
open import Relation.Binary.Consequences using (flip-Connex)
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary hiding (Irrelevant)
open import Relation.Nullary.Decidable using (True; via-injection; map′)
open import Relation.Nullary.Decidable
using (Dec; yes; no; T?; True; via-injection; map′)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
open import Relation.Nullary.Reflects using (fromEquivalence)
open import Relation.Nullary.Reflects as Reflects using (Reflects)

open import Algebra.Definitions {A = ℕ} _≡_
hiding (LeftCancellative; RightCancellative; Cancellative)
Expand Down Expand Up @@ -135,7 +134,7 @@ m ≟ n = map′ (≡ᵇ⇒≡ m n) (≡⇒≡ᵇ m n) (T? (m ≡ᵇ n))
<⇒<ᵇ (s<s m<n@(s≤s _)) = <⇒<ᵇ m<n

<ᵇ-reflects-< : ∀ m n → Reflects (m < n) (m <ᵇ n)
<ᵇ-reflects-< m n = fromEquivalence (<ᵇ⇒< m n) <⇒<ᵇ
<ᵇ-reflects-< m n = Reflects.fromEquivalence (<ᵇ⇒< m n) <⇒<ᵇ

------------------------------------------------------------------------
-- Properties of _≤ᵇ_
Expand All @@ -150,7 +149,7 @@ m ≟ n = map′ (≡ᵇ⇒≡ m n) (≡⇒≡ᵇ m n) (T? (m ≡ᵇ n))
≤⇒≤ᵇ m≤n@(s≤s _) = <⇒<ᵇ m≤n

≤ᵇ-reflects-≤ : ∀ m n → Reflects (m ≤ n) (m ≤ᵇ n)
≤ᵇ-reflects-≤ m n = fromEquivalence (≤ᵇ⇒≤ m n) ≤⇒≤ᵇ
≤ᵇ-reflects-≤ m n = Reflects.fromEquivalence (≤ᵇ⇒≤ m n) ≤⇒≤ᵇ

------------------------------------------------------------------------
-- Properties of _≤_
Expand Down
7 changes: 4 additions & 3 deletions src/Relation/Nullary.agda
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private

open import Relation.Nullary.Negation.Core public
open import Relation.Nullary.Reflects public
hiding (recompute; fromEquivalence)
open import Relation.Nullary.Decidable.Core public

------------------------------------------------------------------------
Expand All @@ -35,11 +36,11 @@ Irrelevant P = ∀ (p₁ p₂ : P) → p₁ ≡ p₂

------------------------------------------------------------------------
-- Recomputability - we can rebuild a relevant proof given an
-- irrelevant one.

-- irrelevant one. NOW moved to Relation.Nullary.Reflects
{-
Recomputable : Set p → Set p
Recomputable P = .P → P

-}
------------------------------------------------------------------------
-- Weak decidability
-- `nothing` is 'don't know'/'give up'; `just` is `yes`/`definitely`
Expand Down
46 changes: 32 additions & 14 deletions src/Relation/Nullary/Decidable.agda
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
module Relation.Nullary.Decidable where

open import Level using (Level)
open import Data.Bool.Base using (true; false; if_then_else_)
open import Data.Empty using (⊥-elim)
open import Data.Product.Base using (∃; _,_)
open import Data.Bool.Base using (true; false; T)
open import Data.Product.Base using (∃; ∃-syntax; _,_)
open import Function.Base
open import Function.Bundles using
(Injection; module Injection; module Equivalence; _⇔_; _↔_; mk↔ₛ′)
(Injection; module Injection; module Equivalence; _⇔_; mk⇔; _↔_; mk↔ₛ′)
open import Relation.Binary.Bundles using (Setoid; module Setoid)
open import Relation.Binary.Definitions using (Decidable)
open import Relation.Nullary
open import Relation.Nullary.Reflects using (invert)
open import Relation.Nullary using (invert; ¬_; contradiction; Irrelevant)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; refl; sym; trans; cong′)

Expand All @@ -32,6 +30,26 @@ private

open import Relation.Nullary.Decidable.Core public

------------------------------------------------------------------------
-- Characterisation : Dec A ⇔ there exists a Bool b st A ⇔ T b

-- forwards direction: use `does`

dec⇔T∘does : (A? : Dec A) → A ⇔ T (does A?)
dec⇔T∘does A? = mk⇔ (does-complete A?) (does-sound A?)

dec⇒∃⇔T : (A? : Dec A) → ∃[ b ] A ⇔ T b
dec⇒∃⇔T A? = does A? , dec⇔T∘does A?

-- backwards direction: inherit from `Decidable.Core`
∃⇔T⇒dec : (∃[ b ] A ⇔ T b) → Dec A
∃⇔T⇒dec (b , A⇔Tb) = fromEquivalence from to
where open Equivalence A⇔Tb

-- finally
dec⇔∃⇔T : Dec A ⇔ (∃[ b ] A ⇔ T b)
dec⇔∃⇔T = mk⇔ dec⇒∃⇔T ∃⇔T⇒dec

------------------------------------------------------------------------
-- Maps

Expand All @@ -52,23 +70,23 @@ via-injection inj _≟_ x y = map′ injective cong (to x ≟ to y)
-- A lemma relating True and Dec

True-↔ : (a? : Dec A) → Irrelevant A → True a? ↔ A
True-↔ (true because [a]) irr = mk↔ₛ′ (λ _ → invert [a]) _ (irr (invert [a])) cong′
True-↔ (false because ofⁿ ¬a) _ = mk↔ₛ′ (λ ()) (invert (ofⁿ ¬a)) (⊥-elim ∘ ¬a) λ ()
True-↔ (yesᵖ [a]) irr = let a = invert [a] in mk↔ₛ′ (λ _ → a) _ (irr a) cong′
True-↔ (noᵖ [¬a]) _ = let ¬a = invert [¬a] in mk↔ₛ′ (λ ()) ¬a (λ a → contradiction a ¬a) λ ()

------------------------------------------------------------------------
-- Result of decidability

isYes≗does : (a? : Dec A) → isYes a? ≡ does a?
isYes≗does (true because _) = refl
isYes≗does (false because _) = refl
isYes≗does (yesᵖ _) = refl
isYes≗does (noᵖ _) = refl

dec-true : (a? : Dec A) → A → does a? ≡ true
dec-true (true because _ ) a = refl
dec-true (false because [¬a]) a = ⊥-elim (invert [¬a] a)
dec-true (yesᵖ _ ) a = refl
dec-true (noᵖ [¬a]) a = contradiction a (invert [¬a])

dec-false : (a? : Dec A) → ¬ A → does a? ≡ false
dec-false (false because _ ) ¬a = refl
dec-false (true because [a]) ¬a = ⊥-elim (¬a (invert [a]))
dec-false (noᵖ _ ) ¬a = refl
dec-false (yesᵖ [a]) ¬a = contradiction (invert [a]) ¬a

dec-yes : (a? : Dec A) → A → ∃ λ a → a? ≡ yes a
dec-yes a? a with dec-true a? a
Expand Down
Loading
Loading