Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0ee6e77

Browse files
authoredSep 13, 2023
Monadic join (#2079)
1 parent 01160b8 commit 0ee6e77

15 files changed

+50
-8
lines changed
 

‎CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,9 @@ Non-backwards compatible changes
692692
This reorganisation means in particular that the functor/applicative of a monad
693693
are not computed using `_>>=_`. This may break proofs.
694694
695+
* When `F : Set f → Set f` we moreover have a definable join/μ operator
696+
`join : (M : RawMonad F) → F (F A) → F A`.
697+
695698
* We now have `RawEmpty` and `RawChoice` respectively packing `empty : M A` and
696699
`(<|>) : M A → M A → M A`. `RawApplicativeZero`, `RawAlternative`, `RawMonadZero`,
697700
`RawMonadPlus` are all defined in terms of these.
@@ -2604,14 +2607,13 @@ Other minor changes
26042607
26052608
diagonal : Vec (Vec A n) n → Vec A n
26062609
DiagonalBind._>>=_ : Vec A n → (A → Vec B n) → Vec B n
2607-
join : Vec (Vec A n) n → Vec A n
26082610
26092611
_ʳ++_ : Vec A m → Vec A n → Vec A (m + n)
26102612
26112613
cast : .(eq : m ≡ n) → Vec A m → Vec A n
26122614
```
26132615

2614-
* Added new instance in `Data.Vec.Categorical`:
2616+
* Added new instance in `Data.Vec.Effectful`:
26152617
```agda
26162618
monad : RawMonad (λ (A : Set a) → Vec A n)
26172619
```

‎src/Data/List/Effectful.agda

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ open P.≡-Reasoning
2525
private
2626
variable
2727
: Level
28+
A : Set
2829

2930
------------------------------------------------------------------------
3031
-- List applicative functor
@@ -66,6 +67,9 @@ monad = record
6667
; _>>=_ = flip concatMap
6768
}
6869

70+
join : List (List A) List A
71+
join = Join.join monad
72+
6973
monadZero : {ℓ} RawMonadZero {ℓ} List
7074
monadZero = record
7175
{ rawMonad = monad

‎src/Data/Maybe/Effectful.agda

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ monad = record
6767
; _>>=_ = _>>=_
6868
}
6969

70+
join : Maybe (Maybe A) Maybe A
71+
join = Join.join monad
72+
7073
monadZero : RawMonadZero {f} Maybe
7174
monadZero = record
7275
{ rawMonad = monad

‎src/Data/Sum/Effectful/Left.agda

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ monad = record
6262
; _>>=_ = [ const ∘′ inj₁ , _|>′_ ]′
6363
}
6464

65+
join : {B : Set (a ⊔ b)} Sumₗ (Sumₗ B) Sumₗ B
66+
join = Join.join monad
67+
6568
------------------------------------------------------------------------
6669
-- Get access to other monadic functions
6770

‎src/Data/Sum/Effectful/Right.agda

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ monad = record
5656
; _>>=_ = [ _|>′_ , const ∘′ inj₂ ]′
5757
}
5858

59+
join : {A : Set (a ⊔ b)} Sumᵣ (Sumᵣ A) Sumᵣ A
60+
join = Join.join monad
61+
5962
monadZero : B RawMonadZero Sumᵣ
6063
monadZero b = record
6164
{ rawMonad = monad

‎src/Data/Vec.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
-- See `Data.Vec.Functional` for an alternative implementation as
1111
-- functions from finite sets, which is more suitable for reasoning
12-
-- about fixed sized vectors and for when ease of retrevial is
12+
-- about fixed sized vectors and for when ease of retrieval is
1313
-- important.
1414

1515
{-# OPTIONS --cubical-compatible --safe #-}

‎src/Data/Vec/Base.agda

+1-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ module DiagonalBind where
189189
_>>=_ : Vec A n (A Vec B n) Vec B n
190190
xs >>= f = diagonal (map f xs)
191191

192-
join : Vec (Vec A n) n Vec A n
193-
join = _>>= id
194192

195193
------------------------------------------------------------------------
196194
-- Operations for reducing vectors
@@ -358,3 +356,4 @@ last {n = suc n} (_ ∷ xs) = last xs
358356
transpose : Vec (Vec A n) m Vec (Vec A m) n
359357
transpose [] = replicate []
360358
transpose (as ∷ ass) = replicate _∷_ ⊛ as ⊛ transpose ass
359+

‎src/Data/Vec/Effectful.agda

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open import Data.Vec.Base as Vec hiding (_⊛_)
1414
open import Data.Vec.Properties
1515
open import Effect.Applicative as App using (RawApplicative)
1616
open import Effect.Functor as Fun using (RawFunctor)
17-
open import Effect.Monad using (RawMonad; RawMonadT; mkRawMonad)
17+
open import Effect.Monad using (RawMonad; module Join; RawMonadT; mkRawMonad)
1818
import Function.Identity.Effectful as Id
1919
open import Function.Base
2020
open import Level using (Level)
@@ -46,6 +46,9 @@ monad = record
4646
; _>>=_ = DiagonalBind._>>=_
4747
}
4848

49+
join : Vec (Vec A n) n Vec A n
50+
join = Join.join monad
51+
4952
------------------------------------------------------------------------
5053
-- Get access to other monadic functions
5154

‎src/Data/Vec/Effectful/Transformer.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ monad {f} {g} M = record
5050
; _>>=_ = λ ma k mkVecT $ do
5151
a runVecT ma
5252
bs mapM {a = f} (runVecT ∘′ k) a
53-
pure (Vec.DiagonalBind.join bs)
53+
pure (Vec.diagonal bs)
5454
} where open RawMonad M; open Vec.TraversableM {m = f} {n = g} M
5555

5656
monadT : RawMonadT {f} {g} (VecT n)

‎src/Effect/Monad.agda

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ open import Data.Unit.Polymorphic.Base using (⊤)
1616
open import Effect.Choice
1717
open import Effect.Empty
1818
open import Effect.Applicative
19-
open import Function.Base using (flip; _$′_; _∘′_)
19+
open import Function.Base using (id; flip; _$′_; _∘′_)
2020
open import Level using (Level; suc; _⊔_)
2121

2222
private
@@ -58,7 +58,16 @@ record RawMonad (F : Set f → Set g) : Set (suc f ⊔ g) where
5858
unless : Bool F ⊤ F ⊤
5959
unless = when ∘′ not
6060

61+
-- When level g=f, a join/μ operator is definable
62+
63+
module Join {F : Set f Set f} (M : RawMonad F) where
64+
open RawMonad M
65+
66+
join : F (F A) F A
67+
join = _>>= id
68+
6169
-- Smart constructor
70+
6271
module _ where
6372

6473
open RawMonad

‎src/Effect/Monad/Identity.agda

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ open import Level
1818
private
1919
variable
2020
a : Level
21+
A : Set a
2122

2223
record Identity (A : Set a) : Set a where
2324
constructor mkIdentity
@@ -47,3 +48,6 @@ comonad = record
4748
{ extract = runIdentity
4849
; extend = λ f a mkIdentity (f a)
4950
}
51+
52+
join : Identity (Identity A) Identity A
53+
join = Join.join monad

‎src/Effect/Monad/Partiality.agda

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ monad = record
7272
; _>>=_ = bind
7373
}
7474

75+
join : (A ⊥) ⊥ A ⊥
76+
join = Join.join monad
77+
7578
private module M {f} = RawMonad (monad {f})
7679

7780
-- Non-termination.

‎src/Effect/Monad/Reader.agda

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ applicative = Trans.applicative Id.applicative
5252
monad : RawMonad (Reader R)
5353
monad = Trans.monad Id.monad
5454

55+
join : Reader R (Reader R A) Reader R A
56+
join = Join.join monad
57+
5558
------------------------------------------------------------------------
5659
-- Reader monad specifics
5760

‎src/Effect/Monad/State.agda

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ applicative = Trans.applicative Id.monad
5959
monad : RawMonad (State S)
6060
monad = Trans.monad Id.monad
6161

62+
join : State S (State S A) State S A
63+
join = Join.join monad
64+
6265
------------------------------------------------------------------------
6366
-- State monad specifics
6467

‎src/Effect/Monad/Writer.agda

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ module _ {𝕎 : RawMonoid w ℓ} where
5555
monad : RawMonad (Writer 𝕎)
5656
monad = Trans.monad Id.monad
5757

58+
join : Writer 𝕎 (Writer 𝕎 A) Writer 𝕎 A
59+
join = Join.join monad
60+
5861
----------------------------------------------------------------------
5962
-- Writer monad specifics
6063

0 commit comments

Comments
 (0)
Please sign in to comment.