Skip to content

Commit

Permalink
Upstream ToBool class
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatisRT committed May 31, 2024
1 parent a37406f commit 30ea3cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Class/ToBool.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# OPTIONS --safe #-}
module Class.ToBool where

open import Level
open import Data.Bool using (Bool; true; false)
open import Data.Sum
open import Function
open import Data.Unit.Polymorphic
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary
open import Data.Maybe
open import Class.Decidable.Core

private variable
ℓ ℓ′ : Level
X : Set ℓ; P : X Set

record ToBool′ (A : Set ℓ) (P 𝕋 𝔽 : A Set ℓ′) : Set (ℓ ⊔ ℓ′) where
field decide : (a : A) ⦃ P a ⦄ 𝕋 a ⊎ 𝔽 a

infix -10 if_then_else_
if_then_else_ : (a : A) ⦃ _ : P a ⦄ ({𝕋 a} X) ({𝔽 a} X) X
if a then t else f =
case decide a of λ where
(inj₁ 𝕥) t {𝕥}
(inj₂ 𝕗) f {𝕗}

toBool : (a : A) ⦃ _ : P a ⦄ Bool
toBool a = if a then true else false
open ToBool′ ⦃...⦄ public

ToBool : (A : Set ℓ) (𝕋 𝔽 : A Set ℓ′) Set (ℓ ⊔ ℓ′)
ToBool {ℓ} A = ToBool′ A (λ _ ⊤)

instance
ToBool-Bool : ToBool Bool (_≡ true) (_≡ false)
ToBool-Bool .decide = λ where
true inj₁ refl
false inj₂ refl

ToBool-Dec : ToBool (Dec X) (const X) (const $ ¬ X)
ToBool-Dec .decide = λ where
(yes x) inj₁ x
(no ¬x) inj₂ ¬x

ToBool-Maybe : ToBool (Maybe X) (const X) (const ⊤)
ToBool-Maybe .decide = λ where
(just x) inj₁ x
nothing inj₂ tt

ToBool-⁇ : ToBool′ (Set ℓ) _⁇ id ¬_
ToBool-⁇ .decide _ = decide dec ⦃ _ ⦄
1 change: 1 addition & 0 deletions Classes.agda
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ open import Class.Decidable public
-- ** Others
open import Class.Show public
open import Class.Default public
open import Class.ToBool public

-- ** Tests
open import Test.Monoid
Expand Down

0 comments on commit 30ea3cb

Please sign in to comment.