考虑以下定义:
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> [Int]
现在,我该怎么说"every Foo
也是一个Bar
, with bar
默认定义为bar x = [foo x]
"在哈斯克尔?
(无论我尝试什么,编译器都会给我“非法实例声明” or “约束不小于实例头”)
顺便说一句,我可以定义我的Foo
and Bar
如果这有帮助的话,可以以其他方式上课。
class Foo a where
foo :: a -> Int
-- 'a' belongs to 'Bar' only if it belongs to 'Foo' also
class Foo a => Bar a where
bar :: a -> [Int]
bar x = [foo x] -- yes, you can specify default implementation
instance Foo Char where
foo _ = 0
-- instance with default 'bar' implementation
instance Bar Char
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)