haskell - 约束类型的同义词

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

最佳答案

您可以查看 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/

相关文章:

string - 戈兰 : how to sum an array of integers as strings and numbers

c - 从数据类型推导出格式说明符?

mysql - 如何将自动递增的主键也用作外键?

ios - iPhone 6 上的模拟器和物理设备之间的差异

haskell - 是否可以通过另一个类型构造函数派生 Traversable 实例?

haskell - 将 GADT 与 DataKinds 用于函数中的类型级数据构造函数约束

linux - haskell cabal : Mysterious missing or recursive dependencies

haskell - 在 Haskell 中计算近似值的生成器、选择器模式

javascript - 如何将 int 转换为 float?

Swift:tableView reloadSections 后不满足约束(自动布局)