如何创建一个新的泛型类型并在haskell中重新定义==?

时间:2018-05-21 14:46:51

标签: haskell

我有下一个haskell代码成功运行:

type Relation a = [[a]]

isAEquivalentRelation   ::  Eq a  => Relation a -> Bool
isAEquivalentRelation x   =   isASimetricRelation x && isAReflexiveRelation x && isATransitiveRelation x

isASimetricRelation   ::  Eq a  =>  Relation a -> Bool
isASimetricRelation []   =   True
isASimetricRelation (x:xs)   =   (elem (reverse x) (x:xs)) && (isASimetricRelation  (filter (/= y) xs)) where y = (reverse x)

isAReflexiveRelation   ::  Eq a  =>  Relation a -> Bool 
isAReflexiveRelation []   =   True
isAReflexiveRelation x   =   isAReflexiveRelationRec (filter (\ a -> a !! 0 == a !! 1) x) x

isAReflexiveRelationRec   ::   Eq a   =>  Relation a -> Relation a -> Bool 
isAReflexiveRelationRec [] _  =   False
isAReflexiveRelationRec _ []  =   True
isAReflexiveRelationRec pr (x:xs)  = or ( map (== [x!!0,x!!0]) pr) && or ( map (== [x!!1,x!!1]) pr) && isAReflexiveRelationRec pr xs

isATransitiveRelation   ::   Eq a   =>  Relation a -> Bool 
isATransitiveRelation []   =   True
isATransitiveRelation x   =   isATransitiveRelationRec x x

isATransitiveRelationRec   ::   Eq a   => Relation a -> Relation a -> Bool 
isATransitiveRelationRec _ []   =   True
isATransitiveRelationRec original (x:xs)   =   and ( map (\ a -> elem a original) (map (\ a -> [x !! 0, a !! 1]) (filter (\ a -> x !! 1 == a !! 0) original)) ) && isATransitiveRelationRec original xs

areEqual   ::   Eq a  =>  Relation a -> Relation a -> Bool
areEqual x y  =   subset x y && subset y x 

subset   ::   Eq a  =>  Relation a -> Relation a -> Bool
subset [] _ = True
subset _ [] = False
subset x y = and (map (\ a -> elem a y) x)

问题是我需要使用我实现的areEqual方法重新定义Relation类型的==但是我无法实现这一点。我已经尝试了很多关于类,实例和数据的东西但是我被困了,我如何声明一个通用的Relation类型并重新定义operator ==以匹配areEqual方法?

0 个答案:

没有答案