haskell - 当省略 "forall"时,它们真的会自动插入到语句之前吗?

标签 haskell types

很多文章和书籍都说,如果没有指定,forall 会显式添加在语句之前。例如

check :: (forall a. [a] -> Int) -> [b] -> [c] -> Bool

实际上是

check :: forall b. forall c. (forall a. [a] -> Int) -> [b] -> [c] -> Bool

我对此有一些问题,因为由于 Haskell 使用柯里化(Currying),我想最终的签名会是这样的:

check :: (forall a. [a] -> Int) -> forall b. [b] -> forall c. [c] -> Bool

为了清楚起见,添加了括号:

check :: (forall a. [a] -> Int) -> (forall b. [b] -> (forall c. [c] -> Bool))

在本例中,表达式之前带有 forall 关键字的版本似乎只是为了方便起见。

我说得对吗?

最佳答案

Haskell 的好处是,您实际上可以通过将 -ddump-simpl 传递给编译器来查看具有显式量词的中间语言。正如 Tarmil 指出的,在 System Fc 中重新排列该函数中的外部全称量词在语义上是相同的。

-- surface language
check :: (forall a. [a] -> Int) -> [b] -> [c] -> Bool
check = undefined

app1 = check undefined
app2 = check undefined undefined
app3 = check undefined undefined undefined

翻译为:

-- core language
check :: forall b c. (forall a. [a] -> Int) -> [b] -> [c] -> Bool
check = \ (@ b) (@ c) -> (undefined)

app1 :: forall b c. [b] -> [c] -> Bool
app1 = \ (@ b) (@ c) -> check (\ (@ a) -> undefined)

app2 :: forall c. [c] -> Bool
app2 = \ (@ c) -> check (\ (@ a) -> undefined) (undefined)

app3 :: Bool
app3 = check (\ (@ a) -> undefined) (undefined) (undefined)

关于haskell - 当省略 "forall"时,它们真的会自动插入到语句之前吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147937/

相关文章:

haskell - 类型模式的名称 : R a b = Q (a -> (R a b, b))

android - Android Studio 中不支持的类型 'add-resource'

groovy - 在 groovy 的 repl 中使用 def 的奇怪行为 (groovysh)

mysql - MySQL中的zerofill有什么好处?

haskell - 有条件地建立一个列表

haskell - 共享部分应用功能

haskell - Haskell 数据类型的非均匀分布

haskell - 如何优化大型 IntMap 生成?

java - 如何请求输入直到收到 2 个整数?

多种数据类型的对象