Advertisement
Guest User

Untitled

a guest
May 24th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. module Filter where
  2.  
  3. -- | F(ilter) a, the Filter algebra
  4. -- Literal := in | eq | lt | gt | false | true
  5. -- Term t := Literal | And t t | Or t t | Not t
  6. data F a
  7. -- Expression literals
  8. = InF [a]
  9. | EqF a
  10. | GtF a
  11. | LtF a
  12. | FFalse
  13. | FTrue
  14.  
  15. -- Expression trees
  16. | AndF (F a) (F a)
  17. | OrF (F a) (F a)
  18. | NotF (F a)
  19. deriving Show
  20.  
  21. instance Functor F where
  22. fmap f (InF xs) = InF (fmap f xs)
  23. fmap f (EqF x) = EqF (f x)
  24. fmap f (GtF x) = GtF (f x)
  25. fmap f (LtF x) = LtF (f x)
  26. fmap _ FFalse = FFalse
  27. fmap _ FTrue = FTrue
  28. fmap f (AndF x1 x2) = AndF (fmap f x1) (fmap f x2)
  29. fmap f (OrF x1 x2) = OrF (fmap f x1) (fmap f x2)
  30. fmap f (NotF x) = NotF (fmap f x)
  31.  
  32. -- | Laws
  33. -- 1. (<||>, 0) form a Monoid
  34. -- 2. (<&&>) forms a Semigroup
  35. -- 3. (x <&&> y) <&&> z = x <&&> (y <&&> z) , associative <&&>
  36. -- 4. (x <|> y) <&&> z = (x <&&> z) <||> (y <&&> z) , <||> distributes over <&&>
  37. -- 5. zero <&&> x = zero
  38. class Semiring a where
  39. zero :: a -- zero, 0
  40. one :: a -- one, 1
  41. (<||>) :: a -> a -> a -- sum, (+)
  42. (<&&>) :: a -> a -> a -- product, (*)
  43.  
  44. instance Semiring (F a) where
  45. zero = FFalse
  46. one = FTrue
  47. (<||>) = OrF
  48. (<&&>) = AndF
  49.  
  50. -- |
  51. -- pure evaluator for Filter language
  52. -- could `compile` filters to other languages,
  53. -- e.g., SQL filters in WHERE clause
  54. eval :: (Ord a, Eq a) => F a -> (a -> Bool)
  55. eval (InF x) = (`elem` x)
  56. eval (EqF x) = (== x)
  57. eval (GtF x) = (> x)
  58. eval (LtF x) = (< x)
  59. eval FFalse = const False
  60. eval FTrue = const True
  61. eval (NotF x) = not . eval x
  62. eval (AndF l r) = \x -> eval l x && eval r x
  63. eval (OrF l r) = \x -> eval l x || eval r x
  64.  
  65. -- this compiles; give it a try!
  66. test :: IO ()
  67. test = print . filter (eval cond) $ xs
  68. where cond = (InF [30..40] <||> InF [1..10] <||> zero)
  69. <&&> fmap (+1) (GtF 33)
  70. <&&> LtF 38
  71. <&&> NotF (EqF 35)
  72. <&&> one
  73. xs = [1..100] :: [Int]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement