Skip to content

[ add ] Relation.Nullary.Decidable.dec-yes-recompute #2738

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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ New modules

* `Relation.Binary.Properties.PartialSetoid` to systematise properties of a PER

* `Relation.Nullary.Recomputable.Core`

Additions to existing modules
-----------------------------

Expand Down Expand Up @@ -337,10 +339,17 @@ Additions to existing modules
<₋-wellFounded : WellFounded _<_ → WellFounded _<₋_
```

* In `Relation.Nullary.Decidable`:
```agda
dec-yes-recompute : (a? : Dec A) → .(a : A) → a? ≡ yes (recompute a? a)
```

* In `Relation.Nullary.Decidable.Core`:
```agda
⊤-dec : Dec {a} ⊤
⊥-dec : Dec {a} ⊥
recompute-irrelevant-id : (a? : Decidable A) → Irrelevant A →
(a : A) → recompute a? a ≡ a
```

* In `Relation.Nullary.Negation.Core`:
Expand All @@ -352,6 +361,7 @@ Additions to existing modules
```agda
⊤-reflects : Reflects (⊤ {a}) true
⊥-reflects : Reflects (⊥ {a}) false
```

* In `Data.List.Relation.Unary.AllPairs.Properties`:
```agda
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Fin/Permutation.agda
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open import Function.Base using (_∘_)
open import Level using (0ℓ)
open import Relation.Binary.Core using (Rel)
open import Relation.Nullary using (does; ¬_; yes; no)
open import Relation.Nullary.Decidable using (dec-yes; dec-no)
open import Relation.Nullary.Decidable using (dec-yes-recompute; dec-no)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; _≢_; refl; sym; trans; subst; cong; cong₂)
Expand Down Expand Up @@ -197,7 +197,7 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′

inverseʳ′ : StrictlyInverseʳ _≡_ to from
inverseʳ′ k with i ≟ k
... | yes i≡k rewrite proj₂ (dec-yes (j ≟ j) refl) = i≡k
... | yes i≡k rewrite dec-yes-recompute (j ≟ j) refl = i≡k
... | no i≢k
with j≢punchInⱼπʳpunchOuti≢k ← punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym
rewrite dec-no (j ≟ punchIn j (π ⟨$⟩ʳ punchOut i≢k)) j≢punchInⱼπʳpunchOuti≢k
Expand All @@ -210,7 +210,7 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′

inverseˡ′ : StrictlyInverseˡ _≡_ to from
inverseˡ′ k with j ≟ k
... | yes j≡k rewrite proj₂ (dec-yes (i ≟ i) refl) = j≡k
... | yes j≡k rewrite dec-yes-recompute (i ≟ i) refl = j≡k
... | no j≢k
with i≢punchInᵢπˡpunchOutj≢k ← punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym
rewrite dec-no (i ≟ punchIn i (π ⟨$⟩ˡ punchOut j≢k)) i≢punchInᵢπˡpunchOutj≢k
Expand Down
14 changes: 9 additions & 5 deletions src/Relation/Nullary/Decidable.agda
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open import Relation.Binary.Definitions using (Decidable)
open import Relation.Nullary.Irrelevant using (Irrelevant)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
open import Relation.Nullary.Reflects using (invert)
open import Relation.Binary.PropositionalEquality.Core
open import Relation.Binary.PropositionalEquality.Core as ≡
using (_≡_; refl; sym; trans)

private
Expand Down Expand Up @@ -77,15 +77,19 @@ dec-false : (a? : Dec A) → ¬ A → does a? ≡ false
dec-false (false because _ ) ¬a = refl
dec-false (true because [a]) ¬a = contradiction (invert [a]) ¬a

dec-yes-recompute : (a? : Dec A) → .(a : A) → a? ≡ yes (recompute a? a)
dec-yes-recompute a? a with yes _ ← a? | refl ← dec-true a? (recompute a? a) = refl

dec-yes-irr : (a? : Dec A) → Irrelevant A → (a : A) → a? ≡ yes a
dec-yes-irr a? irr a =
trans (dec-yes-recompute a? a) (≡.cong yes (recompute-irrelevant-id a? irr a))

dec-yes : (a? : Dec A) → A → ∃ λ a → a? ≡ yes a
dec-yes a? a with yes a′ ← a? | refl ← dec-true a? a = a′ , refl
dec-yes a? a = _ , dec-yes-recompute a? a

dec-no : (a? : Dec A) (¬a : ¬ A) → a? ≡ no ¬a
dec-no a? ¬a with no _ ← a? | refl ← dec-false a? ¬a = refl

