Advertisement
mudri

Transitive comparisons

Apr 5th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. infixl 4 ==., <., <=., >., >=., /=.
  2. infixl 4 .==., .<., .<=., .>., .>=., ./=.
  3. infixl 4 .==, .<, .<=, .>, .>=, ./=
  4.  
  5. data ACompResult a = TrueCond a | FalseCond
  6.  
  7. (==.) :: Eq a => a -> a -> ACompResult a
  8. (<.) :: Ord a => a -> a -> ACompResult a
  9. (<=.) :: Ord a => a -> a -> ACompResult a
  10. (>.) :: Ord a => a -> a -> ACompResult a
  11. (>=.) :: Ord a => a -> a -> ACompResult a
  12. (/=.) :: Eq a => a -> a -> ACompResult a
  13. makeFirstComparison :: (a -> a -> Bool) -> a -> a -> ACompResult a
  14.  
  15. makeFirstComparison f x y =
  16.     if f x y
  17.         then TrueCond y  -- Comparison successful, carry the right argument on
  18.         else FalseCond   -- Comparison failed
  19. (==.) = makeFirstComparison (==)
  20. (<.) = makeFirstComparison (<)
  21. (<=.) = makeFirstComparison (<=)
  22. (>.) = makeFirstComparison (>)
  23. (>=.) = makeFirstComparison (>=)
  24. (/=.) = makeFirstComparison (/=)
  25.  
  26. (.==.) :: Eq a => ACompResult a -> a -> ACompResult a
  27. (.<.) :: Ord a => ACompResult a -> a -> ACompResult a
  28. (.<=.) :: Ord a => ACompResult a -> a -> ACompResult a
  29. (.>.) :: Ord a => ACompResult a -> a -> ACompResult a
  30. (.>=.) :: Ord a => ACompResult a -> a -> ACompResult a
  31. (./=.) :: Eq a => ACompResult a -> a -> ACompResult a
  32. makeMiddleComparison :: (a -> a -> Bool) -> ACompResult a -> a -> ACompResult a
  33.  
  34. makeMiddleComparison _ FalseCond _ = FalseCond  -- Previous failure
  35. makeMiddleComparison f (TrueCond x) y =
  36.     if f x y
  37.         then TrueCond y  -- Comparison successful, carry the right argument on
  38.         else FalseCond   -- Comparison failed
  39. (.==.) = makeMiddleComparison (==)
  40. (.<.) = makeMiddleComparison (<)
  41. (.<=.) = makeMiddleComparison (<=)
  42. (.>.) = makeMiddleComparison (>)
  43. (.>=.) = makeMiddleComparison (>=)
  44. (./=.) = makeMiddleComparison (/=)
  45.  
  46. (.==) :: Eq a => ACompResult a -> a -> Bool
  47. (.<) :: Ord a => ACompResult a -> a -> Bool
  48. (.<=) :: Ord a => ACompResult a -> a -> Bool
  49. (.>) :: Ord a => ACompResult a -> a -> Bool
  50. (.>=) :: Ord a => ACompResult a -> a -> Bool
  51. (./=) :: Eq a => ACompResult a -> a -> Bool
  52. makeLastComparison :: (a -> a -> Bool) -> ACompResult a -> a -> Bool
  53.  
  54. makeLastComparison _ FalseCond _ = False  -- Previous failure
  55. makeLastComparison f (TrueCond x) y = f x y
  56. (.==) = makeLastComparison (==)
  57. (.<) = makeLastComparison (<)
  58. (.<=) = makeLastComparison (<=)
  59. (.>) = makeLastComparison (>)
  60. (.>=) = makeLastComparison (>=)
  61. (./=) = makeLastComparison (/=)
  62.  
  63. -- Example:
  64. -- Haskell> 1 <. 3 .==. 3 .< 6
  65. -- True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement