为什么类型 Id a = a 不能部分应用于数据 D f = D(f())?
Why can't type Id a = a be partially applied to data D f = D (f ())?
我还按照 When (if ever) can type synonyms be partially applied? 中的建议尝试了以下使用 LiberalTypeSynonyms 扩展的方法,并在 Id
的定义中向 f
添加了明确的种类签名。我仍然遇到同样的错误。我不知道是否还有其他扩展可能有帮助。
ghci> type Id a = a
ghci> type Const a b = a
ghci> data D f = D (f ())
ghci> (((() :: Id ()) :: ()) :: Const () a)
()
ghci> D () :: D Id
<interactive>:10:9:
Type synonym ‘Id’ should have 1 argument, but has been given none
In an expression type signature: D Id
In the expression: D () :: D Id
In an equation for ‘it’: it = D () :: D Id
这让我很困惑。 D
中的 f
是 * -> *
而 Id
是 * -> *
。这还有什么?
Haskell 的类型系统不支持部分应用(也称为 不饱和)类型同义词或族。不过,data
类型和 newtypes
确实支持部分应用。
newtype D f = D (f ())
newtype Id a = Id a
d :: D Id
d = D (Id ())
newtype
s 在编译过程中被删除,所以你付出的唯一代价是语法代价。
我还按照 When (if ever) can type synonyms be partially applied? 中的建议尝试了以下使用 LiberalTypeSynonyms 扩展的方法,并在 Id
的定义中向 f
添加了明确的种类签名。我仍然遇到同样的错误。我不知道是否还有其他扩展可能有帮助。
ghci> type Id a = a
ghci> type Const a b = a
ghci> data D f = D (f ())
ghci> (((() :: Id ()) :: ()) :: Const () a)
()
ghci> D () :: D Id
<interactive>:10:9:
Type synonym ‘Id’ should have 1 argument, but has been given none
In an expression type signature: D Id
In the expression: D () :: D Id
In an equation for ‘it’: it = D () :: D Id
这让我很困惑。 D
中的 f
是 * -> *
而 Id
是 * -> *
。这还有什么?
Haskell 的类型系统不支持部分应用(也称为 不饱和)类型同义词或族。不过,data
类型和 newtypes
确实支持部分应用。
newtype D f = D (f ())
newtype Id a = Id a
d :: D Id
d = D (Id ())
newtype
s 在编译过程中被删除,所以你付出的唯一代价是语法代价。