/libs/_old/greyfold/src/Data/GreyFold/Base/SequenceRecursion.hs

http://copperbox.googlecode.com/ · Haskell · 115 lines · 58 code · 28 blank · 29 comment · 0 complexity · aeca3713fc4c6474efd697722ded01d7 MD5 · raw file

  1. {-# OPTIONS -Wall #-}
  2. --------------------------------------------------------------------------------
  3. -- |
  4. -- Module : GreyFold.Base.SequenceRecursion
  5. -- Copyright : (c) Stephen Tetley 2008
  6. -- License : BSD-style (as per the Haskell Hierarchical Libraries)
  7. --
  8. -- Maintainer : Stephen Tetley <stephen.tetley@gmail.com>
  9. -- Stability : highly unstable
  10. -- Portability : to be determined
  11. --
  12. -- (Some of) the famous recursion schemes for Data.Sequence.
  13. --
  14. --------------------------------------------------------------------------------
  15. module GreyFold.Base.SequenceRecursion where
  16. import Data.Sequence
  17. -- | Catamorphism - foldr.
  18. cata :: (a -> b -> b) -> b -> Seq a -> b
  19. cata f b se = step (viewl se) where
  20. step EmptyL = b
  21. step (a :< sa) = f a (step (viewl sa))
  22. -- | Anamorphism - unfoldr.
  23. ana :: (b -> Maybe (a,b)) -> b -> Seq a
  24. ana f b0 = step (f b0) where
  25. step Nothing = empty
  26. step (Just (a,st)) = a <| step (f st)
  27. -- | Hylomorphism.
  28. -- A hylomorphism has no dependency of Data.Sequence of course.
  29. hylo :: (a -> c -> c) -> (b -> Maybe (a, b)) -> c -> b -> c
  30. hylo f g c0 b0 = step (g b0) where
  31. step Nothing = c0
  32. step (Just (a,st)) = f a (step (g st))
  33. -- | Paramorphism (generalizes cata).
  34. para :: (a -> (Seq a, b) -> b) -> b -> Seq a -> b
  35. para f b0 se = step (viewl se) where
  36. step EmptyL = b0
  37. step (a :< sa) = f a (sa, step (viewl sa))
  38. -- | Apomorphism (generalizes ana).
  39. apo :: (b -> Maybe (a, b)) -> (b -> Seq a) -> b -> Seq a
  40. apo f g b0 = step (f b0) where
  41. step Nothing = g b0
  42. step (Just (a,st)) = a <| step (f st)
  43. -- | Zygomorphism.
  44. zygo :: (a -> b -> b) -> (a -> (Seq a, b) -> b) -> b -> Seq a -> b
  45. zygo f g b se = step (viewl se) where
  46. step EmptyL = b
  47. step (a :< sa) = f a (g a (sa, (step (viewl sa))))
  48. --------------------------------------------------------------------------------
  49. -- Monadic versions
  50. -- | Monadic catamorphism.
  51. cataM :: Monad m => (a -> b -> m b) -> b -> Seq a -> m b
  52. cataM f b se = step (viewl se) where
  53. step EmptyL = return b
  54. step (a :< sa) = do x <- step (viewl sa)
  55. f a x
  56. -- | Monadic anamorphism.
  57. anaM :: Monad m => (b -> m (Maybe (a, b))) -> b -> m (Seq a)
  58. anaM f b0 = f b0 >>= step where
  59. step Nothing = return empty
  60. step (Just (a,st)) = do x <- f st >>= step
  61. return (a <| x)
  62. -- | Monadic hylomorphism.
  63. hyloM :: Monad m => (a -> c -> m c) -> (b -> m (Maybe (a, b))) -> c -> b -> m c
  64. hyloM f g c0 b0 = g b0 >>= step where
  65. step Nothing = return c0
  66. step (Just (a,st)) = do x <- g st >>= step
  67. f a x
  68. -- | Monadic paramorphism.
  69. paraM :: Monad m => (a -> (Seq a, b) -> m b) -> b -> Seq a -> m b
  70. paraM f b0 se = step (viewl se) where
  71. step EmptyL = return b0
  72. step (a :< sa) = do st <- step (viewl sa)
  73. f a (se,st)
  74. -- | Monadic apomorphism.
  75. apoM :: Monad m => (b -> m (Maybe (a, b))) -> (b -> m (Seq a)) -> b -> m (Seq a)
  76. apoM f g b0 = f b0 >>= step where
  77. step Nothing = g b0
  78. step (Just (a,st)) = do sa <- f st >>= step
  79. return (a <| sa)
  80. -- | Monadic zygomorphism.
  81. zygoM :: Monad m =>
  82. (a -> b -> m b) -> (a -> (Seq a, b) -> m b) -> b -> Seq a -> m b
  83. zygoM f g b se = step (viewl se) where
  84. step EmptyL = return b
  85. step (a :< sa) = do st <- step (viewl sa)
  86. x <- g a (sa,st)
  87. f a x