From 0e199043ef7cfe72d4076ac0c5f54001a06907d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20L=C3=B3pez=20Juan?= <victor@lopezjuan.com>
Date: Sat, 14 Jun 2014 23:36:20 +0200
Subject: [PATCH 1/2] Changed reduction order to row-major.

---
 repa/Data/Array/Repa/Eval/Reduction.hs | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/repa/Data/Array/Repa/Eval/Reduction.hs b/repa/Data/Array/Repa/Eval/Reduction.hs
index 71e072e..270054c 100644
--- a/repa/Data/Array/Repa/Eval/Reduction.hs
+++ b/repa/Data/Array/Repa/Eval/Reduction.hs
@@ -153,7 +153,7 @@ reduceAny f c !r !start !end
    {-# INLINE iter #-}
    iter !i !z 
     | 1# <- i >=# end  = z 
-    | otherwise        = iter (i +# 1#) (f i `c` z)
+    | otherwise        = iter (i +# 1#) (z `c` f i)
 
 
 {-# INLINE [0] reduceInt #-}
@@ -170,7 +170,7 @@ reduceInt f c !r !start !end
    {-# INLINE iter #-}
    iter !i !z 
     | 1# <- i >=# end   = z 
-    | otherwise         = iter (i +# 1#) (f i `c` z)
+    | otherwise         = iter (i +# 1#) (z `c` f i)
 
 
 {-# INLINE [0] reduceFloat #-}
@@ -187,7 +187,7 @@ reduceFloat f c !r !start !end
    {-# INLINE iter #-}
    iter !i !z 
     | 1# <- i >=# end   = z 
-    | otherwise         = iter (i +# 1#) (f i `c` z)
+    | otherwise         = iter (i +# 1#) (z `c` f i)
 
 
 {-# INLINE [0] reduceDouble #-}
@@ -204,7 +204,7 @@ reduceDouble f c !r !start !end
    {-# INLINE iter #-}
    iter !i !z 
     | 1# <- i >=# end   = z 
-    | otherwise         = iter (i +# 1#) (f i `c` z)
+    | otherwise         = iter (i +# 1#) (z `c` f i)
 
 
 {-# INLINE unboxInt #-}
-- 
1.9.3


From d7b2d1c0ea240ad9ef0e08a002d88a724cc9ebc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20L=C3=B3pez=20Juan?= <victor@lopezjuan.com>
Date: Sun, 15 Jun 2014 00:05:17 +0200
Subject: [PATCH 2/2] Documented reduction order for fold functions.

---
 repa/Data/Array/Repa/Operators/Reduction.hs | 39 +++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/repa/Data/Array/Repa/Operators/Reduction.hs b/repa/Data/Array/Repa/Operators/Reduction.hs
index 7257bf7..046992d 100644
--- a/repa/Data/Array/Repa/Operators/Reduction.hs
+++ b/repa/Data/Array/Repa/Operators/Reduction.hs
@@ -24,6 +24,15 @@ import GHC.Exts
 -- | Sequential reduction of the innermost dimension of an arbitrary rank array.
 --
 --   Combine this with `transpose` to fold any other dimension.
+--
+--   Elements are reduced in the order of their indices, from lowest to highest.
+--   Applications of the operator are associatied arbitrarily.
+--
+--   >>> let c 0 0 = 0; c 0 x = x; c x 0 = x; c x y = y
+--   >>> let a = fromListUnboxed (Z :. 2 :. 2) [1,2,3,4] :: Array U (Z :. Int :. Int) Int
+--   >>> foldS c 0 a
+--   AUnboxed (Z :. 2) (fromList [2,4])
+--
 foldS   :: (Shape sh, Source r a, Elt a, Unbox a)
         => (a -> a -> a)
         -> a
@@ -49,6 +58,15 @@ foldS f z arr
 --   example @0@ is neutral with respect to @(+)@ as @0 + a = a@.
 --   These restrictions are required to support parallel evaluation, as the
 --   starting element may be used multiple times depending on the number of threads.
+--
+--   Elements are reduced in the order of their indices, from lowest to highest.
+--   Applications of the operator are associatied arbitrarily.
+--
+--   >>> let c 0 0 = 0; c 0 x = x; c x 0 = x; c x y = y
+--   >>> let a = fromListUnboxed (Z :. 2 :. 2) [1,2,3,4] :: Array U (Z :. Int :. Int) Int
+--   >>> foldP c 0 a
+--   AUnboxed (Z :. 2) (fromList [2,4])
+--
 foldP   :: (Shape sh, Source r a, Elt a, Unbox a, Monad m)
         => (a -> a -> a)
         -> a
@@ -79,6 +97,16 @@ foldP f z arr
 -- foldAll --------------------------------------------------------------------
 -- | Sequential reduction of an array of arbitrary rank to a single scalar value.
 --
+--   Elements are reduced in row-major order. Applications of the operator are
+--   associated arbitrarily.
+--
+--   >>> let show' 0 = ""; show' x = show x
+--   >>> let read' "" = 0; read' x = read x
+--   >>> let c x y = read' $ show' x Prelude.++ show' y
+--   >>> let a = fromListUnboxed (Z :. 2 :. 2) [1,2,3,4] :: Array U (Z :. Int :. Int) Int
+--   >>> foldAllS c 0 a
+--   1234
+--
 foldAllS :: (Shape sh, Source r a, Elt a, Unbox a)
 	=> (a -> a -> a)
 	-> a
@@ -102,6 +130,17 @@ foldAllS f z arr
 --   for example @0@ is neutral with respect to @(+)@ as @0 + a = a@.
 --   These restrictions are required to support parallel evaluation, as the
 --   starting element may be used multiple times depending on the number of threads.
+--
+--   Elements are reduced in row-major order. Applications of the operator are
+--   associated arbitrarily.
+--
+--   >>> let show' 0 = ""; show' x = show x
+--   >>> let read' "" = 0; read' x = read x
+--   >>> let c x y = read' $ show' x Prelude.++ show' y
+--   >>> let a = fromListUnboxed (Z :. 2 :. 2) [1,2,3,4] :: Array U (Z :. Int :. Int) Int
+--   >>> foldAllP c 0 a
+--   1234
+--
 foldAllP 
         :: (Shape sh, Source r a, Elt a, Unbox a, Monad m)
 	=> (a -> a -> a)
-- 
1.9.3

