{-# OPTIONS --cubical-compatible --safe #-}
module Relation.Nullary.Reflects where
open import Agda.Builtin.Equality
open import Data.Bool.Base
open import Data.Unit.Base using (⊤)
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; id)
open import Relation.Nullary.Negation.Core
private
variable
a : Level
A B : Set a
data Reflects (A : Set a) : Bool → Set a where
ofʸ : ( a : A) → Reflects A true
ofⁿ : (¬a : ¬ A) → Reflects A false
of : ∀ {b} → if b then A else ¬ A → Reflects A b
of {b = false} ¬a = ofⁿ ¬a
of {b = true } a = ofʸ a
invert : ∀ {b} → Reflects A b → if b then A else ¬ A
invert (ofʸ a) = a
invert (ofⁿ ¬a) = ¬a
infixr 1 _⊎-reflects_
infixr 2 _×-reflects_ _→-reflects_
T-reflects : ∀ b → Reflects (T b) b
T-reflects true = of _
T-reflects false = of id
¬-reflects : ∀ {b} → Reflects A b → Reflects (¬ A) (not b)
¬-reflects (ofʸ a) = ofⁿ (_$ a)
¬-reflects (ofⁿ ¬a) = ofʸ ¬a
_×-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
Reflects (A × B) (a ∧ b)
ofʸ a ×-reflects ofʸ b = ofʸ (a , b)
ofʸ a ×-reflects ofⁿ ¬b = ofⁿ (¬b ∘ proj₂)
ofⁿ ¬a ×-reflects _ = ofⁿ (¬a ∘ proj₁)
_⊎-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
Reflects (A ⊎ B) (a ∨ b)
ofʸ a ⊎-reflects _ = ofʸ (inj₁ a)
ofⁿ ¬a ⊎-reflects ofʸ b = ofʸ (inj₂ b)
ofⁿ ¬a ⊎-reflects ofⁿ ¬b = ofⁿ (¬a ¬-⊎ ¬b)
_→-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
Reflects (A → B) (not a ∨ b)
ofʸ a →-reflects ofʸ b = ofʸ (const b)
ofʸ a →-reflects ofⁿ ¬b = ofⁿ (¬b ∘ (_$ a))
ofⁿ ¬a →-reflects _ = ofʸ (⊥-elim ∘ ¬a)
fromEquivalence : ∀ {b} → (T b → A) → (A → T b) → Reflects A b
fromEquivalence {b = true} sound complete = ofʸ (sound _)
fromEquivalence {b = false} sound complete = ofⁿ complete
det : ∀ {b b′} → Reflects A b → Reflects A b′ → b ≡ b′
det (ofʸ a) (ofʸ _) = refl
det (ofʸ a) (ofⁿ ¬a) = contradiction a ¬a
det (ofⁿ ¬a) (ofʸ a) = contradiction a ¬a
det (ofⁿ ¬a) (ofⁿ _) = refl
T-reflects-elim : ∀ {a b} → Reflects (T a) b → b ≡ a
T-reflects-elim {a} r = det r (T-reflects a)