Skip to content

Commit 766692e

Browse files
committed
Add Decidable typeclass.
1 parent f3139f7 commit 766692e

File tree

16 files changed

+168
-136
lines changed

16 files changed

+168
-136
lines changed

Class/Applicative/Core.agda

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ open import Class.Prelude
44
open import Class.Core
55
open import Class.Functor.Core
66

7-
{-
8-
Applicative : (Type ℓ → Type ℓ) → Type (lsuc ℓ)
9-
Applicative {ℓ = ℓ} = RawApplicative {f = ℓ}
10-
open RawApplicative ⦃...⦄ public
11-
using (pure)
12-
renaming ( _⊛_ to _<*>_; _<⊛_ to _<*_ ; _⊛>_ to _*>_)
13-
-}
14-
15-
-- record Applicative (F : Type ℓ → Type ℓ′) : Type (lsuc ℓ ⊔ₗ ℓ′) where
167
record Applicative (F : Type↑) : Typeω where
178
infixl 4 _<*>_ _⊛_ _<*_ _<⊛_ _*>_ _⊛>_
189
infix 4 _⊗_
@@ -51,8 +42,7 @@ open Applicative₀ ⦃...⦄ public
5142
record Alternative (F : Type↑) : Typeω where
5243
infixr 3 _<|>_
5344
field _<|>_ : F A F A F A
54-
-- overlap ⦃ ap₀ ⦄ : Applicative₀ F
55-
open Alternative ⦃...⦄ {- hiding (ap₀) -} public
45+
open Alternative ⦃...⦄ public
5646

5747
infix -1 ⋃⁺_ ⋃_
5848

Class/DecEq.agda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ module Class.DecEq where
22

33
open import Class.DecEq.Core public
44
open import Class.DecEq.Instances public
5+
-- open import Class.DecEq.WithK public

Class/DecEq/Derive.agda

Lines changed: 0 additions & 10 deletions
This file was deleted.

