假设我有Heap a
类型,其中Heap
是种类* -> *
的类型构造函数。堆上的许多基本操作要求a
类型是Ord
类型类的实例。带类型的类型类型实例的类型参数约束* - > *
data Heap a = ...
findMin :: Ord a => Heap a -> a
deleteMin :: Ord a => Heap a -> Heap a
我想尽快a
类型参数Ord
类型的类的实例声明我Heap
类型Foldable
型类的一个实例(它会很容易通过findMin
和deleteMin
函数来表示)。
这种关系可当我们需要的那种*
类型类型类,处理像Show
被easely表示:
instance Show a => Show (Heap a) where
show h = ...
但我有问题,与申报的Foldable
:
instance Foldable Heap where
-- Ouch, there is no `a` type parameter to put the constraint on!
foldr f z h = ...
是否可以在这种实例声明中对a
类型参数设置约束?
看看[这个东西](http://blog.omega-prime.co.uk/?p=127)。他们正在做一些与monads类似的事情。 – phg
非常感谢您的链接,'ConstraintKind'是_really_有趣的东西! –
顺便说一句,'findMin'真的需要一个'Ord'实例吗? – yairchu