ghci是否适用特殊情况?

问题描述 投票:9回答:1

在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)不打印任何内容。

haskell monads ghci applicative
1个回答
12
投票
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实例)。] >

© www.soinside.com 2019 - 2024. All rights reserved.