【发布时间】:2011-06-08 20:41:11
【问题描述】:
我正在尝试对类型限制为某些类型类的实例的项目列表进行编码:
{-# LANGUAGE RankNTypes, TypeSynonymInstances, LiberalTypeSynonyms #-}
module Test where
class Someable a where
some :: a -> String
data Some = Some String
type SomeGroup = forall a. Someable a => [a]
instance Someable Some where
some (Some v) = v
instance Someable SomeGroup where
some (x:xs) = (some x) ++ ", " ++ (some xs)
main = do
putStrLn $ show.some [Some "A", [Some "B", Some "C"]]
但是编译失败并出现错误:
Test.hs:14:10:
Illegal polymorphic or qualified type: SomeGroup
In the instance declaration for `Someable SomeGroup'
似乎我什至没有为类型同义词定义实例......
我知道heterogenous collections wiki 文章,但想知道为什么我的方法不起作用——对我来说,通过将集合限制为仅包含具有某种类型实例的类型的项目来定义类型似乎很自然类。
【问题讨论】:
-
您在这里尝试做的事情确实是有道理的,但是 Haskell 没有很好地支持它,并且通常是非惯用设计的症状。如果您对如何操作感到好奇,@hammar 将为您提供一个起点。如果您在实际代码中遇到这种情况,您最好重新考虑您的方法。
标签: haskell typeclass heterogeneous