1------------------------------------------------------------------------------
2-- CPP macros for common instances
3------------------------------------------------------------------------------
5-- XXX use template haskell instead and include Monoid and IsStream instances
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 (<*>) #-}; \
17#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
18instance Monad m => Functor (STREAM m) where { \
21instance (MonadBase b m, Monad m CONSTRAINT) => MonadBase b (STREAM m) where {\
22 liftBase = liftBaseDefault }; \
24instance (MonadIO m CONSTRAINT) => MonadIO (STREAM m) where { \
25 liftIO = lift . liftIO }; \
27instance (MonadThrow m CONSTRAINT) => MonadThrow (STREAM m) where { \
28 throwM = lift . throwM }; \
31instance (MonadError e m CONSTRAINT) => MonadError e (STREAM m) where { \
32 throwError = lift . throwError; \
34 fromStream $ withCatchError (toStream m) (\e -> toStream $ h e) }; \
37instance (MonadReader r m CONSTRAINT) => MonadReader r (STREAM m) where { \
39 local f m = fromStream $ K.withLocal f (toStream m) }; \
41instance (MonadState s m CONSTRAINT) => MonadState s (STREAM m) where { \
43 put x = lift (put x); \
44 state k = lift (state k) }
46------------------------------------------------------------------------------
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
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) () }
65#define NFDATA1_INSTANCE(STREAM)
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 }; \
76instance Eq a => Eq (STREAM Identity a) where { \
77 {-# INLINE (==) #-}; \
78 (==) xs ys = runIdentity $ P.eqBy (==) xs ys }; \
80instance Ord a => Ord (STREAM Identity a) where { \
81 {-# INLINE compare #-}; \
82 compare xs ys = runIdentity $ P.cmpBy compare xs ys; \
84 x < y = case compare x y of { LT -> True; _ -> False }; \
85 {-# INLINE (<=) #-}; \
86 x <= y = case compare x y of { GT -> False; _ -> True }; \
88 x > y = case compare x y of { GT -> True; _ -> False }; \
89 {-# INLINE (>=) #-}; \
90 x >= y = case compare x y of { LT -> False; _ -> True }; \
92 max x y = if x <= y then y else x; \
94 min x y = if x <= y then x else y; }; \
96instance Show a => Show (STREAM Identity a) where { \
97 showsPrec p dl = showParen (p > 10) $ \
98 showString "fromList " . shows (toList dl) }; \
100instance Read a => Read (STREAM Identity a) where { \
101 readPrec = parens $ prec 10 $ do { \
102 Ident "fromList" <- lexP; \
103 fromList <$> readPrec }; \
104 readListPrec = readListPrecDefault }; \
106instance (a ~ Char) => IsString (STREAM Identity a) where { \
107 {-# INLINE fromString #-}; \
108 fromString = P.fromList }; \
110instance NFData a => NFData (STREAM Identity a) where { \
111 {-# INLINE rnf #-}; \
112 rnf = runIdentity . P.foldl' (\_ x -> rnf x) () }; \
114-------------------------------------------------------------------------------
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-------------------------------------------------------------------------------
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 }}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list