哈斯克尔($)是一个魔术师? [英] Haskell's ($) is a magic operator?
问题描述
infixr 0< |
$ b $ { - #INLINE(< |)# - }
(< |)::(a - > b) - > a - > b
f <| x = f x
foo :: a - > (全部b.b→b)→> a
foo xf = fx
以下不会输入check:
ghci> foo 3 <| id
无法匹配期望的类型`forall b。 b - > b'
,实际类型为'a0 - > a0'
在'(< |)'的第二个参数中,即`id'
在表达式中:f 3 <| id
在'it'的等式中:it = f 3 <| id
然而, foo 3 $ id
< (< |)的定义是(据我所知)与($)的定义相同。 我几乎从基础库源中删除了定义,并将每个($)的实例都更改为(< |)。 Compiler magic?
是的,围绕($)$ < c $ c>来处理impandicative类型。这是因为每个人都期望
runST $ do
foo
bar
baz
进行类型检查,但它不能正常运行。有关更多详情,请参阅此处(搜索 runST
),这个电子邮件和电子邮件。它的缺点是在类型检查器中实际上有一个特殊的规则,专门用于($)
,这使得它能够解决一些常见的意外类型检查。
Say I have the following functions:
infixr 0 <|
{-# INLINE (<|) #-}
(<|) :: (a -> b) -> a -> b
f <| x = f x
foo :: a -> (forall b. b -> b) -> a
foo x f = f x
The following does not type check:
ghci> foo 3 <| id
Couldn't match expected type `forall b. b -> b'
with actual type `a0 -> a0'
In the second argument of `(<|)', namely `id'
In the expression: f 3 <| id
In an equation for `it': it = f 3 <| id
However, foo 3 $ id
does.
The definition of (<|) is (as far as I know) identical to the definition of ($). I pretty much ripped out the definition from the base library sources, and changed every instance of ($) to (<|). Compiler magic?
Yes, there's a small amount of compiler magic around ($)
to handle impredicative types. It was introduced because everyone expects
runST $ do
foo
bar
baz
to typecheck, but it cannot normally. For more details see here (search runST
), this email, and this email. The short of it is that there's actually a special rule in the type-checker specifically for ($)
which gives it the ability to resolve the common case of impredicative types.
这篇关于哈斯克尔($)是一个魔术师?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!