我很难理解Haskell类型系统中的exists
关键字。据我所知,Haskell默认情况下没有这样的关键字,但是:
- 有一些扩展可以添加它们,在声明中像这样:
data Accum a = exists s. MkAccum s (a -> s -> s) (s -> a)
- 我看过一篇关于它们的论文,(如果我没记错的话)它指出
exists
关键字在类型系统中是不必要的,因为它可以被forall
广义化。
但我甚至不理解exists
是什么意思。
当我说forall a . a -> Int
时,它的意思是(在我的理解中,可能是错误的):“对于每个(类型)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
请注意,这并不是一个真正可编译的代码,只是一个例子,展示了我听到这些量化器时的想象。
另外,我在Haskell方面并不是完全的新手(也许像二年级学生那样),但我的数学基础还欠缺一些。