[commit: packages/base] master: Add fusion RULES for mapMaybe (de86df4)

git at git.haskell.org git at git.haskell.org
Wed Dec 4 13:04:04 UTC 2013


Repository : ssh://git@git.haskell.org/base

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/de86df4da85f704a433c546e77f0d33ad6b9ad0d/base

>---------------------------------------------------------------

commit de86df4da85f704a433c546e77f0d33ad6b9ad0d
Author: Takano Akio <aljee at hyper.cx>
Date:   Wed Nov 20 16:30:31 2013 +0900

    Add fusion RULES for mapMaybe


>---------------------------------------------------------------

de86df4da85f704a433c546e77f0d33ad6b9ad0d
 Data/Maybe.hs |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/Data/Maybe.hs b/Data/Maybe.hs
index 245e307..fe2a0ab 100644
--- a/Data/Maybe.hs
+++ b/Data/Maybe.hs
@@ -126,4 +126,16 @@ mapMaybe f (x:xs) =
  case f x of
   Nothing -> rs
   Just r  -> r:rs
-
+{-# NOINLINE [1] mapMaybe #-}
+
+{-# RULES
+"mapMaybe"     [~1] forall f xs. mapMaybe f xs
+                    = build (\c n -> foldr (mapMaybeFB c f) n xs)
+"mapMaybeList" [1]  forall f. foldr (mapMaybeFB (:) f) [] = mapMaybe f
+  #-}
+
+{-# NOINLINE [0] mapMaybeFB #-}
+mapMaybeFB :: (b -> r -> r) -> (a -> Maybe b) -> a -> r -> r
+mapMaybeFB cons f x next = case f x of
+  Nothing -> next
+  Just r -> cons r next



More information about the ghc-commits mailing list