无法在Monad实例定义中将类型“ a”与错误“ b”匹配

问题描述 投票:2回答:1

我正在编写一个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执行并返回实际状态。

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

haskell monads functor monad-transformers applicative
1个回答
0
投票

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

而且,由于对类型(a,s,LogS)的了解不足,因此您通常将无法产生a。只是某些操作的返回类型,该操作在操作最终失败之前就已绑定到整个monadic操作中,因此调用方通常不会准备对其执行任何操作。不过,您可以返回(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)

可以将适当的ApplicativeMonad实例更新为:

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
© www.soinside.com 2019 - 2024. All rights reserved.