消失的应用?
在未指定 Applicative 实例的情况下,我对 pure 的行为有点困惑。在此示例中,我希望结果值在应用上下文中为数字 5:
Prelude> pure 5 :: Applicative t => t Integer
5
Prelude> :t it
it :: Integer
相反,它只是一个普通的整数。
如果指定了应用实例,如下例所示,则返回的值是应用类型,正如我所期望的:
Prelude> pure 5 :: Maybe Integer
Just 5
Prelude> :t it
it :: Maybe Integer
为什么 Applicative t
在第一个示例中似乎消失了?
似乎未指定的应用上下文被剥离以用于最终评估以打印到输出,但我想知道这的规则是什么。
I'm a bit confused about the behavior of pure
in a case where its Applicative instance is unspecified. In this example, I expect the resulting value to be the number 5 in an applicative context:
Prelude> pure 5 :: Applicative t => t Integer
5
Prelude> :t it
it :: Integer
Instead, it's just a plain Integer.
If the applicative instance is specified, like in the following example, the returned value is in an applicative type, as I expect:
Prelude> pure 5 :: Maybe Integer
Just 5
Prelude> :t it
it :: Maybe Integer
Why does the Applicative t
seem to disappear in the first example?
It seems that the unspecified applicative context is being stripped off for the final evaluation to print to the output, but I'd like to know what the rules are for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个简短讨论的GHCI特殊性,在这里。当表达式以GHCI提示输入并评估时,如果可以用
io a
统一其类型,则将其作为IO操作执行,并使用返回类型a
,并且将显示操作的返回值(如果是()
,并且它具有show> show
实例)。事实证明,
it
的相应值将设置为IO操作的返回值,而不是IO操作本身。我想这个想法是,如果您要写:您想要
it
给出返回的值,而不是重新运行IO操作:具体来说,在您的情况下,是表达式的类型:
可以用
io Integer
统一,也是如此。运行IO操作,返回值是5
,这既变成it
的输出和值。请注意,如果您编写一个无法与IO统一的表达式:
此处,因为
io
没有可折叠>折叠>
实例,类型不能与<<<代码> IO ,而GHCI的“扩展默认设置”规则用于将类型分配给未指定的类型变量。这些扩展规则包括使用列表类型[]
对未知类型的类型* - &gt; *
,所以我们得到纯5 :: [Integer]
。如果我们首先添加可折叠io
实例,则与IO统一再次起作用,并且我们得到您已经观察到的行为:This is a GHCi peculiarity briefly discussed here. When an expression is entered at the GHCi prompt and evaluated, if its type can be unified with
IO a
, then it will be executed as an IO action with return typea
, and the return value of the action will be displayed (if it is other than()
and if it has aShow
instance).It turns out that the corresponding value of
it
will be set to the return value of the IO action, rather than the IO action itself. I guess the idea is that, if you were to write:you'd want
it
to give the returned value, not re-run the IO action:Specifically, in your case, the type of the expression:
can be unified with
IO Integer
, and so it is. The IO action is run, the return value is5
, and that becomes both the output and the value ofit
.Note what happens if you write an expression that can't be unified with IO:
Here, because
IO
has noFoldable
instance in scope, the type can't unify withIO
, and instead GHCi's "extended defaulting" rules for assigning types to unspecified type variables are used. These extended rules include using the list type[]
for unknown types of kind* -> *
, so we getpure 5 :: [Integer]
. If we first add aFoldable IO
instance, then unification with IO works again, and we get the behavior you already observed: