提前抱歉这篇长篇文章。
我在Haskell中编写了一个事件驱动的应用程序,因此我需要存储几个回调函数以供进一步使用。我希望这样的回调是:
ReaderT
,ErrorT
,StateT
而非裸IO
s; (MonadIO m, MonadReader MyContext m, MonadState MyState m, MonadError MyError m) => m ()
,而不是ReaderT MyContext (StateT MyState (ErrorT MyError IO)))
为了简单起见,让我们忘记State
和Error
图层。
我开始编写所有回调记录,存储在MyContext
内,如:
data MyContext = MyContext { _callbacks :: Callbacks {- etc -} }
-- In this example, 2 callbacks only
data Callbacks = Callbacks {
_callback1 :: IORef (m ()),
_callback2 :: IORef (m ())}
主要问题是:在哪里放置m
的类型类约束?我尝试了以下内容,但没有编译:
我以为我可以使用Callbacks
参数化m
,例如:
data (MonadIO m, MonadReader (MyContext m) m) => Callbacks m = Callbacks {
_callback1 :: IORef (m ()),
_callback2 :: IORef (m ())}
由于Callbacks
是MyContext
的一部分,后者也必须进行参数化,并导致无限类型问题(MonadReader (MyContext m) m
)。
然后我考虑使用存在量词:
data Callbacks = forall m . (MonadIO m, MonadReader MyContext m) => Callbacks {
_callback1 :: IORef (m ()),
_callback2 :: IORef (m ())}
在我编写在Callbacks
中注册新回调的实际代码之前,似乎工作正常:
register :: (MonadIO m, MonadReader MyContext m) => m () -> m ()
register f = do
(Callbacks { _callback1 = ref1 }) <- asks _callbacks -- Note the necessary use of pattern matching
liftIO $ modifyIORef ref1 (const f)
但是我收到了以下错误(在此简化):
Could not deduce (m ~ m1)
from the context (MonadIO m, MonadReader MyContext m)
bound by the type signature for
register :: (MonadIO m, MonadReader MyContext m) => m () -> m ()
or from (MonadIO m1, MonadReader MyContext m1)
bound by a pattern with constructor
Callbacks :: forall (m :: * -> *).
(MonadIO m, MonadReader MyContext m) =>
IORef (m ())
-> IORef (m ())
-> Callbacks,
Expected type: m1 ()
Actual type: m ()
我无法找到解决方法。
如果有人能够启发我,我将非常感激。什么是 设计这个的好方法,如果有的话?
提前感谢您的意见。
[编辑] 据我所知ysdx的答案,我尝试使用m
参数化我的数据类型而不强加任何类型类约束,但后来我无法生成{ {1}} Callbacks
的一个实例;写这样的东西:
Data.Default
...导致GHC抱怨:
instance (MonadIO m, MonadReader (MyContext m) m) => Default (Callbacks m) where
def = Callbacks {
_callback1 = {- something that makes explicit use of the Reader layer -},
_callback2 = return ()}
它建议使用UndecidableInstances,但我听说这是一件非常糟糕的事情,虽然我不知道为什么。这是否意味着我必须放弃使用Variable occurs more often in a constraint than in the instance head
in the constraint: MonadReader (MyContext m) m
?
答案 0 :(得分:6)
简单的适应(使事情编译):
data MyContext m = MyContext { _callbacks :: Callbacks m }
data Callbacks m = Callbacks {
_callback1 :: IORef (m ()),
_callback2 :: IORef (m ())}
-- Needs FlexibleContexts:
register :: (MonadIO m, MonadReader (MyContext m) m) => m () -> m ()
register f = do
(Callbacks { _callback1 = ref1 }) <- asks _callbacks
liftIO $ modifyIORef ref1 (const f)
但是需要-XFlexibleContexts。
你真的需要IORef吗?为什么不使用简单的状态monad?
import Control.Monad.State
import Control.Monad.Reader.Class
import Control.Monad.Trans
data Callbacks m = Callbacks {
_callback1 :: m (),
_callback2 :: m ()
}
-- Create a "new" MonadTransformer layer (specialization of StateT):
class Monad m => MonadCallback m where
getCallbacks :: m (Callbacks m)
setCallbacks :: Callbacks m -> m ()
newtype CallbackT m a = CallbackT (StateT (Callbacks (CallbackT m) ) m a)
unwrap (CallbackT x) = x
instance Monad m => Monad (CallbackT m) where
CallbackT x >>= f = CallbackT (x >>= f')
where f' x = unwrap $ f x
return a = CallbackT $ return a
instance Monad m => MonadCallback (CallbackT m) where
getCallbacks = CallbackT $ get
setCallbacks c = CallbackT $ put c
instance MonadIO m => MonadIO (CallbackT m) where
liftIO m = CallbackT $ liftIO m
instance MonadTrans (CallbackT) where
lift m = CallbackT $ lift m
-- TODO, add other instances
-- Helpers:
getCallback1 = do
c <- getCallbacks
return $ _callback1 c
-- This is you "register" function:
setCallback1 :: (Monad m, MonadCallback m) => m () -> m ()
setCallback1 f = do
callbacks <- getCallbacks
setCallbacks $ callbacks { _callback1 = f }
-- Test:
test :: CallbackT IO ()
test = do
c <- getCallbacks
_callback1 c
_callback2 c
main = runCallbackT test s
where s = Callbacks { _callback1 = lift $ print "a" (), _callback2 = lift $ print "b" }
即使没有MonadIO,此代码也能正常运行。
定义“默认”似乎工作正常:
instance (MonadIO m, MonadCallback m) => Default (Callbacks m) where
def = Callbacks {
_callback1 = getCallbacks >>= \c -> setCallbacks $ c { _callback2 = _callback1 c },
_callback2 = return ()}