在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 ()
和pure (+1)
不打印任何内容。
v <- return (1 :: Integer)
print v
return v
正在执行(魔术变量it
绑定到返回的v
)。对于pure ()
,适用特殊情况,因为认为()
不感兴趣,因此仅执行return ()
并将it
绑定到()
,对于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
具有作用域内函数的Show
实例,它会被打印出来(不是它提供的信息),然后就可以使用该功能(当然,后者独立于作用域内的Show
实例)。] >