约束类型的同义词

时间:2014-08-19 08:33:41

标签: haskell types constraints synonym

我知道我可以为类型创建同义词,例如:

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是否支持约束类型的同义词?

1 个答案:

答案 0 :(得分:4)

您可以查看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页面。)但是,AFAIK,没有达到你想要的。