module Data.Number.IReal.FoldB where
import Data.Number.IReal.IReal
import Data.Number.IReal.Auxiliary
import Data.Number.IReal.IntegerInterval
import Data.Number.IReal.Scalable
import Data.Number.IReal.IRealOperations
import Data.List
foldb :: (a -> a -> a) -> a -> [a] -> a
foldb f u [] = u
foldb f u [x] = f u x
foldb f u xs = foldb f u (g xs)
where g (x:y:xs) = f x y : g xs
g xs = xs
foldb1 f (x : xs) = foldb f x xs
bsum :: Num a => [a] -> a
bsum = foldb (+) 0
foldb' :: (a -> a -> a) -> a -> [a] -> a
foldb' f u xs = foldl' sum u (foldl' add [Nothing] xs)
where add st x = seq x $ case st of
[] -> [Just x]
Nothing : st' -> Just x : st'
Just y : st' -> seq st' (Nothing : add st' (f x y))
sum s m = maybe s (f s) m
isum' :: [IReal] -> IReal
isum' xs = isumN' (fromIntegral (length xs)) xs
isumN' :: Integer -> [IReal] -> IReal
isumN' _ [] = 0
isumN' _ [x] = x
isumN' n xs = ir (\p -> scale (foldl' pl 0 (map (flip appr (p+p')) xs)) (p'))
where p' = lg2 n + 2
pl (I (a,b)) (I (c,d))
= seq e (seq f (I (e,f)))
where e = a+c
f = b+d