In ghci:
λ> :t (pure 1)
(pure 1) :: (Applicative f, Num a) => f a
λ> show (pure 1)
<interactive>:1:1:
No instance for (Show (f0 a0))
arising from a use of `show'
Possible fix: add an instance declaration for (Show (f0 a0))
In the expression: show (pure 1)
In an equation for `it': it = show (pure 1)
λ> pure 1
1
这是否意味着ghci执行Applicative并显示结果,就像IO
?
注意pure ()
and pure (+1)
不要打印任何东西。
如果您使用,您会得到相同的行为return
代替pure
。要了解要做什么,ghci 必须为给定表达式选择一种类型。 ghci 的默认规则是在没有其他约束的情况下,它选择IO
for an Applicative
or Monad
实例。因此它解释了pure 1
作为类型的表达式IO Integer
。类型表达式IO a
如果为 1,则执行在提示符下输入的内容并打印其结果。a
has a Show
实例和2.a
is not ()
。从而进入pure 1
在提示结果中
v <- return (1 :: Integer)
print v
return v
正在执行(以及魔术变量it
绑定到返回的v
). For pure ()
,特殊情况适用,因为()
被认为是无趣的,因此只有return ()
被执行并且it
势必()
, for pure (+1)
,返回一个函数,没有Show
范围内函数的实例,因此不会打印任何内容。然而,
Prelude Control.Applicative> :m +Text.Show.Functions
Prelude Control.Applicative Text.Show.Functions> pure (+1)
<function>
it :: Integer -> Integer
Prelude Control.Applicative Text.Show.Functions> it 3
4
it :: Integer
with a Show
对于范围内的函数的实例,它会被打印(并不是说它提供了信息),然后可以使用该函数(后者独立于Show
当然,实例在范围内)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)