on master help sign in
More efficient instances for compiler
9964f24Jasper Van der Jeugt
src/Hakyll/Core/Compiler/Internal.hs+42−8
@@ -97,53 +97,87 @@ data Compiler a b = Compiler
97
97
98--------------------------------------------------------------------------------
98--------------------------------------------------------------------------------
99instance Functor (Compiler a) where
99instance Functor (Compiler a) where
100− fmap f ~(Compiler d j) = Compiler d $ fmap f . j
100+ fmap f (Compiler d j) = Compiler d $ fmap f . j
101+ {-# INLINE fmap #-}
101
102
102
103
103--------------------------------------------------------------------------------
104--------------------------------------------------------------------------------
104instance Applicative (Compiler a) where
105instance Applicative (Compiler a) where
105 pure = fromJob . const . return
106 pure = fromJob . const . return
107+ {-# INLINE pure #-}
108+
106− ~(Compiler d1 j1) <*> ~(Compiler d2 j2) =
109+ Compiler d1 j1 <*> Compiler d2 j2 =
107 Compiler (liftM2 S.union d1 d2) $ \x -> j1 x <*> j2 x
110 Compiler (liftM2 S.union d1 d2) $ \x -> j1 x <*> j2 x
111+ {-# INLINE (<*>) #-}
108
112
109
113
110--------------------------------------------------------------------------------
114--------------------------------------------------------------------------------
111instance Alternative (Compiler a) where
115instance Alternative (Compiler a) where
112 empty = fromJob $ const $ CompilerM $
116 empty = fromJob $ const $ CompilerM $
113 throwError "Hakyll.Core.Compiler.Internal: empty alternative"
117 throwError "Hakyll.Core.Compiler.Internal: empty alternative"
118+
114− ~(Compiler d1 j1) <|> ~(Compiler d2 j2) =
119+ Compiler d1 j1 <|> Compiler d2 j2 =
115 Compiler (liftM2 S.union d1 d2) $ \x -> CompilerM $
120 Compiler (liftM2 S.union d1 d2) $ \x -> CompilerM $
116 catchError (unCompilerM $ j1 x) (\_ -> unCompilerM $ j2 x)
121 catchError (unCompilerM $ j1 x) (\_ -> unCompilerM $ j2 x)
122+ {-# INLINE (<|>) #-}
117
123
118
124
119--------------------------------------------------------------------------------
125--------------------------------------------------------------------------------
120instance Category Compiler where
126instance Category Compiler where
121 id = Compiler (return S.empty) return
127 id = Compiler (return S.empty) return
128+ {-# INLINE id #-}
129+
122− ~(Compiler d1 j1) . ~(Compiler d2 j2) =
130+ Compiler d1 j1 . Compiler d2 j2 =
123 Compiler (liftM2 S.union d1 d2) (j1 <=< j2)
131 Compiler (liftM2 S.union d1 d2) (j1 <=< j2)
132+ {-# INLINE (.) #-}
124
133
125
134
126--------------------------------------------------------------------------------
135--------------------------------------------------------------------------------
127instance Arrow Compiler where
136instance Arrow Compiler where
128 arr f = fromJob (return . f)
137 arr f = fromJob (return . f)
138+ {-# INLINE arr #-}
139+
129− first ~(Compiler d j) = Compiler d $ \(x, y) -> do
140+ first (Compiler d j) = Compiler d $ \(x, y) -> do
130 x' <- j x
141 x' <- j x
131 return (x', y)
142 return (x', y)
143+ {-# INLINE first #-}
144+
145+ second (Compiler d j) = Compiler d $ \(x, y) -> do
146+ y' <- j y
147+ return (x, y')
148+ {-# INLINE second #-}
149+
150+ Compiler d1 j1 *** Compiler d2 j2 =
151+ Compiler (liftM2 S.union d1 d2) $ \(x, y) -> do
152+ x' <- j1 x
153+ y' <- j2 y
154+ return (x', y')
155+ {-# INLINE (***) #-}
156+
157+ Compiler d1 j1 &&& Compiler d2 j2 =
158+ Compiler (liftM2 S.union d1 d2) $ \x -> do
159+ y1 <- j1 x
160+ y2 <- j2 x
161+ return (y1, y2)
162+ {-# INLINE (&&&) #-}
132
163
133
164
134--------------------------------------------------------------------------------
165--------------------------------------------------------------------------------
135instance ArrowChoice Compiler where
166instance ArrowChoice Compiler where
136− left ~(Compiler d j) = Compiler d $ \e -> case e of
167+ left (Compiler d j) = Compiler d $ \e -> case e of
137 Left l -> Left <$> j l
168 Left l -> Left <$> j l
138 Right r -> Right <$> return r
169 Right r -> Right <$> return r
139− -- Defined here for efficiency
170+ {-# INLINE left #-}
171+
140− ~(Compiler d1 j1) ||| ~(Compiler d2 j2) = Compiler (liftM2 S.union d1 d2) $
172+ Compiler d1 j1 ||| Compiler d2 j2 = Compiler (liftM2 S.union d1 d2) $
141 \e -> case e of Left x -> j1 x; Right y -> j2 y
173 \e -> case e of Left x -> j1 x; Right y -> j2 y
174+ {-# INLINE (|||) #-}
142
175
143
176
144--------------------------------------------------------------------------------
177--------------------------------------------------------------------------------
145instance ArrowMap Compiler where
178instance ArrowMap Compiler where
146 mapA (Compiler d j) = Compiler d $ mapM j
179 mapA (Compiler d j) = Compiler d $ mapM j
180+ {-# INLINE mapA #-}
147
181
148
182
149--------------------------------------------------------------------------------
183--------------------------------------------------------------------------------