【问题标题】:Couldn't match type ‘a’ with ‘b’error in Monad instance definition在 Monad 实例定义中无法将类型“a”与“b”错误匹配
【发布时间】:2019-12-16 10:31:06
【问题描述】:

我正在编写一个haskell 程序来执行一堆语句来修改数据记录。我想在没有用户干预的情况下对每个语句的状态进行修改和测试。我想修改Control.Monad.Trans.State.Strict 模块以将我的特定类型合并到一个元组(,) 中。

在出现以下错误之前它运行良好

• Couldn't match type ‘a’ with ‘b’
  ‘a’ is a rigid type variable bound by
    the type signature for:
      (>>=) :: forall a b.
               StateT s m a -> (a -> StateT s m b) -> StateT s m b
    at TestMon.hs:38:7
  ‘b’ is a rigid type variable bound by
    the type signature for:
      (>>=) :: forall a b.
               StateT s m a -> (a -> StateT s m b) -> StateT s m b
    at TestMon.hs:38:7
  Expected type: m (b, s, LogS)
    Actual type: m (a, s, LogS)

我不明白为什么会出现此错误。 我的代码是这样的:

module TestStateJLJ1  where

import Data.Functor.Identity
import Control.Applicative
import Control.Monad

data LogS = LogOK { stepS:: Int ,logS::[String] }
          | LogErr {stepS:: Int , logS::[String] }
         deriving (Show)

initLogS = LogOK { stepS=1, logS=[]}

type State s = StateT s Identity

state :: (Monad m) => ((s,LogS) -> (a, s,LogS)) -> StateT s m a
state f  = StateT (return . f )

newtype StateT s m a = StateT { runStateT :: (s,LogS) -> m (a,s,LogS) }

instance (Functor m) => Functor (StateT s m) where
    fmap f m = StateT $ \ (s,l) ->
        fmap (\ (a, s',l') -> (f a, s',l')) $ runStateT m (s,l)

instance (Functor m, Monad m) => Applicative (StateT s m) where
    pure a = StateT $ \ (s,l) -> return (a, s,l)

    StateT mf <*> StateT mx = StateT $ \ (s,l) -> do
        (f, s',l') <- mf (s,l)
        (x, s'',l'') <- mx (s',l')
        return (f x, s'',l'')

    m *> k = m >>= \_ -> k

instance (Monad m) => Monad (StateT s m) where
    return a = StateT $ \ (s,l) -> return (a, s,l)

    m >>= k  = StateT $ \ (s,l) -> do
        (a, s',l') <- runStateT m (s,l)
        case l' of
           LogOK _ _ -> runStateT (k a) (s',l'{stepS=1+stepS l'})
           LogErr _ _-> do return ( a, s',l') -- <- This line is causing trouble

    fail str = StateT $ \ _ -> fail str

我尝试修改Monad 实例的行为以测试LogS 数据类型的值并根据其值:

  • 执行 stepS 的增量(计算语句的数量)和许多其他事情(尚未实现)并继续执行 monad。

  • 或者停止monad执行,返回实际状态。

您知道我的代码有什么问题以及如何更正吗?

【问题讨论】:

  • 你不能返回a,因为它应该是StateT s m b,而不是StateT s m a。
  • 我已经理解了这个错误,但我真的不知道如何纠正它?有什么想法吗?

标签: haskell monads functor monad-transformers applicative


【解决方案1】:

您需要从其他单子中获取一个页面,该页面在单子计算中间发出错误信号。他们不会 return 错误值,因为这通常是不可能的:m b 类型的单子动作只能 return b 类型的值,而不是其他一些任意类型喜欢(a,s,LogS)。相反,这些其他错误信号单子使用求和类型来表示错误。例如,一元操作Either MyNastyError b 只能return 类型为b 的值(通过使用Right),但它可以使用Left 表示MyNastyError 类型的错误。

此外,您通常无法生成(a,s,LogS),因为对a 类型知之甚少。它只是在最终失败的动作之前立即绑定到整体一元动作中的某些动作的返回类型,因此调用者通常不会准备好对其进行任何操作。不过,您可以返回 (s,LogS),因为该类型将在给定 monad 中的所有操作中固定。

具体来说,您可以将StateT 类型重新定义为:

newtype StateT s m a
  = StateT { runStateT :: (s, LogS) -> m (Maybe a, s, LogS) }
  deriving (Functor)

这使用Maybe a 表示失败。返回 (Just x, s, l) 的计算可以继续,但 (Nothing, s, l) 已准备好停止并转储其状态和日志。

对于这种类型,两个LogS 构造函数现在是多余的,因为Maybe a 值已经指示失败,所以LogS 可以简化为:

data LogS = LogS { stepS :: Int , logS :: [String] }
  deriving (Show)

相应的Applicative 和Monad 实例可以更新为:

instance (Monad m) => Applicative (StateT s m) where
  pure a = StateT $ \(s, l) -> return (Just a, s, l)
  (<*>) = ap
instance (Monad m) => Monad (StateT s m) where
  return = pure
  m >>= k = StateT $ \(s, l) -> do
    (maybeA, s', l') <- runStateT m (s, l)
    case maybeA of
      Nothing -> return (Nothing, s', l')
      Just a -> runStateT (k a) (s', l' {stepS = 1 + stepS l'})
  fail err = StateT $ \(s, l) -> return (Nothing, s, l { logS = err:logS l })

请注意,如果 fail 在这个 monad 中发生“不错”的失败,而不是使用基本 monad 的 fail,我认为它更有意义。我在这里假设logS 字段的顺序是相反的,所以最新的消息会附加到头部。

完整代码:

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE DeriveFunctor #-}

module TestStateJLJ1  where

import Data.Functor.Identity
import Control.Monad

data LogS = LogS { stepS :: Int , logS :: [String] }
  deriving (Show)

initLogS :: LogS
initLogS = LogS 1 []

newtype StateT s m a
  = StateT { runStateT :: (s, LogS) -> m (Maybe a, s, LogS) }
  deriving (Functor)

instance (Monad m) => Applicative (StateT s m) where
  pure a = StateT $ \(s, l) -> return (Just a, s, l)
  (<*>) = ap
instance (Monad m) => Monad (StateT s m) where
  return = pure
  m >>= k = StateT $ \(s, l) -> do
    (maybeA, s', l') <- runStateT m (s, l)
    case maybeA of
      Nothing -> return (Nothing, s', l')
      Just a -> runStateT (k a) (s', l' {stepS = 1 + stepS l'})
  fail err = StateT $ \(s, l) -> return (Nothing, s, l { logS = err:logS l })

type State s = StateT s Identity

state :: (Monad m) => ((s, LogS) -> (Maybe a, s, LogS)) -> StateT s m a
state f = StateT $ return . f

【讨论】:

  • 伟大而准确的答案!
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多