{-# OPTIONS --without-K --safe #-}
module Relation.Nullary.Reflects where
open import Agda.Builtin.Equality
open import Data.Bool.Base
open import Data.Empty
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
open import Level using (Level)
open import Function.Base using (_$_; _∘_; const)
open import Relation.Nullary.Negation.Core
private
variable
p q : Level
P Q : Set p
data Reflects {p} (P : Set p) : Bool → Set p where
ofʸ : ( p : P) → Reflects P true
ofⁿ : (¬p : ¬ P) → Reflects P false
of : ∀ {b} → if b then P else ¬ P → Reflects P b
of {b = false} ¬p = ofⁿ ¬p
of {b = true } p = ofʸ p
invert : ∀ {b} → Reflects P b → if b then P else ¬ P
invert (ofʸ p) = p
invert (ofⁿ ¬p) = ¬p
¬-reflects : ∀ {b} → Reflects P b → Reflects (¬ P) (not b)
¬-reflects (ofʸ p) = ofⁿ (_$ p)
¬-reflects (ofⁿ ¬p) = ofʸ ¬p
infixr 2 _×-reflects_
_×-reflects_ : ∀ {a b} → Reflects P a → Reflects Q b →
Reflects (P × Q) (a ∧ b)
ofʸ p ×-reflects ofʸ q = ofʸ (p , q)
ofʸ p ×-reflects ofⁿ ¬q = ofⁿ (¬q ∘ proj₂)
ofⁿ ¬p ×-reflects _ = ofⁿ (¬p ∘ proj₁)
infixr 1 _⊎-reflects_
_⊎-reflects_ : ∀ {a b} → Reflects P a → Reflects Q b →
Reflects (P ⊎ Q) (a ∨ b)
ofʸ p ⊎-reflects _ = ofʸ (inj₁ p)
ofⁿ ¬p ⊎-reflects ofʸ q = ofʸ (inj₂ q)
ofⁿ ¬p ⊎-reflects ofⁿ ¬q = ofⁿ (¬p ¬-⊎ ¬q)
infixr 2 _→-reflects_
_→-reflects_ : ∀ {a b} → Reflects P a → Reflects Q b →
Reflects (P → Q) (not a ∨ b)
ofʸ p →-reflects ofʸ q = ofʸ (const q)
ofʸ p →-reflects ofⁿ ¬q = ofⁿ (¬q ∘ (_$ p))
ofⁿ ¬p →-reflects _ = ofʸ (⊥-elim ∘ ¬p)
fromEquivalence : ∀ {b} → (T b → P) → (P → T b) → Reflects P b
fromEquivalence {b = true} sound complete = ofʸ (sound _)
fromEquivalence {b = false} sound complete = ofⁿ complete
det : ∀ {b b′} → Reflects P b → Reflects P b′ → b ≡ b′
det (ofʸ p) (ofʸ p′) = refl
det (ofʸ p) (ofⁿ ¬p′) = ⊥-elim (¬p′ p)
det (ofⁿ ¬p) (ofʸ p′) = ⊥-elim (¬p p′)
det (ofⁿ ¬p) (ofⁿ ¬p′) = refl