module PartialOrder where import Prelude import Maybe class Eq a => PartialOrd a where lt, le, gt, ge :: a -> a -> Maybe Bool le x y | (lt x y == Just True) || x == y = Just True | otherwise = Nothing gt x y = lt y x ge x y = le y x isMinimal :: PartialOrd a => a -> [a] -> Bool isMinimal x = null . ( mapMaybe (gt x) ) upperStar :: PartialOrd a => a -> [a] -> [a] upperStar x xs = [ y | y <- xs, lt x y == Just True ] maxChainFromElt :: PartialOrd a => a -> [a] -> [a] maxChainFromElt x xs = x : (maxChain ( upperStar x xs )) maxChain :: PartialOrd a => [a] -> [a] maxChain xs = foldr (\x y -> if length x > length y then x else y) [ ] [ maxChainFromElt x xs | x <- xs, isMinimal x xs ] instance PartialOrd Int where lt x y | (even x) && (even y) && (x < y) = Just True | (odd x) && (odd y) && (x < y) = Just True | otherwise = Nothing {-- tests PartialOrder> maxChain ([3, 4, 6, 7, 7, 9, 10, 0, -1, 2] :: [Int]) [0,2,4,6,10] PartialOrder> maxChain ([3, 4, 5] :: [Int]) [3,5] PartialOrder> maxChain ([3, 4] :: [Int]) [4] PartialOrder> maxChain ([ ]::[Int]) [] instance PartialOrd Int where lt x y | (x < y) && (x > 0) = Just True | otherwise = Nothing -}