Indexedモナド
指標付きモナド, Parameterizedモナド, IxMonadとも言う
IxMonadはMonadの一般化である
iとoを同じにすれば通常のMonadを作れる
モナド則を満たす
以下のようなことができる
複数のmonadic computationを適切な順序で実行する
指定した順序で実行できる
monadic computationで出力の型を変えられる
StateモナドをIndexed Stateモナドにしていく
hackage
pursuit
定義
通常のMonad型クラスに、型引数を追加したもの
code:hs
class IxMonad m where
return :: a -> m i i a
(>>=) :: m i j a -> (a -> m j k b) -> m i k b
m i j aの型の意味
mは、moand
通常のmonadにおけるm aのmと同じmrsekut.icon
aは、monadic computationによって生成される値の型
通常のmonadにおけるm aのaと同じmrsekut.icon
iは、monadic computationを実行する前の型
jは、monadic computationの実行した後の型
do記法
既存のdoは、通常のMonadのための糖衣構文なので
IxMonadを楽に書くためには、
元のdoの役割を無視して、
IxMonad用のdoを使用する必要がある
その実現方法
RebindableSyntaxを使う
https://kseo.github.io/posts/2017-01-12-indexed-monads.html
https://jordanmartinez.github.io/purescript-jordans-reference-site/content/11-Syntax/06-Modifying-Do-Ado-Syntax-Sugar/index.html
indexed-do-notation
#古い情報
https://hackage.haskell.org/package/indexed-do-notation
この記事が書かれた当時はRebindableSyntaxがなかった
ユースケース
途中で状態の型を変えるStateモナド
ref StateモナドをIndexed Stateモナドにしていく
React Basic Hooks
ref purescript-react-basic-hooksはIxMonadでHooksの順序を規定する
ハンバーガーを作る
指定した順序でパンやチーズを重ねていく
重ねる順番を間違うと型エラー
PureScript Hyper
既存のHogeモナドをIxMonadで定義し直したものは、Indexed Hoge Monadのように呼ぶ
例
Indexed Stateモナド
Indexed Freeモナド
関連
Superモナド
参考
Kwang's Haskell Blog - Indexed Monads
簡潔でわかりやすい
最初に読むと良さそう
モナドの新しい力!スーパープリキュアモナド! - Haskell-jp
#WIP
全ての通常のMonadは、IxMonadで表現できる
code:hs
newtype MW m p q a = MW { unMW:: m a }
instance Monad m => IxMonad (MW m) where
ireturn = MW . return
ibind (MW m) f = MW (m >>= unMW . f)
MWは幽霊型
purs
code:purs(hs)
newtype Indexed ∷ ∀ ix. (Type → Type) → ix → ix → Type → Type
newtype Indexed m x y a = Indexed (m a)
class IxApply ∷ ∀ ix. (ix → ix → Type → Type) → Constraint
class IxBind ∷ ∀ ix. (ix → ix → Type → Type) → Constraint
なぜこのpackageのbindは引数が通常のモナドに対して入れ替わっているのか #??
https://hackage.haskell.org/package/indexed-0.1/docs/src/Control-Monad-Indexed.html#IxMonadZero
code:hs
class IxApplicative m => IxMonad m where
ibind :: (a -> m j k b) -> m i j a -> m i k b
1,2番めの引数が普通と逆になっているmrsekut.icon
do-notation
https://github.com/isovector/do-notation
https://haskell.jp/blog/posts/2018/super-precure-monad.html
Indexed Monadと普通のMonadを型クラスで抽象化したパッケージ
https://github.com/shiatsumat/wiwinwlh-jp/wiki/高度なモナド#indexed-monads
https://keigoi.hatenadiary.org/entry/20100704/1278223814
https://qiita.com/kimagure/items/a0ee7313e8c7690bf3f5
https://bentnib.org/paramnotions-jfp.pdf
https://stackoverflow.com/questions/28690448/what-is-indexed-monad
https://zenn.dev/nakaji_dayo/articles/999e41568ce0bd
https://twitter.com/paf31/status/1339710564574982144
https://mobile.twitter.com/paf31/status/1231614635754819584
https://wespiser.com/posts/2020-05-06-IxMonad.html
https://zenn.dev/nakaji_dayo/articles/999e41568ce0bd
https://m-hiyama-memo.hatenablog.jp/entry/20111112/1321073864
https://discourse.purescript.org/t/exploration-indexed-capability-design-pattern-with-indexed-monad-transformers/956
https://dev.to/mikesol/modeling-asynchronous-transactions-with-types-part-2-3k85
https://zenn.dev/yukikurage/articles/345d8497ad38d3