所描述的类型家庭扩展不起作用 [英] Type Families extension does not work as described
问题描述
在类型族的Haskell wiki页面中,有以下例子:
type family F a :: *
type instance F [Int] = Int - OK!
类型实例F String = Char - OK!
类型实例F(F a)= a - WRONG:类型参数提到一个类型系列
类型实例F(a。(a,b))= b - WRONG:出现类型在类型参数中
类型实例F Float = forall aa - WRONG:右手边可能不是所有类型
类型实例其中 - OK!
F(Maybe Int)= Int
F(Maybe Bool)= Bool
F(也许a)= String
类型实例其中 - WRONG:与早期实例冲突以下)
F Int =浮动
F a = [a]
类型系列G ab :: * - > *
类型实例G Int =(,) - WRONG:必须是两个类型参数
类型实例G Int Char Float =双重 - WRONG:必须是两个类型参数
$这证明类型实例,其中
在此扩展名下是有效的语法。但是下面的代码在GHC 7.4.2中没有为我编译:
{ - #LANGUAGE TypeFamilies# - }
类型家庭F a :: *
类型实例其中
F(Maybe Int)= Int
F(也许Bool)= Bool
F(也许a) =字符串
错误信息是:
test.hs:4:15:解析输入错误'where'
由于这是一个解析错误,它看起来像不支持语法,所以我错过了必要的扩展,或者是其他东西不对?
解决方案这似乎是过早记录的情况。根据此博客文章,此语法是最近添加到GHC HEAD中的功能的一部分,但它尚未在GHC的任何发布版本中有效。
On the Haskell wiki page for Type Families, there is the following list of examples:
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
This demonstrates that type instance where
is valid syntax under this extension. However the following code does not compile for me with 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
The error message is:
test.hs:4:15: parse error on input `where'
Since this is a parsing error, it looks like that syntax is not supported, so am I missing a requisite extension, or is something else amiss?
解决方案 This appears to be a case of premature documentation. According to this blog post, this syntax is part of a feature recently added to GHC HEAD, but it's not yet valid in any released version of GHC.
这篇关于所描述的类型家庭扩展不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!