Skip to content

Commit 9ee0bde

Browse files
committed
Change Monad polymorphism
1 parent aa62ce6 commit 9ee0bde

File tree

12 files changed

+128
-21
lines changed

12 files changed

+128
-21
lines changed

Class/Applicative/Core.agda

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open import Class.Prelude
55
open import Class.Core
66
open import Class.Functor.Core
77

8-
record Applicative (F : Type↑) : Typeω where
8+
record Applicative (F : Type↑ ℓ↑) : Typeω where
99
infixl 4 _<*>_ _⊛_ _<*_ _<⊛_ _*>_ _⊛>_
1010
infix 4 _⊗_
1111

@@ -34,13 +34,13 @@ record Applicative (F : Type↑) : Typeω where
3434

3535
open Applicative ⦃...⦄ public
3636

37-
record Applicative₀ (F : Type↑) : Typeω where
37+
record Applicative₀ (F : Type↑ ℓ↑) : Typeω where
3838
field
3939
overlap ⦃ super ⦄ : Applicative F
4040
ε₀ : F A
4141
open Applicative₀ ⦃...⦄ public
4242

43-
record Alternative (F : Type↑) : Typeω where
43+
record Alternative (F : Type↑ ℓ↑) : Typeω where
4444
infixr 3 _<|>_
4545
field _<|>_ : F A F A F A
4646
open Alternative ⦃...⦄ public

Class/Bifunctor.agda

+20-6
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,25 @@ instance
3434
Bifunctor-Σ .bimap′ = ×.map
3535

3636
-- ** non-dependent version
37-
record Bifunctor (F : Type a Type b Type (a ⊔ b)) : Type (lsuc (a ⊔ b)) where
37+
Type[_∣_↝_] : ℓ ℓ′ ℓ″ Type _
38+
Type[ ℓ ∣ ℓ′ ↝ ℓ″ ] = Type ℓ Type ℓ′ Type ℓ″
39+
40+
Level↑² = Level Level Level
41+
42+
Type↑² : Level↑² Typeω
43+
Type↑² ℓ↑² = {ℓ ℓ′} Type[ ℓ ∣ ℓ′ ↝ ℓ↑² ℓ ℓ′ ]
44+
45+
variable
46+
ℓ↑² : Level Level Level
47+
48+
record Bifunctor (F : Type↑² ℓ↑²) : Typeω where
3849
field
3950
bimap : {A A′ : Type a} {B B′ : Type b} (A A′) (B B′) F A B F A′ B′
4051

