消失的应用?

发布于 2025-01-18 22:34:00 字数 455 浏览 2 评论 0原文

在未指定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2025-01-25 22:34:00

这是一个简短讨论的GHCI特殊性,在这里。当表达式以GHCI提示输入并评估时,如果可以用io a统一其类型,则将其作为IO操作执行,并使用返回类型a,并且将显示操作的返回值(如果是(),并且它具有show> show实例)。

事实证明,it的相应值将设置为IO操作的返回值,而不是IO操作本身。我想这个想法是,如果您要写:

> getLine
I typed this!    <-- what I typed
"I typed this!"  <-- the return value displayed

您想要it给出返回的值,而不是重新运行IO操作:

> it
"I typed this!"
> :t it
it :: String

具体来说,在您的情况下,是表达式的类型:

> pure 5 :: Applicative t => t Integer

可以用io Integer统一,也是如此。运行IO操作,返回值是5,这既变成it的输出和值。

请注意,如果您编写一个无法与IO统一的表达式:

> pure 5 :: (Applicative t, Foldable t) => t Integer
[5]

此处,因为io没有可折叠>折叠>实例,类型不能与<<<代码> IO ,而GHCI的“扩展默认设置”规则用于将类型分配给未指定的类型变量。这些扩展规则包括使用列表类型[]对未知类型的类型* - &gt; *,所以我们得到纯5 :: [Integer]。如果我们首先添加可折叠io实例,则与IO统一再次起作用,并且我们得到您已经观察到的行为:

> instance Foldable IO   -- this works, despite the warning

<interactive>:11:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        either ‘foldMap’ or ‘foldr’
    • In the instance declaration for ‘Foldable IO’
> pure 5 :: (Applicative m, Foldable m) => m Integer
5

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 type a, and the return value of the action will be displayed (if it is other than () and if it has a Show 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:

> getLine
I typed this!    <-- what I typed
"I typed this!"  <-- the return value displayed

you'd want it to give the returned value, not re-run the IO action:

> it
"I typed this!"
> :t it
it :: String

Specifically, in your case, the type of the expression:

> pure 5 :: Applicative t => t Integer

can be unified with IO Integer, and so it is. The IO action is run, the return value is 5, and that becomes both the output and the value of it.

Note what happens if you write an expression that can't be unified with IO:

> pure 5 :: (Applicative t, Foldable t) => t Integer
[5]

Here, because IO has no Foldable instance in scope, the type can't unify with IO, 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 get pure 5 :: [Integer]. If we first add a Foldable IO instance, then unification with IO works again, and we get the behavior you already observed:

> instance Foldable IO   -- this works, despite the warning

<interactive>:11:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        either ‘foldMap’ or ‘foldr’
    • In the instance declaration for ‘Foldable IO’
> pure 5 :: (Applicative m, Foldable m) => m Integer
5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文