我知道我可以为类型创建同义词,例如:
type ListToArray a e = [e] -> a Int e
我还读到约束类型仍然是一种类型,所以我想我也可以为其创建一个同义词:
type (Data.Array.IArray.IArray a e) => ListToArray a e = [e] -> a Int e
但是 GHC 提示:
parse error on input '=>'
GHC 是否支持约束类型的同义词?
最佳答案
您可以查看 Haskell 2010 Report ,关于类型同义词的部分指出类型同义词的语法是:
type simpletype = type
simpletype → tycon tyvar1 … tyvark (k ≥ 0)
换句话说:不,您不能将类约束放在 type
声明中的 =
左侧。
另请注意,即使在此类声明中的 =
右侧,也不能有类约束。 type
的定义是:
type → btype [-> type] (function type)
btype → [btype] atype (type application)
atype → gtycon
| tyvar
| ( type1 , … , typek ) (tuple type, k ≥ 2)
| [ type ] (list type)
| ( type ) (parenthesised constructor)
gtycon → qtycon
| () (unit type)
| [] (list constructor)
| (->) (function constructor)
| (,{,}) (tupling constructors)
因此,type
非终结符指的是没有上下文的类型。换句话说:不,你不能在类型同义词中拥有上下文,至少在一般情况下不能。
GHC 对 Haskell 类型系统有很多扩展,可以在某些情况下解除上述一些限制(请参阅 this 页。)但是,据我所知,没有一个能够完全实现您想要的。
关于haskell - 约束类型的同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378912/