我正在尝试编写使用monad变换器的函数,但是关于我的monadic类型的确切结构是不可知的。尽管我不完全明白自己在做什么 - 试图遵循#haskell的建议。这个想法是我想写像覆盖条件失败,因为它的一个依赖关系
doSomething :: (MonadRandom m, MonadError MyError m) => Arg -> m Result
功能(其中M为一变压器是添加随机生成的状态和错误处理的堆栈)
我开始写一些状态:
{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses, FlexibleInstances #-}
import System.Random
import Control.Monad.Error
import Control.Monad.State
{-
-- I want to make a class of monads which contain random generator state.
class Monad m => RandMonad m where
putGen :: StdGen -> m()
getGen :: m StdGen
-}
-- the following creates a monadic type BT
data BTState = BTState
{ bGoalN :: Int
, bRandState :: StdGen }
newtype BT m a = BT { runBT :: StateT BTState m a }
-- what the following does is say that if e and m can be used the
-- way a monad error can be used, then so can e and (BT m)
instance MonadError e m => MonadError e (BT m) where
throwError x = BT (throwError x)
-- edit: I added the following definition but I'm still getting the same error
-- In fact I tried every conceivable definition of catchError and still get the
-- same error about the coverage condition
catchError (BT x) y = BT (catchError y)
当我运行此我得到“为‘MonadError E(BT M)’非法实例声明(覆盖条件它的一个依赖失败)”
我是哈斯克尔l新手,所以我不知道这是什么意思。
编辑以反映尝试定义'catchError' – composerMike