On 类型族的 Haskell wiki 页面 http://www.haskell.org/haskellwiki/GHC/Type_families,有以下示例列表:
type family F a :: *
type instance F [Int] = Int -- OK!
type instance F String = Char -- OK!
type instance F (F a) = a -- WRONG: type parameter mentions a type family
type instance F (forall a. (a, b)) = b -- WRONG: a forall type appears in a type parameter
type instance F Float = forall a.a -- WRONG: right-hand side may not be a forall type
type instance where -- OK!
F (Maybe Int) = Int
F (Maybe Bool) = Bool
F (Maybe a) = String
type instance where -- WRONG: conflicts with earlier instances (see below)
F Int = Float
F a = [a]
type family G a b :: * -> *
type instance G Int = (,) -- WRONG: must be two type parameters
type instance G Int Char Float = Double -- WRONG: must be two type parameters
这表明type instance where
是此扩展下的有效语法。但是,以下代码无法使用 GHC 7.4.2 进行编译:
{-# LANGUAGE TypeFamilies #-}
type family F a :: *
type instance where
F (Maybe Int) = Int
F (Maybe Bool) = Bool
F (Maybe a) = String
错误信息是:
test.hs:4:15:输入“where”时解析错误
由于这是一个解析错误,看起来该语法不受支持,所以我是否缺少必要的扩展,或者是否有其他问题?
这似乎是一个过早记录的情况。根据这篇博文 http://typesandkinds.wordpress.com/2012/12/22/ordered-overlapping-type-family-instances/,此语法是最近添加到 GHC HEAD 的功能的一部分,但它在任何已发布的 GHC 版本中尚未有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)