【发布时间】:2014-07-22 16:20:14
【问题描述】:
我们可以用参数定义类型同义词,这在与实际类型一起使用时效果很好:
type MyType t = t String String
data Test a b = Test a b
f :: MyType Test
f = undefined
main = undefined
编译不会出错:
$ghc --make test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
Linking test ...
但是,当 Test 是类型同义词时,这不起作用:
type MyType t = t String String
data Test a b = Test a b
type Test' a b = Test a b
f :: MyType Test'
f = undefined
main = undefined
这给出了以下错误:
$ghc --make test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
test.hs:7:6:
Type synonym Test' should have 2 arguments, but has been given none
In the type signature for `f': f :: MyType (Test')
让我感到困惑的是Test'被应用于两个参数,那么为什么 GHC 抱怨我没有通过参数?
类型同义词不应该是完全透明的,无法与其他类型区分开来吗?
有没有办法实现预期的行为?
【问题讨论】:
-
@dbaupp 我的问题与 GHC 扩展或实例声明无关。答案可能相同的事实不暗示问题是重复的。该问题的答案也没有解决我的问题的第二部分(如何允许这种代码),我刚刚发现 is 可能使用
LiberalTypeSynonyms(即 与这些答案相比)。 -
@Cactus 这个答案和问题是关于 partial 类型的同义词应用程序。我的问题是关于使用其他类型同义词的 total 类型同义词应用程序(
MyType Test'在扩展后导致Test'的 total 应用程序)。它们是完全不同的东西,实际上答案是不同的,因为您可以使用非常简单的扩展来解决后者,而前者会使类型系统无法确定。