dec-yes-irr : (a? : Dec A) → Irrelevant A → (a : A) → a? ≡ yes a
dec-yes-irr a? irr a with a′ , eq ← dec-yes a? a rewrite irr a a′ = eq

⌊⌋-map′ : ∀ t f (a? : Dec A) → ⌊ map′ {B = B} t f a? ⌋ ≡ ⌊ a? ⌋
⌊⌋-map′ t f a? = trans (isYes≗does (map′ t f a?)) (sym (isYes≗does a?))

Expand Down
8 changes: 6 additions & 2 deletions src/Relation/Nullary/Decidable/Core.agda
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ open import Data.Empty.Polymorphic using (⊥)
open import Data.Product.Base using (_×_)
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
open import Function.Base using (_∘_; const; _$_; flip)
open import Relation.Nullary.Recomputable as Recomputable
hiding (recompute-constant)
open import Relation.Nullary.Irrelevant using (Irrelevant)
open import Relation.Nullary.Recomputable.Core as Recomputable
using (Recomputable)
open import Relation.Nullary.Reflects as Reflects
hiding (recompute; recompute-constant)
open import Relation.Nullary.Negation.Core
Expand Down Expand Up @@ -84,6 +85,9 @@ recompute = Reflects.recompute ∘ proof
recompute-constant : (a? : Dec A) (p q : A) → recompute a? p ≡ recompute a? q
recompute-constant = Recomputable.recompute-constant ∘ recompute

recompute-irrelevant-id : (a? : Dec A) → Irrelevant A → (a : A) → recompute a? a ≡ a
recompute-irrelevant-id = Recomputable.recompute-irrelevant-id ∘ recompute

------------------------------------------------------------------------
-- Interaction with negation, sum, product etc.

Expand Down
17 changes: 2 additions & 15 deletions src/Relation/Nullary/Recomputable.agda
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

module Relation.Nullary.Recomputable where

open import Agda.Builtin.Equality using (_≡_; refl)
open import Data.Empty using (⊥)
open import Data.Irrelevant using (Irrelevant; irrelevant; [_])
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
Expand All @@ -22,22 +21,10 @@ private
B : Set b

------------------------------------------------------------------------
-- Definition
--
-- The idea of being 'recomputable' is that, given an *irrelevant* proof
-- of a proposition `A` (signalled by being a value of type `.A`, all of
-- whose inhabitants are identified up to definitional equality, and hence
-- do *not* admit pattern-matching), one may 'promote' such a value to a
-- 'genuine' value of `A`, available for subsequent eg. pattern-matching.

Recomputable : (A : Set a) → Set a
Recomputable A = .A → A
-- Re-export

------------------------------------------------------------------------
-- Fundamental property: 'promotion' is a constant function
open import Relation.Nullary.Recomputable.Core public

recompute-constant : (r : Recomputable A) (p q : A) → r p ≡ r q
recompute-constant r p q = refl

------------------------------------------------------------------------
-- Constructions
Expand Down
45 changes: 45 additions & 0 deletions src/Relation/Nullary/Recomputable/Core.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Recomputable types and their algebra as Harrop formulas
------------------------------------------------------------------------

{-# OPTIONS --cubical-compatible --safe #-}

module Relation.Nullary.Recomputable.Core where

open import Agda.Builtin.Equality using (_≡_; refl)
open import Level using (Level)
open import Relation.Nullary.Irrelevant using (Irrelevant)

private
variable
a : Level
A : Set a


------------------------------------------------------------------------
-- Definition
--
-- The idea of being 'recomputable' is that, given an *irrelevant* proof
-- of a proposition `A` (signalled by being a value of type `.A`, all of
-- whose inhabitants are identified up to definitional equality, and hence
-- do *not* admit pattern-matching), one may 'promote' such a value to a
-- 'genuine' value of `A`, available for subsequent eg. pattern-matching.

Recomputable : (A : Set a) → Set a
Recomputable A = .A → A

------------------------------------------------------------------------
-- Fundamental properties:
-- given `Recomputable A`, `recompute` is a constant function;
-- moreover, the identity, if `A` is propositionally irrelevant.

module _ (recompute : Recomputable A) where

recompute-constant : (p q : A) → recompute p ≡ recompute q
recompute-constant _ _ = refl

recompute-irrelevant-id : Irrelevant A → (a : A) → recompute a ≡ a
recompute-irrelevant-id irr a = irr (recompute a) a