考虑以下代码:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Study where
class C a where
type T a = r | r -> a
pred :: T a -> Bool
pred _ = True
我希望有一个更有意义的默认定义pred
,如下所示:
class C' a where
...
pred' = not . null
(我想默认 T' a
= [a]
。)
有办法吗?
答案 0 :(得分:2)
Prelude> :{
Prelude| class C a where
Prelude| type T a = r | r -> a
Prelude| pred :: T a -> Bool
Prelude| default pred :: (T a ~ [a]) => T a -> Bool
Prelude| pred = not . null
Prelude| :}