52+
-- record Bifunctor {a}{b} (F : Type a → Type b → Type (a ⊔ b)) : Type (lsuc (a ⊔ b)) where
53+
-- field
54+
-- bimap : ∀ {A A′ : Type a} {B B′ : Type b} → (A → A′) → (B → B′) → F A B → F A′ B′
55+
4156
map₁ : {A A′ : Type a} {B : Type b} (A A′) F A B F A′ B
4257
map₁ f = bimap f id
4358
_<$>₁_ = map₁
@@ -50,16 +65,15 @@ record Bifunctor (F : Type a → Type b → Type (a ⊔ b)) : Type (lsuc (a ⊔
5065

5166
open Bifunctor ⦃...⦄ public
5267

53-
map₁₂ : {F : Type a Type a Type a} {A B : Type a}
54-
⦃ Bifunctor F ⦄
55-
(A B) F A A F B B
68+
map₁₂ : {F : Type↑² ℓ↑²} ⦃ _ : Bifunctor F ⦄
69+
( {a} {A B : Type a} (A B) F A A F B B)
5670
map₁₂ f = bimap f f
5771
_<$>₁₂_ = map₁₂
5872
infixl 4 _<$>₁₂_
5973

6074
instance
61-
Bifunctor-× : Bifunctor {a}{b} _×_
75+
Bifunctor-× : Bifunctor _×_
6276
Bifunctor-× .bimap f g = ×.map f g
6377

64-
Bifunctor-⊎ : Bifunctor {a}{b} _⊎_
78+
Bifunctor-⊎ : Bifunctor _⊎_
6579
Bifunctor-⊎ .bimap = ⊎.map

Class/Core.agda

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ open import Class.Prelude
66
Type[_↝_] : ℓ ℓ′ Type (lsuc ℓ ⊔ lsuc ℓ′)
77
Type[ ℓ ↝ ℓ′ ] = Type ℓ Type ℓ′
88

9-
Type↑ : Typeω
10-
Type↑ = {ℓ} Type[ ℓ ↝ ℓ ]
9+
Level↑ = Level Level
1110

12-
module _ (M : Type↑) where
11+
variable ℓ↑ ℓ↑′ ℓ↑″ : Level↑
12+
13+
Type↑ : Level↑ Typeω
14+
Type↑ ℓ↑ = {ℓ} Type[ ℓ ↝ ℓ↑ ℓ ]
15+
16+
variable M F : Type↑ ℓ↑
17+
18+
module _ (M : Type↑ ℓ↑) where
1319
: (A Type ℓ) Type _
1420
_¹ P = {x} M (P x)
1521

@@ -18,6 +24,3 @@ module _ (M : Type↑) where
1824

1925
: (A B C Type ℓ) Type _
2026
_³ _~_~_ = {x y z} M (x ~ y ~ z)
21-
22-
variable
23-
M F : Type↑

Class/Foldable/Core.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ open import Class.Functor
77
open import Class.Semigroup
88
open import Class.Monoid
99

10-
record Foldable (F : Type↑) ⦃ _ : Functor F ⦄ : Typeω where
10+
record Foldable (F : Type↑ ℓ↑) ⦃ _ : Functor F ⦄ : Typeω where
1111
field fold : ⦃ _ : Semigroup A ⦄ ⦃ Monoid A ⦄ F A A
1212
open Foldable ⦃...⦄ public

Class/Functor/Core.agda

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open import Class.Core
66

77
private variable a b c : Level
88

9-
record Functor (F : Type↑) : Typeω where
9+
record Functor (F : Type↑ ℓ↑) : Typeω where
1010
infixl 4 _<$>_ _<$_
1111
infixl 1 _<&>_
1212

@@ -20,7 +20,7 @@ record Functor (F : Type↑) : Typeω where
2020
_<&>_ = flip _<$>_
2121
open Functor ⦃...⦄ public
2222

23-
record FunctorLaws (F : Type↑) ⦃ _ : Functor F ⦄ : Typeω where
23+
record FunctorLaws (F : Type↑ ℓ↑) ⦃ _ : Functor F ⦄ : Typeω where
2424
field
2525
-- preserves identity morphisms
2626
fmap-id : {A : Type a} (x : F A)

Class/Monad/Core.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open import Class.Core
66
open import Class.Functor
77
open import Class.Applicative
88

9-
record Monad (M : Type↑) : Typeω where
9+
record Monad (M : Type↑ ℓ↑) : Typeω where
1010
infixl 1 _>>=_ _>>_ _>=>_
1111
infixr 1 _=<<_ _<=<_
1212

Class/Monad/Instances.agda

+12
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ instance
1919
.return just
2020
._>>=_ Maybe._>>=_
2121
where import Data.Maybe as Maybe
22+
23+
Monad-Sumˡ : Monad (_⊎ A)
24+
Monad-Sumˡ = λ where
25+
.return inj₁
26+
._>>=_ (inj₁ a) f f a
27+
._>>=_ (inj₂ b) f inj₂ b
28+
29+
Monad-Sumʳ : Monad (A ⊎_)
30+
Monad-Sumʳ = λ where
31+
.return inj₂
32+
._>>=_ (inj₁ a) f inj₁ a
33+
._>>=_ (inj₂ b) f f b

Class/Traversable/Core.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open import Class.Core
66
open import Class.Functor.Core
77
open import Class.Monad
88

9-
record Traversable (F : Type↑) ⦃ _ : Functor F ⦄ : Typeω where
9+
record Traversable (F : Type↑ ℓ↑) ⦃ _ : Functor F ⦄ : Typeω where
1010
field sequence : ⦃ Monad M ⦄ F (M A) M (F A)
1111

1212
traverse : ⦃ Monad M ⦄ (A M B) F A M (F B)

Test/Functor.agda

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ _ = map₂′ id (1 , 2 ∷ []) ≡ ((∃ λ n → Vec ℕ n) ∋ (1 , 2 ∷ [])
3030
∋ refl
3131
_ = bimap′ suc (2 ∷_) (0 , []) ≡ ((∃ λ n Vec ℕ n) ∋ (1 , 2 ∷ []))
3232
∋ refl
33+
34+
-- ** cross-level mapping
35+
module _ (X : Type) (Y : Type₁) (Z : Type₂) (g : X Y) (f : Y Z) (xs : List X) where
36+
_ : fmap (f ∘ g) xs
37+
≡ (fmap f ∘ fmap g) xs
38+
_ = fmap-∘ xs

Test/Monad.agda

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{-# OPTIONS --cubical-compatible #-}
2+
module Test.Monad where
3+
4+
open import Data.List using (length)
5+
6+
open import Class.Core
7+
open import Class.Prelude
8+
open import Class.Functor
9+
open import Class.Monad
10+
11+
-- ** maybe monad
12+
13+
pred? : Maybe ℕ
14+
pred? = λ where
15+
0 nothing
16+
(suc n) just n
17+
18+
getPred : Maybe ℕ
19+
getPred = λ n do
20+
x pred? n
21+
return x
22+
23+
_ = getPred 3 ≡ just 2
24+
∋ refl
25+
26+
-- ** reader monad
27+
28+
ReaderT : (M : Type↑ ℓ↑) Type ℓ Type ℓ′ _
29+
ReaderT M A B = A M B
30+
31+
instance
32+
Monad-ReaderT : ⦃ _ : Monad M ⦄ Monad (ReaderT M A)
33+
Monad-ReaderT = λ where
34+
.return λ x _ return x
35+
._>>=_ m k λ a m a >>= λ b k b a
36+
37+
Reader : Type ℓ Type ℓ′ Type _
38+
Reader = ReaderT id
39+
40+
instance
41+
Monad-Id : Monad id
42+
Monad-Id = λ where
43+
.return id
44+
._>>=_ m k k m
45+
{-# INCOHERENT Monad-Id #-}
46+
47+
ask : Reader A A
48+
ask a = a
49+
50+
local : (A B) Reader B C Reader A C
51+
local f r = r ∘ f
52+
53+
runReader : A Reader A B B
54+
runReader = flip _$_
55+
56+
getLength : List (Maybe ℕ)
57+
getLength ys = runReader ys $ local (just 0 ∷_) $ do
58+
xs ask
59+
return (length xs)
60+
61+
_ = getLength (just 1 ∷ nothing ∷ just 2 ∷ []) ≡ 4
62+
∋ refl

Test/Reflection.agda

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Test.Reflection where
2+
3+
open import Reflection hiding (_>>=_)
4+
open import Data.Nat using (zero)
5+
open import Class.Monad
6+
7+
-- ** cross-level bind
8+
_ : TC Set
9+
_ = getType (quote zero) >>= unquoteTC

standard-library-classes.agda

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ open import Class.HasAdd public
2525
open import Class.HasOrder public
2626
open import Class.Show public
2727
open import Class.ToBool public
28-
open import Class.MonotonePredicate public
28+
open import Class.MonotonePredicate public
2929

3030
-- ** Tests
3131
open import Test.Monoid
3232
open import Test.Functor
33+
open import Test.Monad
3334
open import Test.DecEq
3435
open import Test.Decidable
3536
open import Test.Show

0 commit comments

Comments
 (0)