哈斯克尔($)是一个魔术师? [英] Haskell's ($) is a magic operator?

查看:120
本文介绍了哈斯克尔($)是一个魔术师?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下功能:

  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?

解决方案

是的,围绕($)来处理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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