Skip to content

Commit 775e99a

Browse files
masaeeduhdgarrood
authored andcommitted
Add monoid instances for uncurried EffectFns (#13)
* Add monoid instances * Eta-expand all the things to help optimizer * Add comment
1 parent 6caa8e1 commit 775e99a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/Effect/Uncurried.purs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132

133133
module Effect.Uncurried where
134134

135+
import Data.Monoid (class Monoid, class Semigroup, mempty, (<>))
135136
import Effect (Effect)
136137

137138
foreign import data EffectFn1 :: Type -> Type -> Type
@@ -186,3 +187,71 @@ foreign import runEffectFn9 :: forall a b c d e f g h i r.
186187
EffectFn9 a b c d e f g h i r -> a -> b -> c -> d -> e -> f -> g -> h -> i -> Effect r
187188
foreign import runEffectFn10 :: forall a b c d e f g h i j r.
188189
EffectFn10 a b c d e f g h i j r -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> Effect r
190+
191+
-- The reason these are written eta-expanded instead of as:
192+
-- ```
193+
-- append f1 f2 = mkEffectFnN $ runEffectFnN f1 <> runEffectFnN f2
194+
-- ```
195+
-- is to help the compiler recognize that it can emit uncurried
196+
-- JS functions (which are more efficient), when an appended
197+
-- EffectFn is applied to all its arguments
198+
199+
instance semigroupEffectFn1 :: Semigroup r => Semigroup (EffectFn1 a r) where
200+
append f1 f2 = mkEffectFn1 \a -> runEffectFn1 f1 a <> runEffectFn1 f2 a
201+
202+
instance semigroupEffectFn2 :: Semigroup r => Semigroup (EffectFn2 a b r) where
203+
append f1 f2 = mkEffectFn2 \a b -> runEffectFn2 f1 a b <> runEffectFn2 f2 a b
204+
205+
instance semigroupEffectFn3 :: Semigroup r => Semigroup (EffectFn3 a b c r) where
206+
append f1 f2 = mkEffectFn3 \a b c -> runEffectFn3 f1 a b c <> runEffectFn3 f2 a b c
207+
208+
instance semigroupEffectFn4 :: Semigroup r => Semigroup (EffectFn4 a b c d r) where
209+
append f1 f2 = mkEffectFn4 \a b c d -> runEffectFn4 f1 a b c d <> runEffectFn4 f2 a b c d
210+
211+
instance semigroupEffectFn5 :: Semigroup r => Semigroup (EffectFn5 a b c d e r) where
212+
append f1 f2 = mkEffectFn5 \a b c d e -> runEffectFn5 f1 a b c d e <> runEffectFn5 f2 a b c d e
213+
214+
instance semigroupEffectFn6 :: Semigroup r => Semigroup (EffectFn6 a b c d e f r) where
215+
append f1 f2 = mkEffectFn6 \a b c d e f -> runEffectFn6 f1 a b c d e f <> runEffectFn6 f2 a b c d e f
216+
217+
instance semigroupEffectFn7 :: Semigroup r => Semigroup (EffectFn7 a b c d e f g r) where
218+
append f1 f2 = mkEffectFn7 \a b c d e f g -> runEffectFn7 f1 a b c d e f g <> runEffectFn7 f2 a b c d e f g
219+
220+
instance semigroupEffectFn8 :: Semigroup r => Semigroup (EffectFn8 a b c d e f g h r) where
221+
append f1 f2 = mkEffectFn8 \a b c d e f g h -> runEffectFn8 f1 a b c d e f g h <> runEffectFn8 f2 a b c d e f g h
222+
223+
instance semigroupEffectFn9 :: Semigroup r => Semigroup (EffectFn9 a b c d e f g h i r) where
224+
append f1 f2 = mkEffectFn9 \a b c d e f g h i -> runEffectFn9 f1 a b c d e f g h i <> runEffectFn9 f2 a b c d e f g h i
225+
226+
instance semigroupEffectFn10 :: Semigroup r => Semigroup (EffectFn10 a b c d e f g h i j r) where
227+
append f1 f2 = mkEffectFn10 \a b c d e f g h i j -> runEffectFn10 f1 a b c d e f g h i j <> runEffectFn10 f2 a b c d e f g h i j
228+
229+
instance monoidEffectFn1 :: Monoid r => Monoid (EffectFn1 a r) where
230+
mempty = mkEffectFn1 \_ -> mempty
231+
232+
instance monoidEffectFn2 :: Monoid r => Monoid (EffectFn2 a b r) where
233+
mempty = mkEffectFn2 \_ _ -> mempty
234+
235+
instance monoidEffectFn3 :: Monoid r => Monoid (EffectFn3 a b c r) where
236+
mempty = mkEffectFn3 \_ _ _ -> mempty
237+
238+
instance monoidEffectFn4 :: Monoid r => Monoid (EffectFn4 a b c d r) where
239+
mempty = mkEffectFn4 \_ _ _ _ -> mempty
240+
241+
instance monoidEffectFn5 :: Monoid r => Monoid (EffectFn5 a b c d e r) where
242+
mempty = mkEffectFn5 \_ _ _ _ _ -> mempty
243+
244+
instance monoidEffectFn6 :: Monoid r => Monoid (EffectFn6 a b c d e f r) where
245+
mempty = mkEffectFn6 \_ _ _ _ _ _ -> mempty
246+
247+
instance monoidEffectFn7 :: Monoid r => Monoid (EffectFn7 a b c d e f g r) where
248+
mempty = mkEffectFn7 \_ _ _ _ _ _ _ -> mempty
249+
250+
instance monoidEffectFn8 :: Monoid r => Monoid (EffectFn8 a b c d e f g h r) where
251+
mempty = mkEffectFn8 \_ _ _ _ _ _ _ _ -> mempty
252+
253+
instance monoidEffectFn9 :: Monoid r => Monoid (EffectFn9 a b c d e f g h i r) where
254+
mempty = mkEffectFn9 \_ _ _ _ _ _ _ _ _ -> mempty
255+
256+
instance monoidEffectFn10 :: Monoid r => Monoid (EffectFn10 a b c d e f g h i j r) where
257+
mempty = mkEffectFn10 \_ _ _ _ _ _ _ _ _ _ -> mempty

0 commit comments

Comments
 (0)