我正在努力理解与exists
Haskell 类型系统相关的关键字。据我所知,默认情况下Haskell中没有这样的关键字,但是:
- 在像这样的声明中有添加它们的扩展
data Accum a = exists s. MkAccum s (a -> s -> s) (s -> a)
- 我看过一篇关于它们的论文,并且(如果我没记错的话)它指出
exists
关键字对于类型系统来说是不必要的,因为它可以被概括为forall
但我什至无法理解是什么exists
意思。
当我说 时forall a . a -> Int
,它意味着(在我的理解中,我猜是不正确的)“对于每个 (type) a
,都有一个类型的函数a -> Int
”:
myF1 :: forall a . a -> Int
myF1 _ = 123
-- okay, that function (`a -> Int`) does exist for any `a`
-- because we have just defined it
当我说exists a . a -> Int
时,它甚至意味着什么?“至少有一种类型a
具有某种类型的功能a -> Int
”?为什么要写这样的声明?目的是什么?语义?编译器行为?
myF2 :: exists a . a -> Int
myF2 _ = 123
-- okay, there is at least one type `a` for which there is such function
-- because, in fact, we have just defined it for any type
-- and there is at least one type...
-- so these two lines are equivalent to the two lines above
请注意,它并不是一个可以编译的真实代码,只是我想象的一个例子,然后我听说了这些量词。
PS 我不是 Haskell 的新手(可能像二年级学生),但我缺乏这些东西的数学基础。