因此,我最近提出了一个很好的想法,希望在严格的和懒惰的State转换器模块之间共享代码:
{-# LANGUAGE FlexibleInstances, DataKinds, KindSignatures #-}
module State where
data Strictness = Strict | Lazy
newtype State (t :: Strictness) s a = State (s -> (s, a))
returnState :: a -> State t s a
returnState x = State $ \s -> (s, x)
instance Monad (State Lazy s) where
return = returnState
State ma >>= amb = State $ \s -> case ma s of
~(s', x) -> runState (amb x) s'
instance Monad (State Strict s) where
return = returnState
State ma >>= amb = State $ \s -> case ma s of
(s', x) -> runState (amb x) s'
get :: State t s s
get = State $ \s -> (s, s)
put :: s -> State t s ()
put s = State $ \_ -> (s, ())您可以看到,get和put在严格类型和惰性类型上都不存在任何复制--没有类型类实例,也没有任何东西。然而,尽管我介绍了Strictness的这两种可能的情况,但总的来说,我没有State t s a的Monad实例:
-- from http://blog.melding-monads.com/2009/12/30/fun-with-the-lazy-state-monad/
pro :: State t [Bool] ()
pro = do
pro
s <- get
put (True : s)
-- No instance for (Monad (State t [Bool])) arising from a do statement尽管需要FlexibleContexts,但以下内容工作正常
pro :: (Monad (State t [Bool])) => State t [Bool] ()
-- otherwise as before然后,我可以在Lazy或Strict实例化Lazy或Strict,运行结果并得到我期望的结果。但为什么我要给出这样的背景呢?这是一个概念上的限制,还是一个实际的限制?是否有什么原因让我忽略了为什么Monad (State t s a)不存在,或者说还没有办法说服GHC呢?
(旁白:使用上下文Monad (State t s)不起作用:
Could not deduce (Monad (State t [Bool])) arising from a do statementfrom the context (Monad (State t s))
这更让我困惑。前者肯定是可以从后者还原的吗?)
发布于 2013-01-11 04:34:29
这是一个限制,但有一个很好的理由:如果它不能这样工作,那么预期的语义会是什么呢?
runState :: State t s a -> s -> (s,a)
runState (State f) s = f s
example :: s -> a
example = snd $ runState ((State undefined) >> return 1) ()嗯,可能是
example = snd $ runState ((State undefined) >>= \_ -> return 1) ()
= snd $ runState (State $ \s -> case undefined s of (s',_) -> (s',1)) ()
= snd $ case undefined () of (s',_) -> (s',1)
= snd $ case undefined of (s',_) -> (s',1)
= snd undefined
= undefined也可能是
example = snd $ runState ((State undefined) >>= \_ -> return 1) ()
= snd $ runState (State $ \s -> case undefined s of ~(s',_) -> (s',1)) ()
= snd $ case undefined () of ~(s',_) -> (s',1)
= snd $ (undefined,1)
= 1这些是不一样的。一种选择是定义一个额外的类函数,如
class IsStrictness t where
bindState :: State t s a -> (a -> State t s b) -> State t s b然后定义
instance IsStrictness t => Monad (State t s) where
return = returnState
(>>=) = bindState而不是将bindState定义为IsStrictness的一部分,您可以使用单例
data SingStrictness (t :: Strictness) where
SingStrict :: SingStrictness Strict
SingLazy :: SingStrictness Lazy
class IsStrictness t where
singStrictness :: SingStrictness t
bindState :: IsStrictness t => State t s a -> (a -> State t s b) -> State t s b
bindState ma' amb' = go singStrictness ma' amb' where
go :: SingStrictness t -> State t s a -> (a -> State t s b) -> State t s b
go SingStrict ma amb = ...
go SingLazy ma amb = ...它可以使用GHC7.6中的单子基础结构来进一步增强,而不是使用自定义类和单例类型。你最终会
instance SingI t => Monad (State t s)其实也没那么可怕。习惯于约束集中有大量的SingI _。这就是它的工作方式,至少有一段时间,而且没有那么丑陋。
关于为什么State t [Bool]不能从State t s中还原:问题是State t s在顶级上下文中,这意味着s是在最外层量化的。您正在定义一个函数,它说:“对于任何t和s,Monad (State T s)我将给您.”。但是,这并不意味着“我会给你.”。可悲的是,这些普遍量化的约束在Haskell并不是那么容易。
https://stackoverflow.com/questions/14269732
复制相似问题