| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties related to negation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Relation.Nullary.Negation where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (Bool; false; true; if_then_else_) |
| 12 |
open import Data.Product.Base as Product using (_,_; Σ; Σ-syntax; ∃; curry; uncurry) |
| 13 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]) |
| 14 |
open import Effect.Monad using (RawMonad; mkRawMonad) |
| 15 |
open import Function.Base using (flip; _∘_; const; _∘′_) |
| 16 |
open import Level using (Level) |
| 17 |
open import Relation.Nullary.Decidable.Core using (Dec; yes; no; ¬¬-excluded-middle) |
| 18 |
open import Relation.Unary using (Universal; Pred) |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
a b c d p w : Level |
| 23 |
A B C D : Set a |
| 24 |
P : Pred A p |
| 25 |
Whatever : Set w |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Re-export public definitions |
| 29 |
|
| 30 |
open import Relation.Nullary.Negation.Core public |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Quantifier juggling |
| 34 |
|
| 35 |
∃⟶¬∀¬ : ∃ P → ¬ (∀ x → ¬ P x) |
| 36 |
∃⟶¬∀¬ = flip uncurry |
| 37 |
|
| 38 |
∀⟶¬∃¬ : (∀ x → P x) → ¬ ∃ λ x → ¬ P x |
| 39 |
∀⟶¬∃¬ ∀xPx (x , ¬Px) = ¬Px (∀xPx x) |
| 40 |
|
| 41 |
¬∃⟶∀¬ : ¬ ∃ (λ x → P x) → ∀ x → ¬ P x |
| 42 |
¬∃⟶∀¬ = curry |
| 43 |
|
| 44 |
∀¬⟶¬∃ : (∀ x → ¬ P x) → ¬ ∃ (λ x → P x) |
| 45 |
∀¬⟶¬∃ = uncurry |
| 46 |
|
| 47 |
∃¬⟶¬∀ : ∃ (λ x → ¬ P x) → ¬ (∀ x → P x) |
| 48 |
∃¬⟶¬∀ = flip ∀⟶¬∃¬ |
| 49 |
|
| 50 |
------------------------------------------------------------------------ |
| 51 |
-- Double Negation |
| 52 |
|
| 53 |
-- Double-negation is a monad (if we assume that all elements of ¬ ¬ P |
| 54 |
-- are equal). |
| 55 |
|
| 56 |
¬¬-Monad : RawMonad {a} DoubleNegation |
| 57 |
¬¬-Monad = mkRawMonad |
| 58 |
DoubleNegation |
| 59 |
contradiction |
| 60 |
(λ x f → negated-stable (¬¬-map f x)) |
| 61 |
|
| 62 |
¬¬-push : DoubleNegation Π[ P ] → Π[ DoubleNegation ∘ P ] |
| 63 |
¬¬-push ¬¬∀P a ¬Pa = ¬¬∀P (λ ∀P → ¬Pa (∀P a)) |
| 64 |
|
| 65 |
-- If Whatever is instantiated with ¬ ¬ something, then this function |
| 66 |
-- is call with current continuation in the double-negation monad, or, |
| 67 |
-- if you will, a double-negation translation of Peirce's law. |
| 68 |
-- |
| 69 |
-- In order to prove ¬ ¬ P one can assume ¬ P and prove ⊥. However, |
| 70 |
-- sometimes it is nice to avoid leaving the double-negation monad; in |
| 71 |
-- that case this function can be used (with Whatever instantiated to |
| 72 |
-- ⊥). |
| 73 |
|
| 74 |
call/cc : ((A → Whatever) → DoubleNegation A) → DoubleNegation A |
| 75 |
call/cc hyp ¬a = hyp (flip contradiction ¬a) ¬a |
| 76 |
|
| 77 |
-- The "independence of premise" rule, in the double-negation monad. |
| 78 |
-- It is assumed that the index set (A) is inhabited. |
| 79 |
|
| 80 |
independence-of-premise : A → (B → Σ A P) → DoubleNegation (Σ[ x ∈ A ] (B → P x)) |
| 81 |
independence-of-premise {A = A} {B = B} {P = P} q f = ¬¬-map helper ¬¬-excluded-middle |
| 82 |
where |
| 83 |
helper : Dec B → Σ[ x ∈ A ] (B → P x) |
| 84 |
helper (yes p) = Product.map₂ const (f p) |
| 85 |
helper (no ¬p) = (q , flip contradiction ¬p) |
| 86 |
|
| 87 |
-- The independence of premise rule for binary sums. |
| 88 |
|
| 89 |
independence-of-premise-⊎ : (A → B ⊎ C) → DoubleNegation ((A → B) ⊎ (A → C)) |
| 90 |
independence-of-premise-⊎ {A = A} {B = B} {C = C} f = ¬¬-map helper ¬¬-excluded-middle |
| 91 |
where |
| 92 |
helper : Dec A → (A → B) ⊎ (A → C) |
| 93 |
helper (yes p) = Sum.map const const (f p) |
| 94 |
helper (no ¬p) = inj₁ (flip contradiction ¬p) |
| 95 |
|
| 96 |
private |
| 97 |
|
| 98 |
-- Note that independence-of-premise-⊎ is a consequence of |
| 99 |
-- independence-of-premise (for simplicity it is assumed that Q and |
| 100 |
-- R have the same type here): |
| 101 |
|
| 102 |
corollary : {B C : Set b} → (A → B ⊎ C) → DoubleNegation ((A → B) ⊎ (A → C)) |
| 103 |
corollary {A = A} {B = B} {C = C} f = |
| 104 |
¬¬-map helper (independence-of-premise true ([ _,_ true , _,_ false ] ∘′ f)) |
| 105 |
where |
| 106 |
helper : ∃ (λ b → A → if b then B else C) → (A → B) ⊎ (A → C) |
| 107 |
helper (true , f) = inj₁ f |
| 108 |
helper (false , f) = inj₂ f |