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_COMMON_INSTANCES(STREAM,CONSTRAINT) \
11instance Monad m => Functor (STREAM m) where { \
12 {-# INLINE fmap #-}; \
13 fmap f (STREAM m) = D.fromStreamD $ D.mapM (return . f) $ D.toStreamD m }; \
15instance (MonadBase b m, Monad m CONSTRAINT) => MonadBase b (STREAM m) where {\
16 liftBase = liftBaseDefault }; \
18instance (MonadIO m CONSTRAINT) => MonadIO (STREAM m) where { \
19 liftIO = lift . liftIO }; \
21instance (MonadThrow m CONSTRAINT) => MonadThrow (STREAM m) where { \
22 throwM = lift . throwM }; \
25instance (MonadError e m CONSTRAINT) => MonadError e (STREAM m) where { \
26 throwError = lift . throwError; \
28 fromStream $ withCatchError (toStream m) (\e -> toStream $ h e) }; \
31instance (MonadReader r m CONSTRAINT) => MonadReader r (STREAM m) where { \
33 local f m = fromStream $ K.withLocal f (toStream m) }; \
35instance (MonadState s m CONSTRAINT) => MonadState s (STREAM m) where { \
37 put x = lift (put x); \
38 state k = lift (state k) }
40------------------------------------------------------------------------------
42------------------------------------------------------------------------------
44-- Serial streams can act like regular lists using the Identity monad
46-- XXX Show instance is 10x slower compared to read, we can do much better.
47-- The list show instance itself is really slow.
49-- XXX The default definitions of "<" in the Ord instance etc. do not perform
50-- well, because they do not get inlined. Need to add INLINE in Ord class in
53#if MIN_VERSION_deepseq(1,4,3)
54#define NFDATA1_INSTANCE(STREAM) \
55instance NFData1 (STREAM Identity) where { \
56 {-# INLINE liftRnf #-}; \
57 liftRnf r = runIdentity . P.foldl' (\_ x -> r x) () }
59#define NFDATA1_INSTANCE(STREAM)
62#define LIST_INSTANCES(STREAM) \
63instance IsList (STREAM Identity a) where { \
64 type (Item (STREAM Identity a)) = a; \
65 {-# INLINE fromList #-}; \
66 fromList = P.fromList; \
67 {-# INLINE toList #-}; \
68 toList = runIdentity . P.toList }; \
70instance Eq a => Eq (STREAM Identity a) where { \
71 {-# INLINE (==) #-}; \
72 (==) xs ys = runIdentity $ P.eqBy (==) xs ys }; \
74instance Ord a => Ord (STREAM Identity a) where { \
75 {-# INLINE compare #-}; \
76 compare xs ys = runIdentity $ P.cmpBy compare xs ys; \
78 x < y = case compare x y of { LT -> True; _ -> False }; \
79 {-# INLINE (<=) #-}; \
80 x <= y = case compare x y of { GT -> False; _ -> True }; \
82 x > y = case compare x y of { GT -> True; _ -> False }; \
83 {-# INLINE (>=) #-}; \
84 x >= y = case compare x y of { LT -> False; _ -> True }; \
86 max x y = if x <= y then y else x; \
88 min x y = if x <= y then x else y; }; \
90instance Show a => Show (STREAM Identity a) where { \
91 showsPrec p dl = showParen (p > 10) $ \
92 showString "fromList " . shows (toList dl) }; \
94instance Read a => Read (STREAM Identity a) where { \
95 readPrec = parens $ prec 10 $ do { \
96 Ident "fromList" <- lexP; \
97 fromList <$> readPrec }; \
98 readListPrec = readListPrecDefault }; \
100instance (a ~ Char) => IsString (STREAM Identity a) where { \
101 {-# INLINE fromString #-}; \
102 fromString = P.fromList }; \
104instance NFData a => NFData (STREAM Identity a) where { \
105 {-# INLINE rnf #-}; \
106 rnf = runIdentity . P.foldl' (\_ x -> rnf x) () }; \
108-------------------------------------------------------------------------------
110-------------------------------------------------------------------------------
112-- XXX the foldable instance seems to be quit slow. We can try writing
113-- custom implementations of foldr and foldl'. If nothing works we can also try
114-- writing a Foldable for Identity monad rather than for "Foldable m".
115#define FOLDABLE_INSTANCE(STREAM) \
116instance (Foldable m, Monad m) => Foldable (STREAM m) where { \
117 {-# INLINE foldMap #-}; \
118 foldMap f = fold . P.foldr mappend mempty . fmap f }
120-------------------------------------------------------------------------------
122-------------------------------------------------------------------------------
124#define TRAVERSABLE_INSTANCE(STREAM) \
125instance Traversable (STREAM Identity) where { \
126 {-# INLINE traverse #-}; \
127 traverse f s = runIdentity $ P.foldr consA (pure mempty) s \
128 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