Class/DecEq/WithK.agda

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
{-# OPTIONS --safe #-}
1+
{-# OPTIONS --with-K #-}
22
module Class.DecEq.WithK where
33

4-
open import Agda.Primitive using () renaming (Set to Type)
5-
open import Level using (Level)
6-
open import Data.Product
7-
open import Relation.Nullary
8-
open import Relation.Nullary.Decidable
9-
open import Relation.Binary.PropositionalEquality
10-
4+
open import Class.Prelude
115
open import Class.DecEq.Core
126

13-
private variable ℓ ℓ′ : Level
14-
15-
module _ {A : Type ℓ} ⦃ _ : DecEq A ⦄ where
7+
module _ ⦃ _ : DecEq A ⦄ where
168
≟-refl : (x : A) (x ≟ x) ≡ yes refl
179
≟-refl x with refl , p dec-yes (x ≟ x) refl = p
1810

Class/Decidable.agda

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Class.Decidable where
2+
3+
open import Class.Decidable.Core public
4+
open import Class.Decidable.Instances public

Class/Decidable/Core.agda

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module Class.Decidable.Core where
2+
3+
open import Class.Prelude
4+
5+
open import Relation.Nullary.Decidable using (True; False; toWitness; toWitnessFalse)
6+
7+
record _⁇ (P : Type ℓ) : Type ℓ where
8+
constructor ⁇_
9+
field dec : Dec P
10+
11+
auto : ⦃ True dec ⦄ P
12+
auto ⦃ pr ⦄ = toWitness pr
13+
14+
contradict : {X : Type} {pr : False dec} P X
15+
contradict {pr = pr} = ⊥-elim ∘ toWitnessFalse pr
16+
17+
open _⁇ ⦃ ... ⦄ public
18+
19+
¿_¿ : (X : Type ℓ) ⦃ X ⁇ ⦄ Dec X
20+
¿ _ ¿ = dec
21+
22+
module _ {A : Type ℓ} where
23+
24+
_⁇¹ : Pred A ℓ′ Type (ℓ ⊔ ℓ′)
25+
P ⁇¹ = {x} P x ⁇
26+
27+
dec¹ : {P : Pred A ℓ′} ⦃ P ⁇¹ ⦄ Decidable¹ P
28+
dec¹ _ = dec
29+
30+
¿_¿¹ : (P : Pred A ℓ′) ⦃ P ⁇¹ ⦄ Decidable¹ P
31+
¿ _ ¿¹ = dec¹
32+
33+
module _ {A B : Type ℓ} where
34+
35+
_⁇² : REL A B ℓ′ Type (ℓ ⊔ ℓ′)
36+
_~_ ⁇² = {x y} (x ~ y) ⁇
37+
38+
dec² : {_~_ : REL A B ℓ′} ⦃ _~_ ⁇² ⦄ Decidable² _~_
39+
dec² _ _ = dec
40+
41+
¿_¿² : (_~_ : REL A B ℓ′) ⦃ _~_ ⁇² ⦄ Decidable² _~_
42+
¿ _ ¿² = dec²
43+
44+
module _ {A B C : Type ℓ} where
45+
46+
_⁇³ : (P : 3REL A B C ℓ′) Type (ℓ ⊔ ℓ′)
47+
_~_~_ ⁇³ = {x y z} (x ~ y ~ z) ⁇
48+
49+
dec³ : {_~_~_ : 3REL A B C ℓ′} ⦃ _~_~_ ⁇³ ⦄ Decidable³ _~_~_
50+
dec³ _ _ _ = dec
51+
52+
¿_¿³ : (_~_~_ : 3REL A B C ℓ′) ⦃ _~_~_ ⁇³ ⦄ Decidable³ _~_~_
53+
¿ _ ¿³ = dec³
54+
55+
infix -100 auto∶_
56+
auto∶_ : (X : Type ℓ) ⦃ X ⁇ ⦄ Type
57+
auto∶_ A = True ¿ A ¿

Class/Decidable/Instances.agda

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module Class.Decidable.Instances where
2+
3+
open import Class.Prelude
4+
open import Class.Decidable.Core
5+
open import Class.DecEq.Core
6+
7+
instance
8+
Dec-⊥ : ⊥ ⁇
9+
Dec-⊥ .dec = no λ()
10+
11+
Dec-⊤ : ⊤ ⁇
12+
Dec-⊤ .dec = yes tt
13+
14+
Dec-→ : ⦃ A ⁇ ⦄ ⦃ B ⁇ ⦄ (A B) ⁇
15+
Dec-→ .dec = dec →-dec dec
16+
where open import Relation.Nullary.Implication using (_→-dec_)
17+
18+
Dec-× : ⦃ A ⁇ ⦄ ⦃ B ⁇ ⦄ (A × B) ⁇
19+
Dec-× .dec = dec ×-dec dec
20+
where open import Relation.Nullary.Product using (_×-dec_)
21+
22+
Dec-⊎ : ⦃ A ⁇ ⦄ ⦃ B ⁇ ⦄ (A ⊎ B) ⁇
23+
Dec-⊎ .dec = dec ⊎-dec dec
24+
where open import Relation.Nullary.Sum using (_⊎-dec_)
25+
26+
DecEq⇒Dec : ⦃ DecEq A ⦄ _≡_ {A = A} ⁇²
27+
DecEq⇒Dec .dec = _ ≟ _
28+
29+
import Data.Bool as 𝔹
30+
31+
Dec-T : {t} T t ⁇
32+
Dec-T .dec = 𝔹.T? _
33+
34+
import Data.List.Relation.Unary.All as L
35+
import Data.List.Relation.Unary.Any as L
36+
37+
private variable n : ℕ; P¹ : Pred A ℓ; P² : Rel A ℓ
38+
39+
Dec-All : ⦃ P¹ ⁇¹ ⦄ L.All P¹ ⁇¹
40+
Dec-All .dec = L.all? dec¹ _
41+
42+
Dec-Any : ⦃ P¹ ⁇¹ ⦄ L.Any P¹ ⁇¹
43+
Dec-Any .dec = L.any? dec¹ _
44+
45+
import Data.List.Relation.Unary.AllPairs as AP
46+
47+
Dec-AllPairs : ⦃ P² ⁇² ⦄ AP.AllPairs P² ⁇¹
48+
Dec-AllPairs .dec = AP.allPairs? dec² _
49+
50+
open import Data.Vec as V
51+
open import Data.Vec.Relation.Unary.All as V
52+
open import Data.Vec.Relation.Unary.Any as V
53+
54+
Dec-VAll : ⦃ P¹ ⁇¹ ⦄ V.All P¹ {n} ⁇¹
55+
Dec-VAll .dec = V.all? dec¹ _
56+
57+
Dec-VAny : ⦃ P¹ ⁇¹ ⦄ V.Any P¹ {n} ⁇¹
58+
Dec-VAny .dec = V.any? dec¹ _
59+
60+
import Data.Maybe as M
61+
import Data.Maybe.Relation.Unary.All as M renaming (dec to all?)
62+
import Data.Maybe.Relation.Unary.Any as M renaming (dec to any?)
63+
64+
Dec-MAll : ⦃ _ : P¹ ⁇¹ ⦄ M.All P¹ ⁇¹
65+
Dec-MAll .dec = M.all? dec¹ _
66+
67+
Dec-MAny : ⦃ _ : P¹ ⁇¹ ⦄ M.Any P¹ ⁇¹
68+
Dec-MAny .dec = M.any? dec¹ _
69+
70+
private
71+
open import Data.List.Membership.Propositional using (_∈_; _∉_)
72+
open import Class.DecEq.Instances
73+
74+
0⋯2 = List ℕ ∋ 012 ∷ []
75+
76+
_ = 1 ∈ 0⋯2
77+
∋ auto
78+
_ = 3 ∉ 0⋯2
79+
∋ auto
80+
f = (3 ∈ 0⋯2 23)
81+
∋ contradict

Class/Functor/Core.agda

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ open import Class.Core
55

66
private variable a b c : Level
77

8-
{-
9-
Functor : (Type ℓ → Type ℓ) → Type (lsuc ℓ)
10-
Functor {ℓ = ℓ} = RawFunctor {ℓ = ℓ} {ℓ′ = ℓ}
11-
open RawFunctor ⦃...⦄ public
12-
-}
13-
148
record Functor (F : Type↑) : Typeω where
159
infixl 4 _<$>_ _<$_
1610
infixl 1 _<&>_

Class/Functor/Derive.agda

Lines changed: 0 additions & 11 deletions
This file was deleted.

Class/Monad/Core.agda

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Class.Monad.Core where
22

33
open import Class.Prelude
4+
open import Class.Core
45
open import Class.Functor
56
open import Class.Applicative
67

7-
record Monad (M : {a} Type a Type a) : Typeω where
8+
record Monad (M : Type) : Typeω where
89
infixl 1 _>>=_ _>>_ _>=>_
910
infixr 1 _=<<_ _<=<_
1011

Class/MonadError.agda

Lines changed: 0 additions & 35 deletions
This file was deleted.

Class/MonadError/Instances.agda

Lines changed: 0 additions & 5 deletions
This file was deleted.

Class/MonadReader.agda

Lines changed: 0 additions & 37 deletions
This file was deleted.

Class/MonadReader/Instances.agda

Lines changed: 0 additions & 5 deletions
This file was deleted.

Class/Prelude.agda

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ open import Level public
77
open import Function public
88
using (id; _∘_; _∋_; _$_; const; constᵣ; flip)
99

10+
open import Data.Empty public
11+
using (⊥; ⊥-elim)
1012
open import Data.Unit public
1113
using (⊤; tt)
1214
open import Data.Product public
1315
using (_×_; _,_; proj₁; proj₂; Σ; ∃; -,_)
1416
open import Data.Sum public
1517
using (_⊎_; inj₁; inj₂)
1618
open import Data.Bool public
17-
using (Bool; true; false; not; if_then_else_)
19+
using (Bool; true; false; not; if_then_else_; T)
1820
open import Data.Nat public
1921
using (ℕ; zero; suc)
2022
open import Data.Fin as Fin public
@@ -39,12 +41,21 @@ open import Data.These public
3941
using (These; this; that; these)
4042

4143
open import Relation.Nullary public
42-
using (yes; no)
44+
using (Dec; yes; no)
4345
open import Relation.Nullary.Decidable public
4446
using (⌊_⌋; dec-yes)
47+
open import Relation.Unary public
48+
using (Pred)
49+
renaming (Decidable to Decidable¹)
4550
open import Relation.Binary public
46-
using (Rel; DecidableEquality)
51+
using (REL; Rel; DecidableEquality)
4752
renaming (Decidable to Decidable²)
53+
module _ {a b c} where
54+
3REL : (A : Set a) (B : Set b) (C : Set c) (ℓ : Level) Type _
55+
3REL A B C ℓ = A B C Type ℓ
56+
57+
Decidable³ : {A B C ℓ} 3REL A B C ℓ Type _
58+
Decidable³ _~_~_ = x y z Dec (x ~ y ~ z)
4859
open import Relation.Binary.PropositionalEquality public
4960
using (_≡_; refl; sym; cong)
5061

Classes.agda

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Classes where
22

3+
-- ** Algebraic structures
34
open import Class.Semigroup public
45
open import Class.Monoid public
56
open import Class.Functor public
@@ -8,7 +9,10 @@ open import Class.Applicative public
89
open import Class.Monad public
910
open import Class.Traversable public
1011

11-
open import Class.Show public
12+
-- ** Decidability
1213
open import Class.DecEq public
14+
open import Class.Decidable public
1315

16+
-- ** Others
17+
open import Class.Show public
1418
open import Class.Default public

0 commit comments

Comments
 (0)