Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 理解状态单子&x27;让我们开始吧`_Haskell - Fatal编程技术网

Haskell 理解状态单子&x27;让我们开始吧`

Haskell 理解状态单子&x27;让我们开始吧`,haskell,Haskell,鉴于: 我试图调用get s,但出现以下编译时错误: *Main> let s = state $ \x -> ("foo", x) *Main> :t s s :: MonadState s m => m [Char] 但是,它仍然没有编译: *Main> :set -XFlexibleContexts *Main>let result=get s :9:5:错误: •无法推断(MonadState s0 m0) 从上下文:(MonadState s m,Mo

鉴于:

我试图调用
get s
,但出现以下编译时错误:

*Main> let s = state $ \x -> ("foo", x)
*Main> :t s
s :: MonadState s m => m [Char]
但是,它仍然没有编译:

*Main> :set -XFlexibleContexts
*Main>let result=get s
:9:5:错误:
•无法推断(MonadState s0 m0)
从上下文:(MonadState s m,MonadState t((->)(m[Char]))
由“result”的推断类型绑定:
(单子态s m,单子态t((->)(m[Char]))=>t
时间:9:5-18
类型变量“s0”、“m0”不明确
•在“结果”推断类型的歧义检查中
要延迟歧义检查以使用站点,请启用AllowAmbigUstypes
检查推断类型时
结果:对于所有s(m::*->*)t。
(单子状态SM,单子状态t((->)(m[Char]))=>
T

请解释为什么它没有编译。

仔细查看箭头<代码>获取不接受任何参数:

*Main> let result = get s

<interactive>:9:5: error:
    • Could not deduce (MonadState s0 m0)
      from the context: (MonadState s m, MonadState t ((->) (m [Char])))
        bound by the inferred type for ‘result’:
                   (MonadState s m, MonadState t ((->) (m [Char]))) => t
        at <interactive>:9:5-18
      The type variables ‘s0’, ‘m0’ are ambiguous
    • In the ambiguity check for the inferred type for ‘result’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      When checking the inferred type
        result :: forall s (m :: * -> *) t.
                  (MonadState s m, MonadState t ((->) (m [Char]))) =>
                  t
s

get :: MonadState s m => m s
--     ^^^^^^^^^^^^^^
--     constraint, not type
我们可以将这些有状态计算与
>
相结合,因为任何
单子状态SM
也是一个
单子状态

s :: MonadState s m => m [Char]
--   ^^^^^^^^^^^^^^
--   constraint, not type
我们可以通过
运行状态查看
setAndGet
的结果:

setAndGet = s >> get

仔细看看箭头<代码>获取
不接受任何参数:

*Main> let result = get s

<interactive>:9:5: error:
    • Could not deduce (MonadState s0 m0)
      from the context: (MonadState s m, MonadState t ((->) (m [Char])))
        bound by the inferred type for ‘result’:
                   (MonadState s m, MonadState t ((->) (m [Char]))) => t
        at <interactive>:9:5-18
      The type variables ‘s0’, ‘m0’ are ambiguous
    • In the ambiguity check for the inferred type for ‘result’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      When checking the inferred type
        result :: forall s (m :: * -> *) t.
                  (MonadState s m, MonadState t ((->) (m [Char]))) =>
                  t
s

get :: MonadState s m => m s
--     ^^^^^^^^^^^^^^
--     constraint, not type
我们可以将这些有状态计算与
>
相结合,因为任何
单子状态SM
也是一个
单子状态

s :: MonadState s m => m [Char]
--   ^^^^^^^^^^^^^^
--   constraint, not type
我们可以通过
运行状态查看
setAndGet
的结果:

setAndGet = s >> get

状态单子中的状态就像一个遵循表面上的计算路径的函数。当您不需要状态时,它会隐藏状态,并让您专注于组成构成计算的(一元)函数


get
就像一口井,它可以插入那条河,让你在需要的时候把州打开。可以说,这是“你所遵循的路径的一部分”,而不是你应用于一元行为的一些外部功能

状态单子中的状态就像一个遵循表面上的计算路径的函数。当您不需要状态时,它会隐藏状态,并让您专注于组成构成计算的(一元)函数

get
就像一口井,它可以插入那条河,让你在需要的时候把州打开。可以说,这是“你所遵循的路径的一部分”,而不是你应用于一元行为的一些外部功能