| 1 | {-# LANGUAGE CPP, ExistentialQuantification, Rank2Types #-} |
|---|
| 2 | |
|---|
| 3 | #ifdef USE_PRAGMAS |
|---|
| 4 | #define INPRAG(f) {-# INLINE f #-} |
|---|
| 5 | #define INPRAG0(f) {-# INLINE [0] f #-} |
|---|
| 6 | #define INPRAG1(f) {-# INLINE [1] f #-} |
|---|
| 7 | #else |
|---|
| 8 | #define INPRAG(f) |
|---|
| 9 | #define INPRAG0(f) |
|---|
| 10 | #define INPRAG1(f) |
|---|
| 11 | #endif |
|---|
| 12 | |
|---|
| 13 | module Data.Vector.Fusion.Stream.Monadic where |
|---|
| 14 | |
|---|
| 15 | import Control.Monad (liftM) |
|---|
| 16 | import Prelude (Monad(..), Int, Ord(..), ($), Maybe(..)) |
|---|
| 17 | |
|---|
| 18 | data Step s a = Yield a s |
|---|
| 19 | | Skip s |
|---|
| 20 | | Done |
|---|
| 21 | |
|---|
| 22 | data Stream m a = forall s. Stream (s -> m (Step s a)) s Size |
|---|
| 23 | |
|---|
| 24 | zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c |
|---|
| 25 | INPRAG1(zipWithM) |
|---|
| 26 | zipWithM f (Stream stepa sa0 na) (Stream stepb sb0 nb) |
|---|
| 27 | = Stream step (sa0, sb0, Nothing) (smaller na nb) |
|---|
| 28 | where |
|---|
| 29 | INPRAG0(step) |
|---|
| 30 | step (sa, sb, Nothing) = liftM (\r -> |
|---|
| 31 | case r of |
|---|
| 32 | Yield x sa' -> Skip (sa', sb, Just x) |
|---|
| 33 | Skip sa' -> Skip (sa', sb, Nothing) |
|---|
| 34 | Done -> Done |
|---|
| 35 | ) (stepa sa) |
|---|
| 36 | |
|---|
| 37 | step (sa, sb, Just x) = do |
|---|
| 38 | r <- stepb sb |
|---|
| 39 | case r of |
|---|
| 40 | Yield y sb' -> |
|---|
| 41 | do |
|---|
| 42 | z <- f x y |
|---|
| 43 | return $ Yield z (sa, sb', Nothing) |
|---|
| 44 | Skip sb' -> return $ Skip (sa, sb', Just x) |
|---|
| 45 | Done -> return $ Done |
|---|
| 46 | |
|---|
| 47 | zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 48 | INPRAG1(zipWith3M) |
|---|
| 49 | zipWith3M f (Stream stepa sa0 na) (Stream stepb sb0 nb) (Stream stepc sc0 nc) |
|---|
| 50 | = Stream step (sa0, sb0, sc0, Nothing) (smaller na (smaller nb nc)) |
|---|
| 51 | where |
|---|
| 52 | INPRAG0(step) |
|---|
| 53 | step (sa, sb, sc, Nothing) = do |
|---|
| 54 | r <- stepa sa |
|---|
| 55 | return $ case r of |
|---|
| 56 | Yield x sa' -> Skip (sa', sb, sc, Just (x, Nothing)) |
|---|
| 57 | Skip sa' -> Skip (sa', sb, sc, Nothing) |
|---|
| 58 | Done -> Done |
|---|
| 59 | |
|---|
| 60 | step (sa, sb, sc, Just (x, Nothing)) = do |
|---|
| 61 | r <- stepb sb |
|---|
| 62 | return $ case r of |
|---|
| 63 | Yield y sb' -> Skip (sa, sb', sc, Just (x, Just y)) |
|---|
| 64 | Skip sb' -> Skip (sa, sb', sc, Just (x, Nothing)) |
|---|
| 65 | Done -> Done |
|---|
| 66 | |
|---|
| 67 | step (sa, sb, sc, Just (x, Just y)) = do |
|---|
| 68 | r <- stepc sc |
|---|
| 69 | case r of |
|---|
| 70 | Yield z sc' -> f x y z >>= (\res -> return $ Yield res (sa, sb, sc', Nothing)) |
|---|
| 71 | Skip sc' -> return $ Skip (sa, sb, sc', Just (x, Just y)) |
|---|
| 72 | Done -> return $ Done |
|---|
| 73 | |
|---|
| 74 | zipWith4M :: Monad m => (a -> b -> c -> d -> m e) |
|---|
| 75 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 76 | -> Stream m e |
|---|
| 77 | INPRAG(zipWith4M) |
|---|
| 78 | zipWith4M f sa sb sc sd |
|---|
| 79 | = zipWithM (\(a,b) (c,d) -> f a b c d) (zip sa sb) (zip sc sd) |
|---|
| 80 | |
|---|
| 81 | zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f) |
|---|
| 82 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 83 | -> Stream m e -> Stream m f |
|---|
| 84 | INPRAG(zipWith5M) |
|---|
| 85 | zipWith5M f sa sb sc sd se |
|---|
| 86 | = zipWithM (\(a,b,c) (d,e) -> f a b c d e) (zip3 sa sb sc) (zip sd se) |
|---|
| 87 | |
|---|
| 88 | zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g) |
|---|
| 89 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 90 | -> Stream m e -> Stream m f -> Stream m g |
|---|
| 91 | INPRAG(zipWith6M) |
|---|
| 92 | zipWith6M fn sa sb sc sd se sf |
|---|
| 93 | = zipWithM (\(a,b,c) (d,e,f) -> fn a b c d e f) (zip3 sa sb sc) |
|---|
| 94 | (zip3 sd se sf) |
|---|
| 95 | |
|---|
| 96 | zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c |
|---|
| 97 | INPRAG(zipWith) |
|---|
| 98 | zipWith f = zipWithM (\a b -> return (f a b)) |
|---|
| 99 | |
|---|
| 100 | zipWith3 :: Monad m => (a -> b -> c -> d) |
|---|
| 101 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 102 | INPRAG(zipWith3) |
|---|
| 103 | zipWith3 f = zipWith3M (\a b c -> return (f a b c)) |
|---|
| 104 | |
|---|
| 105 | zipWith4 :: Monad m => (a -> b -> c -> d -> e) |
|---|
| 106 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 107 | -> Stream m e |
|---|
| 108 | INPRAG(zipWith4) |
|---|
| 109 | zipWith4 f = zipWith4M (\a b c d -> return (f a b c d)) |
|---|
| 110 | |
|---|
| 111 | zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f) |
|---|
| 112 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 113 | -> Stream m e -> Stream m f |
|---|
| 114 | INPRAG(zipWith5) |
|---|
| 115 | zipWith5 f = zipWith5M (\a b c d e -> return (f a b c d e)) |
|---|
| 116 | |
|---|
| 117 | zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g) |
|---|
| 118 | -> Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 119 | -> Stream m e -> Stream m f -> Stream m g |
|---|
| 120 | INPRAG(zipWith6) |
|---|
| 121 | zipWith6 fn = zipWith6M (\a b c d e f -> return (fn a b c d e f)) |
|---|
| 122 | |
|---|
| 123 | zip :: Monad m => Stream m a -> Stream m b -> Stream m (a,b) |
|---|
| 124 | INPRAG(zip) |
|---|
| 125 | zip = zipWith (,) |
|---|
| 126 | |
|---|
| 127 | zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a,b,c) |
|---|
| 128 | INPRAG(zip3) |
|---|
| 129 | zip3 = zipWith3 (,,) |
|---|
| 130 | |
|---|
| 131 | zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 132 | -> Stream m (a,b,c,d) |
|---|
| 133 | INPRAG(zip4) |
|---|
| 134 | zip4 = zipWith4 (,,,) |
|---|
| 135 | |
|---|
| 136 | zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 137 | -> Stream m e -> Stream m (a,b,c,d,e) |
|---|
| 138 | INPRAG(zip5) |
|---|
| 139 | zip5 = zipWith5 (,,,,) |
|---|
| 140 | |
|---|
| 141 | zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d |
|---|
| 142 | -> Stream m e -> Stream m f -> Stream m (a,b,c,d,e,f) |
|---|
| 143 | INPRAG(zip6) |
|---|
| 144 | zip6 = zipWith6 (,,,,,) |
|---|
| 145 | |
|---|
| 146 | delay_inline :: (a -> b) -> a -> b |
|---|
| 147 | delay_inline f = f |
|---|
| 148 | |
|---|
| 149 | data Size = Exact Int |
|---|
| 150 | | Max Int |
|---|
| 151 | | Unknown |
|---|
| 152 | |
|---|
| 153 | smaller :: Size -> Size -> Size |
|---|
| 154 | INPRAG(smaller) |
|---|
| 155 | smaller (Exact m) (Exact n) = Exact (min m n) |
|---|
| 156 | smaller (Exact m) (Max n) = Max (min m n) |
|---|
| 157 | smaller (Exact m) Unknown = Max m |
|---|
| 158 | smaller (Max m) (Exact n) = Max (min m n) |
|---|
| 159 | smaller (Max m) (Max n) = Max (min m n) |
|---|
| 160 | smaller (Max m) Unknown = Max m |
|---|
| 161 | smaller Unknown (Exact n) = Max n |
|---|
| 162 | smaller Unknown (Max n) = Max n |
|---|
| 163 | smaller Unknown Unknown = Unknown |
|---|