使用修改和放置时,String、List 的 MonadState 问题
MonadState problem with String, List when using modify and put
我正在尝试使用 MonadState monad,尤其是 'modify' 函数。
当我尝试时
f :: (MonadState s m, Num s) => m ()
f = put 1
它没有任何问题,但是当我尝试将 State 设置为 String、Char 或 List 时,出现此错误:
• Non type-variable argument in the constraint: MonadState Char m
(Use FlexibleContexts to permit this)
• When checking the inferred type
a :: forall (m :: * -> *). MonadState Char m => m ()
同样的情况发生在:
b = modify (1:)
• Non type-variable argument in the constraint: MonadState [a] m
(Use FlexibleContexts to permit this)
• When checking the inferred type
b :: forall a (m :: * -> *). (MonadState [a] m, Num a) => m ()
感谢您的帮助。
如错误所示,您需要在 Haskell 中启用 FlexibleContexts
扩展到 运行。您可以通过在文件顶部添加语言编译指示来完成此操作:
{-# LANGUAGE <b>FlexibleContexts</b> #-}
b = modify (1:)
或者如果您使用 ghci
,您可以使用 -XFlexibleContexts
标志启用此扩展:
$ ghci <b>-XFlexibleContexts</b>
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
Prelude> import Control.Monad.State.Class
Prelude Control.Monad.State.Class> b = modify (1:)
Prelude Control.Monad.State.Class>
我正在尝试使用 MonadState monad,尤其是 'modify' 函数。
当我尝试时
f :: (MonadState s m, Num s) => m ()
f = put 1
它没有任何问题,但是当我尝试将 State 设置为 String、Char 或 List 时,出现此错误:
• Non type-variable argument in the constraint: MonadState Char m
(Use FlexibleContexts to permit this)
• When checking the inferred type
a :: forall (m :: * -> *). MonadState Char m => m ()
同样的情况发生在:
b = modify (1:)
• Non type-variable argument in the constraint: MonadState [a] m
(Use FlexibleContexts to permit this)
• When checking the inferred type
b :: forall a (m :: * -> *). (MonadState [a] m, Num a) => m ()
感谢您的帮助。
如错误所示,您需要在 Haskell 中启用 FlexibleContexts
扩展到 运行。您可以通过在文件顶部添加语言编译指示来完成此操作:
{-# LANGUAGE <b>FlexibleContexts</b> #-}
b = modify (1:)
或者如果您使用 ghci
,您可以使用 -XFlexibleContexts
标志启用此扩展:
$ ghci <b>-XFlexibleContexts</b>
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
Prelude> import Control.Monad.State.Class
Prelude Control.Monad.State.Class> b = modify (1:)
Prelude Control.Monad.State.Class>