LoginSignup
3
1

More than 5 years have passed since last update.

[Haskell]モナドを定義するときのTips(と質問)

Posted at

Haskellでモナドを定義するときって、FunctorとApplicativeのインスタンスにする必要もあって面倒くさいですよね。

そこで、FunctorとApplicativeを以下のように定義すると簡単です。

instance
newtype Foo s = Foo s

instance Functor Foo where
    fmap f mv = do
        v <- mv
        return $ f v

instance Applicative Foo where
    pure = return
    mf <*> mv = do
        f <- mf
        v <- mv
        return $ f v

instance Monad Foo where
    ...

Identity、Maybe、Either、List、Writer、Reader、Stateあたりのモナドはこの定義で正しく動きます。

質問は、「この定義でFunctorとApplicativeを正しく定義できないモナドは存在するか」もしくは、「これ以外のFunctorとApplicativeの定義でモナド則を満たすモナドは定義できるか」ということです。

強い人だれか懇切丁寧に教えて下さい……

3
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1