9

Type Families 的 Haskell wiki 页面上,有以下示例列表:

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”时解析错误

由于这是一个解析错误,看起来该语法不受支持,所以我是否缺少必要的扩展,或者还有其他问题?

4

1 回答 1

13

这似乎是过早记录的情况。根据这篇博客文章,这个语法是最近添加到 GHC HEAD 的功能的一部分,但它在任何已发布的 GHC 版本中都无效。

于 2013-05-02T13:03:30.193 回答