on 3de89fe help sign in
134 lines 8.3 kB history raw
1------------------------------------------------------------------------------
2-- CPP macros for common instances
3------------------------------------------------------------------------------
5-- XXX use template haskell instead and include Monoid and IsStream instances
6-- as well.
8#define MONADPARALLEL , MonadAsync m
10#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
11instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
12 {-# INLINE pure #-}; \
13 pure = STREAM . K.yield; \
14 {-# INLINE (<*>) #-}; \
15 (<*>) = ap }
17#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
18instance Monad m => Functor (STREAM m) where { \
19 fmap = map }; \
20 \
21instance (MonadBase b m, Monad m CONSTRAINT) => MonadBase b (STREAM m) where {\
22 liftBase = liftBaseDefault }; \
23 \
24instance (MonadIO m CONSTRAINT) => MonadIO (STREAM m) where { \
25 liftIO = lift . liftIO }; \
26 \
27instance (MonadThrow m CONSTRAINT) => MonadThrow (STREAM m) where { \
28 throwM = lift . throwM }; \
29 \
30{- \
31instance (MonadError e m CONSTRAINT) => MonadError e (STREAM m) where { \
32 throwError = lift . throwError; \
33 catchError m h = \
34 fromStream $ withCatchError (toStream m) (\e -> toStream $ h e) }; \
35-} \
36 \
37instance (MonadReader r m CONSTRAINT) => MonadReader r (STREAM m) where { \
38 ask = lift ask; \
39 local f m = fromStream $ K.withLocal f (toStream m) }; \
40 \
41instance (MonadState s m CONSTRAINT) => MonadState s (STREAM m) where { \
42 get = lift get; \
43 put x = lift (put x); \
44 state k = lift (state k) }
46------------------------------------------------------------------------------
47-- Lists
48------------------------------------------------------------------------------
50-- Serial streams can act like regular lists using the Identity monad
52-- XXX Show instance is 10x slower compared to read, we can do much better.
53-- The list show instance itself is really slow.
55-- XXX The default definitions of "<" in the Ord instance etc. do not perform
56-- well, because they do not get inlined. Need to add INLINE in Ord class in
57-- base?
59#if MIN_VERSION_deepseq(1,4,3)
60#define NFDATA1_INSTANCE(STREAM) \
61instance NFData1 (STREAM Identity) where { \
62 {-# INLINE liftRnf #-}; \
63 liftRnf r = runIdentity . P.foldl' (\_ x -> r x) () }
64#else
65#define NFDATA1_INSTANCE(STREAM)
66#endif
68#define LIST_INSTANCES(STREAM) \
69instance IsList (STREAM Identity a) where { \
70 type (Item (STREAM Identity a)) = a; \
71 {-# INLINE fromList #-}; \
72 fromList = P.fromList; \
73 {-# INLINE toList #-}; \
74 toList = runIdentity . P.toList }; \
75 \
76instance Eq a => Eq (STREAM Identity a) where { \
77 {-# INLINE (==) #-}; \
78 (==) xs ys = runIdentity $ P.eqBy (==) xs ys }; \
79 \
80instance Ord a => Ord (STREAM Identity a) where { \
81 {-# INLINE compare #-}; \
82 compare xs ys = runIdentity $ P.cmpBy compare xs ys; \
83 {-# INLINE (<) #-}; \
84 x < y = case compare x y of { LT -> True; _ -> False }; \
85 {-# INLINE (<=) #-}; \
86 x <= y = case compare x y of { GT -> False; _ -> True }; \
87 {-# INLINE (>) #-}; \
88 x > y = case compare x y of { GT -> True; _ -> False }; \
89 {-# INLINE (>=) #-}; \
90 x >= y = case compare x y of { LT -> False; _ -> True }; \
91 {-# INLINE max #-}; \
92 max x y = if x <= y then y else x; \
93 {-# INLINE min #-}; \
94 min x y = if x <= y then x else y; }; \
95 \
96instance Show a => Show (STREAM Identity a) where { \
97 showsPrec p dl = showParen (p > 10) $ \
98 showString "fromList " . shows (toList dl) }; \
99 \
100instance Read a => Read (STREAM Identity a) where { \
101 readPrec = parens $ prec 10 $ do { \
102 Ident "fromList" <- lexP; \
103 fromList <$> readPrec }; \
104 readListPrec = readListPrecDefault }; \
105 \
106instance (a ~ Char) => IsString (STREAM Identity a) where { \
107 {-# INLINE fromString #-}; \
108 fromString = P.fromList }; \
109 \
110instance NFData a => NFData (STREAM Identity a) where { \
111 {-# INLINE rnf #-}; \
112 rnf = runIdentity . P.foldl' (\_ x -> rnf x) () }; \
114-------------------------------------------------------------------------------
115-- Foldable
116-------------------------------------------------------------------------------
118-- XXX the foldable instance seems to be quit slow. We can try writing
119-- custom implementations of foldr and foldl'. If nothing works we can also try
120-- writing a Foldable for Identity monad rather than for "Foldable m".
121#define FOLDABLE_INSTANCE(STREAM) \
122instance (Foldable m, Monad m) => Foldable (STREAM m) where { \
123 {-# INLINE foldMap #-}; \
124 foldMap f = fold . P.foldr mappend mempty . fmap f }
126-------------------------------------------------------------------------------
127-- Traversable
128-------------------------------------------------------------------------------
130#define TRAVERSABLE_INSTANCE(STREAM) \
131instance Traversable (STREAM Identity) where { \
132 {-# INLINE traverse #-}; \
133 traverse f s = runIdentity $ P.foldr consA (pure mempty) s \
134 where { consA x ys = liftA2 K.cons (f x) ys }}