haskell
1type State s a = s -> (a, s)
このシノニムを Monad インスタンスにできません。
Haskell の Monad インスタンスは、
haskell
1M :: Type -> Type
2instance Monad M where
3 ...
の形しかできません。
((->) r)
と ((,) w)
は下記の通りこの形にあっています。
haskell
1(->) r x :: Type
2(->) r :: Type -> Type
3instance Monad ((->) r) where
4 ...
haskell
1(,) w x :: Type
2(,) w :: Type -> Type
3instance Monad ((,) w) where
4 ...
ですが、s -> (x, s)
はその形にあっていません。
haskell
1(->) s ((,) x s) :: Type
2??? :: Type -> Type
s
は引数にも戻り値にもあるから、シノニムだけで Type -> Type
の形にならないのです。
それができる言語もあるけど、Haskell ではありません。できるのに Kind-level の高階関数が必要です。Coq, Agda, Lean prover みたいな証明支援システムができるかもしれません。が、Lean の monad のソースを読んだら、そちらも新しいコンストラクタを作っています。
newtype
を用いて、Type -> Type
の形にできます。
haskell
1newtype State s x = State { runState :: s -> (x, s) }
2
3State s x :: Type
4State s :: Type -> Type
5instance Monad (State s) where
6 ...
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。