\begin{code}
module DecidableAndDetachable where
open import SetsAndFunctions
open import CurryHoward
open import Two
open import Equality
\end{code}
\begin{code}
decidable : Prp → Prp
decidable A = A ∨ ¬ A
¬¬-elim : {A : Prp} →
decidable A → ¬¬ A → A
¬¬-elim (in₀ a) f = a
¬¬-elim (in₁ g) f = ⊥-elim(f g)
negation-preserves-decidability : {A : Prp} →
decidable A → decidable(¬ A)
negation-preserves-decidability (in₀ a) = in₁ (λ f → f a)
negation-preserves-decidability (in₁ g) = in₀ g
which-of : {A B : Prp} →
A ∨ B → ∃ \(b : ₂) → (b ≡ ₀ → A) ∧ (b ≡ ₁ → B)
which-of (in₀ a) = ∃-intro ₀ (∧-intro (λ r → a) (λ ()))
which-of (in₁ b) = ∃-intro ₁ (∧-intro (λ ()) (λ r → b))
\end{code}
\begin{code}
truth-value : {A : Prp} →
decidable A → ∃ \(b : ₂) → (b ≡ ₀ → A) ∧ (b ≡ ₁ → ¬ A)
truth-value = which-of
\end{code}
\begin{code}
indicator : {X : Set} → {A B : X → Prp} →
(∀(x : X) → A x ∨ B x)
→ ∃ \(p : X → ₂) → ∀(x : X) → (p x ≡ ₀ → A x) ∧ (p x ≡ ₁ → B x)
indicator {X} {A} {B} h =
∃-intro (λ x → ∃-witness(lemma₁ x)) (λ x → ∃-elim(lemma₁ x))
where
lemma₀ : ∀(x : X) → (A x ∨ B x) → ∃ \b → (b ≡ ₀ → A x) ∧ (b ≡ ₁ → B x)
lemma₀ x = which-of {A x} {B x}
lemma₁ : ∀(x : X) → ∃ \b → (b ≡ ₀ → A x) ∧ (b ≡ ₁ → B x)
lemma₁ = λ x → lemma₀ x (h x)
\end{code}
\begin{code}
detachable : {X : Set} → (A : X → Prp) → Prp
detachable {X} A = ∀ x → decidable(A x)
characteristic-function : {X : Set} → {A : X → Prp} →
detachable A
→ ∃ \(p : X → ₂) → ∀(x : X) → (p x ≡ ₀ → A x) ∧ (p x ≡ ₁ → ¬(A x))
characteristic-function = indicator
\end{code}