0
我得到「MonadState BTState BT沒有實例」,我不知道我在做什麼錯誤。我試過在不同的地方添加約束,把MonadState推導()子句中,等沒有用於MonadState的實例
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
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 a = BT { insideBT :: ErrorT String (State BTState) a }
deriving(Monad)
runBT a s = runState (runErrorT $ insideBT a) s
instance RandMonad BT where
getGen = BT $ gets bRandState
putGen g = BT $ do { s <- get; put s {bRandState=g} }
-- trying to use BT
backtrackBT :: BT Int
backtrackBT = do
s <- get
put s {bGoalN=2}
return 3