我正在尝试对类型限制为某些类型类的实例的项目列表进行编码:
{-# 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'
看来我什至没有为类型同义词定义实例......
我知道异构集合wiki 文章,但想知道为什么我的方法不起作用 - 通过将集合限制为仅包含具有某种类型类实例的类型的项目来定义类型对我来说似乎很自然。