summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Breitner <mail@joachim-breitner.de>2014-10-06 23:04:02 +0200
committerJoachim Breitner <mail@joachim-breitner.de>2014-10-31 14:19:42 +0100
commit294b7490e028d619c6e27636fe0d9affd32be6c1 (patch)
treeeb6b0e3eaa16894912362e56fd4343d6beb2d28d
parent8b1de47667bb8b30986ef9fc72d37c15b61640ae (diff)
downloadhaskell-wip/oneShot.tar.gz
Add oneShot demo filewip/oneShot
(if you remove {-# GHC_OPTIONS -fno-call-arity #-} then both functions have the same Core). Obviously, this patch is not meant to be merged.
-rw-r--r--OneShotTest.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/OneShotTest.hs b/OneShotTest.hs
new file mode 100644
index 0000000000..b595285069
--- /dev/null
+++ b/OneShotTest.hs
@@ -0,0 +1,22 @@
+{-# OPTIONS_GHC -fno-call-arity #-}
+
+module OneShotTest (foldlB, foldlA, fooA, fooB, fooC) where
+
+import GHC.Prim (oneShot)
+
+foldlA, foldlB :: (x -> a -> a) -> a -> [x] -> a
+
+foldlA k a xs = foldr (\v f a -> f (v `k` a)) id xs a
+{-# INLINEABLE foldlA #-}
+
+foldlB k a xs = foldr (\v f -> oneShot (\ a -> f (v `k` a))) id xs a
+{-# INLINEABLE foldlB #-}
+
+f :: Int -> Bool
+f 0 = True
+f 1 = False
+{-# NOINLINE f #-}
+
+fooA = foldlA (+) 0 . filter f
+fooB = foldlB (+) 0 . filter f
+fooC = foldl (+) 0 . filter f