我正在嘗試編寫使用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