Wed Jul 29 13:03:09 EDT 2009 wasserman.louis@gmail.com
* Ticket #3271: New methods for Data.Sequence
New patches:
[Ticket #3271: New methods for Data.Sequence
wasserman.louis@gmail.com**20090729170309
Ignorethis: d5a2ee4c7f2c4c30b994094a16310504
] {
hunk ./Data/Sequence.hs 6
 
 Module : Data.Sequence
 Copyright : (c) Ross Paterson 2005
+ (c) Louis Wasserman 2009
 License : BSDstyle
 Maintainer : libraries@haskell.org
 Stability : experimental
hunk ./Data/Sequence.hs 40
 * Construction
empty,  :: Seq a
singleton,  :: a > Seq a
+ replicate,  :: Int > a > Seq a
(<),  :: a > Seq a > Seq a
(>),  :: Seq a > a > Seq a
(><),  :: Seq a > Seq a > Seq a
hunk ./Data/Sequence.hs 45
fromList,  :: [a] > Seq a
+  ** Sequential construction
+ iterateN,  :: Int > (a > a) > a > Seq a
+ unfoldr,  :: (b > Maybe (a, b)) > b > Seq a
+ unfoldl,  :: (b > Maybe (b, a)) > b > Seq a
 * Deconstruction
  Additional functions for deconstructing sequences are available
 via the 'Foldable' instance of 'Seq'.
hunk ./Data/Sequence.hs 61
viewl,  :: Seq a > ViewL a
ViewR(..),
viewr,  :: Seq a > ViewR a
+  ** Scanning
+ scanl,  :: (a > b > a) > a > Seq b > Seq a
+ scanl1,  :: (a > a > a) > Seq a > Seq a
+ scanr,  :: (a > b > b) > b > Seq a > Seq b
+ scanr1,  :: (a > a > a) > Seq a > Seq a
+  ** Sublists
+ tails,  :: Seq a > Seq (Seq a)
+ inits,  :: Seq a > Seq (Seq a)
+ takeWhile,  :: (a > Bool) > Seq a > Seq a
+ takeWhileEnd,  :: (a > Bool) > Seq a > Seq a
+ dropWhile,  :: (a > Bool) > Seq a > Seq a
+ dropWhileEnd,  :: (a > Bool) > Seq a > Seq a
+ span,  :: (a > Bool) > Seq a > (Seq a, Seq a)
+ spanEnd,  :: (a > Bool) > Seq a > (Seq a, Seq a)
+ break,  :: (a > Bool) > Seq a > (Seq a, Seq a)
+ breakEnd,  :: (a > Bool) > Seq a > (Seq a, Seq a)
+ partition,  :: (a > Bool) > Seq a > (Seq a, Seq a)
+ filter,  :: (a > Bool) > Seq a > Seq a
+  ** Sorts
+ sort,  :: Ord a => Seq a > Seq a
+ sortBy,  :: (a > a > Ordering) > Seq a > Seq a
+ unstableSort,  :: Ord a => Seq a > Seq a
+ unstableSortBy,  :: (a > a > Ordering) > Seq a > Seq a
 ** Indexing
index,  :: Seq a > Int > a
adjust,  :: (a > a) > Int > Seq a > Seq a
hunk ./Data/Sequence.hs 91
take,  :: Int > Seq a > Seq a
drop,  :: Int > Seq a > Seq a
splitAt,  :: Int > Seq a > (Seq a, Seq a)
+  *** Indexing with predicates
+ elemIndex,  :: Eq a => a > Seq a > Maybe Int
+ elemIndices,  :: Eq a => a > Seq a > [Int]
+ elemLastIndex,  :: Eq a => a > Seq a > Maybe Ind
+ elemIndicesDesc, :: Eq a => a > Seq a > [Int]
+ findIndex,  :: (a > Bool) > Seq a > Maybe Int
+ findIndices,  :: (a > Bool) > Seq a > [Int]
+ findLastIndex,  :: (a > Bool) > Seq a > Maybe Int
+ findIndicesDesc, :: (a > Bool) > Seq a > [Int]
 * Transformations
hunk ./Data/Sequence.hs 101
+ mapWithIndex,  :: (Int > a > b) > Seq a > Seq b
reverse,  :: Seq a > Seq a
hunk ./Data/Sequence.hs 103
+  ** Zips
+ zip,  :: Seq a > Seq b > Seq (a, b)
+ zipWith,  :: (a > b > c) > Seq a > Seq b > Seq c
+ zip3,  :: Seq a > Seq b > Seq c > Seq (a, b, c)
+ zipWith3,  :: (a > b > c > d) > Seq a > Seq b > Seq c > Seq d
+ zip4,  :: Seq a > Seq b > Seq c > Seq d > Seq (a, b, c, d)
+ zipWith4,  :: (a > b > c > d > e) > Seq a > Seq b > Seq c > Seq d > Seq e
#if TESTING
valid,
#endif
hunk ./Data/Sequence.hs 116
) where
import Prelude hiding (
 null, length, take, drop, splitAt, foldl, foldl1, foldr, foldr1,
 reverse)
import qualified Data.List (foldl')
import Control.Applicative (Applicative(..), (<$>))
import Control.Monad (MonadPlus(..))
+ null, length, take, drop, splitAt, foldl, foldl1, foldr, foldr1, span,
+ scanl, scanl1, scanr, scanr1, replicate, zip, zipWith, zip3, zipWith3,
+ takeWhile, dropWhile, break, iterate, reverse, filter, mapM, all)
+import qualified Data.List (foldl', zipWith)
+import Control.Applicative (Applicative(..), (<$>), liftA, liftA2, liftA3)
+import Control.Monad (MonadPlus(..), ap, liftM, liftM2, liftM3, liftM4)
import Data.Monoid (Monoid(..))
import Data.Foldable
import Data.Traversable
hunk ./Data/Sequence.hs 131
import Data.Typeable (TyCon, Typeable1(..), mkTyCon, mkTyConApp )
#ifdef __GLASGOW_HASKELL__
+import GHC.Exts (build)
import Text.Read (Lexeme(Ident), lexP, parens, prec,
readPrec, readListPrec, readListPrecDefault)
import Data.Data (Data(..), DataType, Constr, Fixity(..),
hunk ./Data/Sequence.hs 139
#endif
#if TESTING
import Control.Monad (liftM, liftM3, liftM4)
import Test.QuickCheck
+import Test.QuickCheck hiding ((><))
#endif
infixr 5 `consTree`
hunk ./Data/Sequence.hs 144
infixl 5 `snocTree`
+infixr 5 `consDTree`
+infixl 6 `snocDTree`
infixr 5 ><
infixr 5 <, :<
hunk ./Data/Sequence.hs 298
traverse f sf
{# INLINE deep #}
{# SPECIALIZE deep :: Digit (Elem a) > FingerTree (Node (Elem a)) > Digit (Elem a) > FingerTree (Elem a) #}
{# SPECIALIZE deep :: Digit (Node a) > FingerTree (Node (Node a)) > Digit (Node a) > FingerTree (Node a) #}
+{# SPECIALIZE INLINE deep :: Digit (Elem a) > FingerTree (Node (Elem a)) > Digit (Elem a) > FingerTree (Elem a) #}
+{# SPECIALIZE INLINE deep :: Digit (Node a) > FingerTree (Node (Node a)) > Digit (Node a) > FingerTree (Node a) #}
deep :: Sized a => Digit a > FingerTree (Node a) > Digit a > FingerTree a
deep pr m sf = Deep (size pr + size m + size sf) pr m sf
hunk ./Data/Sequence.hs 303
+{# INLINE pullL #}
+pullL :: Sized a => Int > FingerTree (Node a) > Digit a > FingerTree a
+pullL s m sf = case viewLTree m of
+ Nothing2 > digitToTree' s sf
+ Just2 pr m' > Deep s (nodeToDigit pr) m' sf
+
+{# INLINE pullR #}
+pullR :: Sized a => Int > Digit a > FingerTree (Node a) > FingerTree a
+pullR s pr m = case viewRTree m of
+ Nothing2 > digitToTree' s pr
+ Just2 m' sf > Deep s pr m' (nodeToDigit sf)
+
+{# SPECIALIZE deepL :: Maybe (Digit (Elem a)) > FingerTree (Node (Elem a)) > Digit (Elem a) > FingerTree (Elem a) #}
+{# SPECIALIZE deepL :: Maybe (Digit (Node a)) > FingerTree (Node (Node a)) > Digit (Node a) > FingerTree (Node a) #}
+deepL :: Sized a => Maybe (Digit a) > FingerTree (Node a) > Digit a > FingerTree a
+deepL Nothing m sf = pullL (size m + size sf) m sf
+deepL (Just pr) m sf = deep pr m sf
+
+{# SPECIALIZE deepR :: Digit (Elem a) > FingerTree (Node (Elem a)) > Maybe (Digit (Elem a)) > FingerTree (Elem a) #}
+{# SPECIALIZE deepR :: Digit (Node a) > FingerTree (Node (Node a)) > Maybe (Digit (Node a)) > FingerTree (Node a) #}
+deepR :: Sized a => Digit a > FingerTree (Node a) > Maybe (Digit a) > FingerTree a
+deepR pr m Nothing = pullR (size m + size pr) pr m
+deepR pr m (Just sf) = deep pr m sf
+
 Digits
data Digit a
hunk ./Data/Sequence.hs 363
fmap = fmapDefault
instance Traversable Digit where
+ {# INLINE traverse #}
traverse f (One a) = One <$> f a
traverse f (Two a b) = Two <$> f a <*> f b
traverse f (Three a b c) = Three <$> f a <*> f b <*> f c
hunk ./Data/Sequence.hs 370
traverse f (Four a b c d) = Four <$> f a <*> f b <*> f c <*> f d
instance Sized a => Sized (Digit a) where
 {# SPECIALIZE instance Sized (Digit (Elem a)) #}
 {# SPECIALIZE instance Sized (Digit (Node a)) #}
 size xs = foldl (\ i x > i + size x) 0 xs
+ {# INLINE size #}
+ size = foldl1 (+) . fmap size
{# SPECIALIZE digitToTree :: Digit (Elem a) > FingerTree (Elem a) #}
{# SPECIALIZE digitToTree :: Digit (Node a) > FingerTree (Node a) #}
hunk ./Data/Sequence.hs 381
digitToTree (Three a b c) = deep (Two a b) Empty (One c)
digitToTree (Four a b c d) = deep (Two a b) Empty (Two c d)
+  Given the size of a digit and the digit itself, efficiently converts it to a FingerTree.
+digitToTree' :: Int > Digit a > FingerTree a
+digitToTree' n (Four a b c d) = Deep n (Two a b) Empty (Two c d)
+digitToTree' n (Three a b c) = Deep n (Two a b) Empty (One c)
+digitToTree' n (Two a b) = Deep n (One a) Empty (One b)
+digitToTree' n (One a) = n `seq` Single a
+
+
+
 Nodes
data Node a
hunk ./Data/Sequence.hs 407
foldl f z (Node3 _ a b c) = ((z `f` a) `f` b) `f` c
instance Functor Node where
+ {# INLINE fmap #}
fmap = fmapDefault
instance Traversable Node where
hunk ./Data/Sequence.hs 411
+ {# INLINE traverse #}
traverse f (Node2 v a b) = Node2 v <$> f a <*> f b
traverse f (Node3 v a b c) = Node3 v <$> f a <*> f b <*> f c
hunk ./Data/Sequence.hs 457
showsPrec p (Elem x) = showsPrec p x
#endif
+
+ Applicative construction
+
+
+newtype Id a = Id {runId :: a}
+
+instance Functor Id where
+ fmap f (Id x) = Id (f x)
+
+instance Monad Id where
+ return = Id
+ m >>= k = k (runId m)
+
+instance Applicative Id where
+ pure = return
+ (<*>) = ap
+
+  This is essentially a clone of Control.Monad.State.Strict.
+newtype State s a = State {runState :: s > (s, a)}
+
+instance Functor (State s) where
+ fmap = liftA
+
+instance Monad (State s) where
+ {# INLINE return #}
+ {# INLINE (>>=) #}
+ return x = State $ \ s > (s, x)
+ m >>= k = State $ \ s > case runState m s of
+ (s', x) > runState (k x) s'
+
+instance Applicative (State s) where
+ pure = return
+ (<*>) = ap
+
+execState :: State s a > s > a
+execState m x = snd (runState m x)
+
+  A helper method: a strict version of mapAccumL.
+mapAccumL' :: Traversable t => (a > b > (a, c)) > a > t b > (a, t c)
+mapAccumL' f s t = runState (traverse (State . flip f) t) s
+
+  'applicativeTree' takes an Applicativewrapped construction of a piece of a FingerTree, assumed
+ to always have the same size (which is put in the second argument), and replicates it as many times
+ as specified. This encapsulates the behavior of several procedures, most notably iterate and replicate.
+
+{# SPECIALIZE applicativeTree :: Int > Int > State s a > State s (FingerTree a) #}
+{# SPECIALIZE applicativeTree :: Int > Int > Id a > Id (FingerTree a) #}
+  Special note: the Id specialization automatically does node sharing, reducing memory usage of the
+  resulting tree to /O(log n)/.
+applicativeTree :: Applicative f => Int > Int > f a > f (FingerTree a)
+applicativeTree n mSize m = mSize `seq` case n of
+ 0 > pure Empty
+ 1 > liftA Single m
+ 2 > deepA one empty one
+ 3 > deepA two empty one
+ 4 > deepA two empty two
+ 5 > deepA three empty two
+ 6 > deepA three empty three
+ 7 > deepA four empty three
+ 8 > deepA four empty four
+ _ > let (q, r) = n `quotRem` 3 in q `seq` case r of
+ 0 > deepA three (applicativeTree (q  2) mSize' n3) three
+ 1 > deepA four (applicativeTree (q  2) mSize' n3) three
+ _ > deepA four (applicativeTree (q  2) mSize' n3) four
+ where one = liftA One m
+ two = liftA2 Two m m
+ three = liftA3 Three m m m
+ four = liftA3 Four m m m <*> m
+ deepA = liftA3 (Deep (n * mSize))
+ mSize' = 3 * mSize
+ n3 = liftA3 (Node3 mSize') m m m
+ empty = pure Empty
+

 Construction

hunk ./Data/Sequence.hs 542
singleton :: a > Seq a
singleton x = Seq (Single (Elem x))
+  /O(log n)/. @replicate n x@ is a sequence of length @n@ with @x@ the value of every element.
+replicate :: Int > a > Seq a
+replicate n x
+  n < 0 = error "replicate takes a nonnegative integer argument"
+  otherwise = Seq (runId (applicativeTree n 1 (Id (Elem x))))
+
  /O(1)/. Add an element to the left end of a sequence.
 Mnemonic: a triangle with the single element at the pointy end.
(<) :: a > Seq a > Seq a
hunk ./Data/Sequence.hs 644
appendTree1 xs a Empty =
xs `snocTree` a
appendTree1 (Single x) a xs =
 x `consTree` a `consTree` xs
+ Two x a `consDTree` xs
appendTree1 xs a (Single x) =
hunk ./Data/Sequence.hs 646
 xs `snocTree` a `snocTree` x
+ xs `snocDTree` Two a x
appendTree1 (Deep s1 pr1 m1 sf1) a (Deep s2 pr2 m2 sf2) =
Deep (s1 + size a + s2) pr1 (addDigits1 m1 sf1 a pr2 m2) sf2
hunk ./Data/Sequence.hs 686
appendTree2 :: FingerTree (Node a) > Node a > Node a > FingerTree (Node a) > FingerTree (Node a)
appendTree2 Empty a b xs =
 a `consTree` b `consTree` xs
+ Two a b `consDTree` xs
appendTree2 xs a b Empty =
hunk ./Data/Sequence.hs 688
 xs `snocTree` a `snocTree` b
+ xs `snocDTree` Two a b
appendTree2 (Single x) a b xs =
hunk ./Data/Sequence.hs 690
 x `consTree` a `consTree` b `consTree` xs
+ Three x a b `consDTree` xs
appendTree2 xs a b (Single x) =
hunk ./Data/Sequence.hs 692
 xs `snocTree` a `snocTree` b `snocTree` x
+ xs `snocDTree` Three a b x
appendTree2 (Deep s1 pr1 m1 sf1) a b (Deep s2 pr2 m2 sf2) =
Deep (s1 + size a + size b + s2) pr1 (addDigits2 m1 sf1 a b pr2 m2) sf2
hunk ./Data/Sequence.hs 732
appendTree3 :: FingerTree (Node a) > Node a > Node a > Node a > FingerTree (Node a) > FingerTree (Node a)
appendTree3 Empty a b c xs =
 a `consTree` b `consTree` c `consTree` xs
+ Three a b c `consDTree` xs
appendTree3 xs a b c Empty =
hunk ./Data/Sequence.hs 734
 xs `snocTree` a `snocTree` b `snocTree` c
+ xs `snocDTree` Three a b c
appendTree3 (Single x) a b c xs =
hunk ./Data/Sequence.hs 736
 x `consTree` a `consTree` b `consTree` c `consTree` xs
+ Four x a b c `consDTree` xs
appendTree3 xs a b c (Single x) =
hunk ./Data/Sequence.hs 738
 xs `snocTree` a `snocTree` b `snocTree` c `snocTree` x
+ xs `snocDTree` Four a b c x
appendTree3 (Deep s1 pr1 m1 sf1) a b c (Deep s2 pr2 m2 sf2) =
Deep (s1 + size a + size b + size c + s2) pr1 (addDigits3 m1 sf1 a b c pr2 m2) sf2
hunk ./Data/Sequence.hs 778
appendTree4 :: FingerTree (Node a) > Node a > Node a > Node a > Node a > FingerTree (Node a) > FingerTree (Node a)
appendTree4 Empty a b c d xs =
 a `consTree` b `consTree` c `consTree` d `consTree` xs
+ Four a b c d `consDTree` xs
appendTree4 xs a b c d Empty =
hunk ./Data/Sequence.hs 780
 xs `snocTree` a `snocTree` b `snocTree` c `snocTree` d
+ xs `snocDTree` Four a b c d
appendTree4 (Single x) a b c d xs =
hunk ./Data/Sequence.hs 782
 x `consTree` a `consTree` b `consTree` c `consTree` d `consTree` xs
+ x `consTree` Four a b c d `consDTree` xs
appendTree4 xs a b c d (Single x) =
hunk ./Data/Sequence.hs 784
 xs `snocTree` a `snocTree` b `snocTree` c `snocTree` d `snocTree` x
+ xs `snocDTree` Four a b c d `snocTree` x
appendTree4 (Deep s1 pr1 m1 sf1) a b c d (Deep s2 pr2 m2 sf2) =
Deep (s1 + size a + size b + size c + size d + s2) pr1 (addDigits4 m1 sf1 a b c d pr2 m2) sf2
hunk ./Data/Sequence.hs 822
addDigits4 m1 (Four a b c d) e f g h (Four i j k l) m2 =
appendTree4 m1 (node3 a b c) (node3 d e f) (node3 g h i) (node3 j k l) m2
+{# INLINE consDTree #}
+ Cons an entire digit to a tree.
+consDTree :: Sized a => Digit a > FingerTree a > FingerTree a
+consDTree dig t = foldr consTree t dig
+
+{# INLINE snocDTree #}
+ Snoc and entire digit to a tree.
+snocDTree :: Sized a => FingerTree a > Digit a > FingerTree a
+snocDTree t dig = foldl snocTree t dig
+
+  Builds a sequence from a seed value. Takes time linear in the number of generated elements. /WARNING: If the number of generated elements is infinite, this method will not terminate./
+unfoldr :: (b > Maybe (a, b)) > b > Seq a
+unfoldr f b = unfoldr' empty b where
+  uses tail recursion rather than, for instance, the List implementation.
+ unfoldr' as b = maybe as (\ (a, b') > unfoldr' (as > a) b') (f b)
+
+  @'unfoldl' f x@ is equivalent to @'reverse' ('unfoldr' f x)@.
+unfoldl :: (b > Maybe (b, a)) > b > Seq a
+unfoldl f b = unfoldl' empty b where
+ unfoldl' as b = maybe as (\ (b', a) > unfoldl' (a < as) b') (f b)
+
+  /O(n)/. Constructs a sequence by repeated application of a function to a seed value.
+
+ > iterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
+iterateN :: Int > (a > a) > a > Seq a
+iterateN n f x
+  n < 0 = error "iterateN takes a nonnegative integer argument"
+  otherwise = Seq (execState (applicativeTree n 1 run) x)
+ where run = State $ \ x > (f x, Elem x)
+

 Deconstruction

hunk ./Data/Sequence.hs 917
viewLTree :: Sized a => FingerTree a > Maybe2 a (FingerTree a)
viewLTree Empty = Nothing2
viewLTree (Single a) = Just2 a Empty
viewLTree (Deep s (One a) m sf) = Just2 a (case viewLTree m of
 Nothing2 > digitToTree sf
 Just2 b m' > Deep (s  size a) (nodeToDigit b) m' sf)
+viewLTree (Deep s (One a) m sf) = Just2 a (pullL (s  size a) m sf)
viewLTree (Deep s (Two a b) m sf) =
Just2 a (Deep (s  size a) (One b) m sf)
viewLTree (Deep s (Three a b c) m sf) =
hunk ./Data/Sequence.hs 954
foldr f z (xs :> x) = foldr f (f x z) xs
foldl _ z EmptyR = z
 foldl f z (xs :> x) = f (foldl f z xs) x
+ foldl f z (xs :> x) = foldl f z xs `f` x
foldr1 _ EmptyR = error "foldr1: empty view"
foldr1 f (xs :> x) = foldr f x xs
hunk ./Data/Sequence.hs 974
viewRTree :: Sized a => FingerTree a > Maybe2 (FingerTree a) a
viewRTree Empty = Nothing2
viewRTree (Single z) = Just2 Empty z
viewRTree (Deep s pr m (One z)) = Just2 (case viewRTree m of
 Nothing2 > digitToTree pr
 Just2 m' y > Deep (s  size z) pr m' (nodeToDigit y)) z
+viewRTree (Deep s pr m (One z)) = Just2 (pullR (s  size z) pr m) z
viewRTree (Deep s pr m (Two y z)) =
Just2 (Deep (s  size z) pr m (One y)) z
viewRTree (Deep s pr m (Three x y z)) =
hunk ./Data/Sequence.hs 982
viewRTree (Deep s pr m (Four w x y z)) =
Just2 (Deep (s  size z) pr m (Three w x y)) z
+
+ Scans
+
+ These are not particularly complex applications of the Traversable
+ functor, though making the correspondence with Data.List exact
+ requires the use of (<) and (>).
+
+ Note that save for the single (<) or (>), we maintain the original
+ structure of the Seq, not having to do any restructuring of our own.
+
+ wasserman.louis@gmail.com, 5/23/09
+
+
+  'scanl' is similar to 'foldl', but returns a sequence of reduced values from the left:
+
+ > scanl f z (fromList [x1, x2, ...]) = fromList [z, z `f` x1, (z `f` x1) `f` x2, ...]
+scanl :: (a > b > a) > a > Seq b > Seq a
+scanl f z0 xs = z0 < snd (mapAccumL (\ x z > let x' = f x z in (x', x')) z0 xs)
+
+  'scanl1' is a variant of 'scanl' that has no starting value argument:
+
+ > scanl1 f (fromList [x1, x2, ...]) = fromList [x1, x1 `f` x2, ...]
+scanl1 :: (a > a > a) > Seq a > Seq a
+scanl1 f xs = case viewl xs of
+ EmptyL > error "scanl1 takes a nonempty sequence as an argument"
+ x :< xs' > scanl f x xs'
+
+  'scanr' is the righttoleft dual of 'scanl'.
+scanr :: (a > b > b) > b > Seq a > Seq b
+scanr f z0 xs = snd (mapAccumR (\ z x > let z' = f x z in (z', z')) z0 xs) > z0
+
+  'scanr1' is a variant of 'scanr' that has no starting value argument.
+scanr1 :: (a > a > a) > Seq a > Seq a
+scanr1 f xs = case viewr xs of
+ EmptyR > error "scanr1 takes a nonempty sequence as an argument"
+ xs' :> x > scanr f x xs'
+
 Indexing
  /O(log(min(i,ni)))/. The element at the specified position,
hunk ./Data/Sequence.hs 1147
sab = sa + size b
sabc = sab + size c
+  A generalization of 'fmap', 'mapWithIndex' takes a mapping function that also depends on the element's
+ index, and applies it to every element in the sequence.
+mapWithIndex :: (Int > a > b) > Seq a > Seq b
+mapWithIndex f xs = snd (mapAccumL' (\ i x > (i + 1, f i x)) 0 xs)
+
 Splitting
  /O(log(min(i,ni)))/. The first @i@ elements of a sequence.
hunk ./Data/Sequence.hs 1197
Split l x r > Split (maybe Empty digitToTree l) x (deepL r m sf)
 i < spm = case splitTree im m of
Split ml xs mr > case splitNode (im  size ml) xs of
 Split l x r > Split (deepR pr ml l) x (deepL r mr sf)
+ Split l x r > Split (deepR pr ml l) x (deepL r mr sf)
 otherwise = case splitDigit (i  spm) sf of
hunk ./Data/Sequence.hs 1199
 Split l x r > Split (deepR pr m l) x (maybe Empty digitToTree r)
+ Split l x r > Split (deepR pr m l) x (maybe Empty digitToTree r)
where spr = size pr
spm = spr + size m
im = i  spr
hunk ./Data/Sequence.hs 1204
{# SPECIALIZE deepL :: Maybe (Digit (Elem a)) > FingerTree (Node (Elem a)) > Digit (Elem a) > FingerTree (Elem a) #}
{# SPECIALIZE deepL :: Maybe (Digit (Node a)) > FingerTree (Node (Node a)) > Digit (Node a) > FingerTree (Node a) #}
deepL :: Sized a => Maybe (Digit a) > FingerTree (Node a) > Digit a > FingerTree a
deepL Nothing m sf = case viewLTree m of
 Nothing2 > digitToTree sf
 Just2 a m' > Deep (size m + size sf) (nodeToDigit a) m' sf
deepL (Just pr) m sf = deep pr m sf

{# SPECIALIZE deepR :: Digit (Elem a) > FingerTree (Node (Elem a)) > Maybe (Digit (Elem a)) > FingerTree (Elem a) #}
{# SPECIALIZE deepR :: Digit (Node a) > FingerTree (Node (Node a)) > Maybe (Digit (Node a)) > FingerTree (Node a) #}
deepR :: Sized a => Digit a > FingerTree (Node a) > Maybe (Digit a) > FingerTree a
deepR pr m Nothing = case viewRTree m of
 Nothing2 > digitToTree pr
 Just2 m' a > Deep (size pr + size m) pr m' (nodeToDigit a)
deepR pr m (Just sf) = deep pr m sf

{# SPECIALIZE splitNode :: Int > Node (Elem a) > Split (Maybe (Digit (Elem a))) (Elem a) #}
{# SPECIALIZE splitNode :: Int > Node (Node a) > Split (Maybe (Digit (Node a))) (Node a) #}
splitNode :: Sized a => Int > Node a > Split (Maybe (Digit a)) a
hunk ./Data/Sequence.hs 1240
where sa = size a
sab = sa + size b
sabc = sab + size c
+
+  /O(n)/. Returns a sequence of all suffixes of this sequence, longest first. For example,
+
+ > tails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]
+
+ Evaluating the /i/th suffix takes /O(log(min(i, ni)))/, but evaluating every suffix in the sequence
+ takes /O(n)/ due to sharing.
+tails :: Seq a > Seq (Seq a)
+tails (Seq xs) = Seq (tailsTree (Elem . Seq) xs) > empty
+
+  /O(n)/. Returns a sequence of all prefixes of this sequence, shortest first. For example,
+
+ > inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
+
+ Evaluating the /i/th prefix takes /O(log(min(i, ni)))/, but evaluating every prefix in the sequence
+ takes /O(n)/ due to sharing.
+inits :: Seq a > Seq (Seq a)
+inits (Seq xs) = empty < Seq (initsTree (Elem . Seq) xs)
+
+ This implementation of tails (and, analogously, inits) has the following algorithmic advantages:
+ Evaluating each tail in the sequence takes linear total time, which is better than we could say for
+ @fromList [drop n xs  n < [0..length xs]]@.
+ Evaluating any individual tail takes logarithmic time, which is better than we can say for either
+ @scanr (<) empty xs@ or @iterateN (length xs + 1) (\ xs > let _ :< xs' = viewl xs in xs') xs@.
+
+ Moreover, if we actually look at every tail in the sequence, the following benchmarks demonstrate that
+ this implementation is modestly faster than any of the above:
+
+ Times (ms)
+ min mean +/sd median max
+ Seq.tails: 23.022 24.815 3.857 23.508 47.535
+ scanr: 88.141 92.531 6.431 89.298 116.264
+ iterateN: 30.747 31.210 0.320 31.181 32.301
+
+ The algorithm for tails (and, analogously, inits) is as follows:
+
+ A Node in the FingerTree of tails is constructed by evaluating the corresponding tail of the FingerTree
+ of Nodes, considering the first Node in this tail, and constructing a Node in which each tail of this
+ Node is made to be the prefix of the remaining tree. This ends up working quite elegantly, as the remainder of
+ the tail of the FingerTree of Nodes becomes the middle of a new tail, the suffix of the Node is the
+ prefix, and the suffix of the original tree is retained.
+
+ In particular, evaluating the /i/th tail involves making as many partial evaluations as the Node depth of
+ the /i/th element. In addition, when we evaluate the /i/th tail, and we also evaluate the /j/th tail,
+ and /m/ Nodes are on the path to both /i/ and /j/, each of those /m/ evaluations are shared between
+ the computation of the /i/th and /j/th tails.
+
+ wasserman.louis@gmail.com, 7/16/09
+
+{# INLINE scanlSize #}
+scanlSize :: (Traversable f, Sized a) => (b > Int > b) > b > f a > f b
+scanlSize f z d = snd (mapAccumL (\ acc x > let ans = f acc (size x) in (ans, ans)) z d)
+
+{# INLINE scanrSize #}
+scanrSize :: (Traversable f, Sized a) => (Int > b > b) > b > f a > f b
+scanrSize f z d = snd (mapAccumR (\ acc x > let ans = size x `f` acc in (ans, ans)) z d)
+
+{# INLINE zipDigit #}
+zipDigit :: (a > b > c) > Digit a > Digit b > Digit c
+zipDigit f (One a) n@(One x)
+ = foldr seq (One (f a x)) n
+zipDigit f (Two a b) n@(Two x y)
+ = foldr seq (Two (f a x) (f b y)) n
+zipDigit f (Three a b c) n@(Three x y z)
+ = foldr seq (Three (f a x) (f b y) (f c z)) n
+zipDigit f (Four a b c d) n@(Four x y z w)
+ = foldr seq (Four (f a x) (f b y) (f c z) (f d w)) n
+zipDigit _ _ _ = undefined
+
+tailsDigit :: Digit a > Digit (Digit a)
+tailsDigit (One a) = One (One a)
+tailsDigit (Two a b) = Two (Two a b) (One b)
+tailsDigit (Three a b c) = Three (Three a b c) (Two b c) (One c)
+tailsDigit (Four a b c d) = Four (Four a b c d) (Three b c d) (Two c d) (One d)
+
+initsDigit :: Digit a > Digit (Digit a)
+initsDigit (One a) = One (One a)
+initsDigit (Two a b) = Two (One a) (Two a b)
+initsDigit (Three a b c) = Three (One a) (Two a b) (Three a b c)
+initsDigit (Four a b c d) = Four (One a) (Two a b) (Three a b c) (Four a b c d)
+
+ Assumes that the two nodes are the same size.
+zipNode :: (a > b > c) > Node a > Node b > Node c
+zipNode f (Node2 s a b) n@(Node2 _ x y) = foldr seq (Node2 s (f a x) (f b y)) n
+zipNode f (Node3 s a b c) n@(Node3 _ x y z) = foldr seq (Node3 s (f a x) (f b y) (f c z)) n
+zipNode _ _ _ = undefined
+
+tailsNode :: Node a > Node (Digit a)
+tailsNode (Node2 s a b) = Node2 s (Two a b) (One b)
+tailsNode (Node3 s a b c) = Node3 s (Three a b c) (Two b c) (One c)
+
+initsNode :: Node a > Node (Digit a)
+initsNode (Node2 s a b) = Node2 s (One a) (Two a b)
+initsNode (Node3 s a b c) = Node3 s (One a) (Two a b) (Three a b c)
+
+{# SPECIALIZE tailsTree :: (FingerTree (Elem a) > Elem b) > FingerTree (Elem a) > FingerTree (Elem b) #}
+{# SPECIALIZE tailsTree :: (FingerTree (Node a) > Node b) > FingerTree (Node a) > FingerTree (Node b) #}
+  Given a function to apply to tails of a tree, applies that function to every tail of the specified tree.
+tailsTree :: (Sized a, Sized b) => (FingerTree a > b) > FingerTree a > FingerTree b
+tailsTree _ Empty = Empty
+tailsTree f (Single x) = Single (f (Single x))
+tailsTree f (Deep n pr m sf) =
+ Deep n (fmap f $ zipDigit (withPr m) (tailsDigit pr) (scanlSize () n pr))
+ (tailsTree (f' $! size sf) m)
+ (fmap f $ zipDigit (flip digitToTree') (tailsDigit sf) (scanrSize (+) 0 sf))
+ where withPr m pr sz = Deep sz pr m sf
+ f' sfSize ms = let Just2 node m' = viewLTree ms in
+ fmap f $! zipNode (withPr m') (tailsNode node) (scanrSize (+) (size m' + sfSize) node)
+
+{# SPECIALIZE initsTree :: (FingerTree (Elem a) > Elem b) > FingerTree (Elem a) > FingerTree (Elem b) #}
+{# SPECIALIZE initsTree :: (FingerTree (Node a) > Node b) > FingerTree (Node a) > FingerTree (Node b) #}
+  Given a function to apply to inits of a tree, applies that function to every init of the specified tree.
+initsTree :: (Sized a, Sized b) => (FingerTree a > b) > FingerTree a > FingerTree b
+initsTree _ Empty = Empty
+initsTree f (Single x) = Single (f (Single x))
+initsTree f (Deep n pr m sf) =
+ Deep n (fmap f $ zipDigit (flip digitToTree') (initsDigit pr) (scanlSize (+) 0 pr))
+ (initsTree (f' $! size pr) m)
+ (fmap f $ zipDigit (withSf m) (initsDigit sf) (scanrSize subtract n sf))
+ where withSf m sf sz = Deep sz pr m sf
+ f' prSize ms = let Just2 m' node = viewRTree ms in
+ fmap f $! zipNode (withSf m') (initsNode node) (scanlSize (+) (prSize + size m') node)
hunk ./Data/Sequence.hs 1363
+{# INLINE [1] foldIndices #}
+  foldIndices encapsulates a common schema of predicatesearching functions. In particular, it
+ fold every element satisfying p, from left to right.
+foldIndices :: (a > Bool) > Seq a > (Int > b > b) > b > b
+foldIndices p xs f z0 =
+ foldr (\ x z n > n `seq` if p x then f n (z (n+1)) else z (n+1))
+ (const z0) xs 0
+
+{# INLINE [1] foldIndices' #}
+  foldIndices' folds in every satisfying value, from right to left.
+foldIndices' :: (a > Bool) > Seq a > (Int > b > b) > b > b
+foldIndices' p xs f z0 =
+ foldl (\ z x n > n `seq` if p x then f n (z (n1)) else z (n1))
+ (const z0) xs (length xs  1)
+
+{# INLINE [0] appFI #}
+appFI :: (a > b) > a > b
+appFI = ($)
+
+{# INLINE [0] constFI #}
+ Specialized version of const for foldIndices rule matching.
+constFI :: (a > b) > a > c > b
+constFI = (const.)
+
+{# RULES
+ "foldIndices" forall f p xs g z0 . appFI f (foldIndices p xs (constFI g) z0) =
+ foldIndices p xs (constFI (f . g)) (f z0);
+ "foldIndices'" forall f p xs g z0 . appFI f (foldIndices' p xs (constFI g) z0) =
+ foldIndices' p xs (constFI (f . g)) (f z0);
+ #}
+
+  /O(i)/ where /i/ is the prefix length. 'takeWhile', applied to a predicate @p@ and a sequence @xs@, returns the
+ longest prefix (possibly empty) of @xs@ of elements that satisfy @p@.
+takeWhile :: (a > Bool) > Seq a > Seq a
+takeWhile p = appFI fst . span p
+
+  /O(i)/ where /i/ is the suffix length. 'takeWhileEnd', applied to a predicate @p@ and a sequence @xs@, returns
+ the longest suffix (possibly empty) of @xs@ of elements that satisfy @p@.
+
+ @'takeWhileEnd' p xs@ is equivalent to @'reverse' ('takeWhile' p ('reverse' xs))@.
+takeWhileEnd :: (a > Bool) > Seq a > Seq a
+takeWhileEnd p = appFI fst . span p
+
+  /O(i)/ where /i/ is the prefix length. @'dropWhile' p xs@ returns the suffix remaining after @takeWhile p xs@.
+dropWhile :: (a > Bool) > Seq a > Seq a
+dropWhile p = appFI snd . span p
+
+  /O(i)/ where /i/ is the suffix length. @'dropWhileEnd' p xs@ returns the prefix remaining after @takeWhileEnd p xs@.
+
+ @'dropWhileEnd' p xs@ is equivalent to @'reverse' ('dropWhile' p ('reverse' xs))@.
+dropWhileEnd :: (a > Bool) > Seq a > Seq a
+dropWhileEnd p = appFI snd . spanEnd p
+
+{# INLINE [~1] span #}
+  /O(i)/ where /i/ is the prefix length. 'span', applied to a predicate @p@ and a sequence @xs@, returns a tuple
+ whose first element is the longest prefix (possibly empty) of @xs@ of elements that satisfy @p@ and the second
+ element is the remainder of the sequence.
+span :: (a > Bool) > Seq a > (Seq a, Seq a)
+span p xs = appFI (maybe (xs, empty) (`splitAt` xs)) (findIndex (not . p) xs)
+
+{# INLINE [~1] spanEnd #}
+  /O(i)/ where /i/ is the suffix length. 'spanEnd', applied to a predicate @p@ and a sequence @xs@, returns a tuple
+ whose /first/ element is the longest /suffix/ (possibly empty) of @xs@ of elements that satisfy @p@ and the second
+ element is the remainder of the sequence.
+spanEnd :: (a > Bool) > Seq a > (Seq a, Seq a)
+spanEnd p xs = appFI (maybe (xs, empty) (flipPair . (`splitAt` xs))) (findLastIndex (not . p) xs)
+ where flipPair (x, y) = (y, x)
+
+  /O(i)/ where /i/ is the breakpoint index. 'break', applied to a predicate @p@ and a sequence @xs@, returns a tuple
+ whose first element is the longest prefix (possibly empty) of @xs@ of elements that /do not satisfy/ @p@ and the
+ second element is the remainder of the sequence.
+
+ @'break' p@ is equivalent to @'span' (not . p)@.
+break :: (a > Bool) > Seq a > (Seq a, Seq a)
+break p = span (not . p)
+
+  @'breakEnd' p@ is equivalent to @'spanEnd' (not . p)@.
+breakEnd :: (a > Bool) > Seq a > (Seq a, Seq a)
+breakEnd p = spanEnd (not . p)
+
+  /O(n)/. The 'partition' function takes a predicate @p@ and a sequence @xs@ and returns sequences of those
+ elements which do and do not satisfy the predicate.
+partition :: (a > Bool) > Seq a > (Seq a, Seq a)
+partition p = foldl part (empty, empty) where
+ part (xs, ys) x = if p x then (xs > x, ys) else (xs, ys > x)
+
+  /O(n)/. The 'filter' function takes a predicate @p@ and a sequence @xs@ and returns a sequence of those
+ elements which satisfy the predicate.
+filter :: (a > Bool) > Seq a > Seq a
+filter p = foldl (\ xs x > if p x then xs > x else xs) empty
+
+ Indexing sequences
+
+  'elemIndex' finds the first index of the specified element, if it is present.
+elemIndex :: Eq a => a > Seq a > Maybe Int
+elemIndex x = findIndex (x ==)
+
+  'elemLastIndex' finds the last index of the specified element, if it is present.
+elemLastIndex :: Eq a => a > Seq a > Maybe Int
+elemLastIndex x = findLastIndex (x ==)
+
+{# INLINE elemIndices #}
+  'elemIndices' finds the indices of the specified element, in ascending order.
+elemIndices :: Eq a => a > Seq a > [Int]
+elemIndices x = findIndices (x ==)
+
+{# INLINE elemIndicesDesc #}
+  'elemIndicesDesc' finds the indices of the specified element, in descending order.
+elemIndicesDesc :: Eq a => a > Seq a > [Int]
+elemIndicesDesc x = findIndicesDesc (x ==)
+
+{# INLINE findIndex #}
+  @'findIndex' p xs@ finds the index of the first element that satisfies @p@, if any exist.
+findIndex :: (a > Bool) > Seq a > Maybe Int
+findIndex p xs = foldIndices p xs (constFI Just) Nothing
+
+{# INLINE findLastIndex #}
+  @'findLastIndex' p xs@ finds the index of the last element that satisfies @p@, if any exist.
+findLastIndex :: (a > Bool) > Seq a > Maybe Int
+findLastIndex p xs = foldIndices' p xs (constFI Just) Nothing
+
+{# INLINE findIndices #}
+  @'findIndices' p@ finds all indices of elements that satisfy @p@, in ascending order.
+findIndices :: (a > Bool) > Seq a > [Int]
+#if __GLASGOW_HASKELL__
+findIndices p xs = build (foldIndices p xs)
+#else
+findIndices p xs = foldIndices p xs (:) []
+#endif
+
+{# INLINE findIndicesDesc #}
+  @'findIndicesDesc' p@ finds all indices of elements that satisfy @p@, in descending order.
+findIndicesDesc :: (a > Bool) > Seq a > [Int]
+#if __GLASGOW_HASKELL__
+findIndicesDesc p xs = build (foldIndices' p xs)
+#else
+findIndicesDesc p xs = foldIndices' p xs (:) []
+#endif
+

 Lists

hunk ./Data/Sequence.hs 1528
(reverseTree (reverseNode f) m)
(reverseDigit f pr)
+{# INLINE reverseDigit #}
reverseDigit :: (a > a) > Digit a > Digit a
reverseDigit f (One a) = One (f a)
reverseDigit f (Two a b) = Two (f b) (f a)
hunk ./Data/Sequence.hs 1539
reverseNode f (Node2 s a b) = Node2 s (f b) (f a)
reverseNode f (Node3 s a b c) = Node3 s (f c) (f b) (f a)
+
+ Zipping
+
+
+  /O(n)/. 'zip' takes two sequences and returns a sequence of corresponding pairs.
+ If one input is short, excess elements of the longer sequence are discarded.
+zip :: Seq a > Seq b > Seq (a, b)
+zip = zipWith (,)
+
+  /O(n)/. 'zipWith' generalizes 'zip' by zipping with the function given as the first argument,
+ instead of a tupling function. For example, @zipWith (+)@ is applied to two sequences to take
+ the sequence of corresponding sums.
+zipWith :: (a > b > c) > Seq a > Seq b > Seq c
+zipWith f xs ys
+  length xs <= length ys = zipWith' f xs ys
+  otherwise = zipWith' (flip f) ys xs
+ where zipWith' f xs ys = snd (mapAccumL ((\ (y :< ys) x > (ys, f x y)) . viewl) ys xs)
+
+zip3 :: Seq a > Seq b > Seq c > Seq (a,b,c)
+zip3 = zipWith3 (,,)
+
+zipWith3 :: (a > b > c > d) > Seq a > Seq b > Seq c > Seq d
+zipWith3 f s1 s2 s3 = zipWith ($) (zipWith f s1 s2) s3
+
+zip4 :: Seq a > Seq b > Seq c > Seq d > Seq (a,b,c,d)
+zip4 = zipWith4 (,,,)
+
+zipWith4 :: (a > b > c > d > e) > Seq a > Seq b > Seq c > Seq d > Seq e
+zipWith4 f s1 s2 s3 s4 = zipWith ($) (zipWith ($) (zipWith f s1 s2) s3) s4
+
+
+ Sorting
+
+ sort and sortBy are implemented by simple deforestations of
+ \ xs > fromList2 (length xs) . Data.List.sortBy cmp . toList
+ which does not get deforested automatically, it would appear.
+
+ Unstable sorting is performed by a heap sort implementation based on pairing heaps. Because the
+ internal structure of sequences is quite varied, it is difficult to get blocks of elements of
+ roughly the same length, which would improve merge sort performance. Pairing heaps, on the other
+ hand, are relatively resistant to the effects of merging heaps of wildly different sizes, as
+ guaranteed by its amortized constanttime merge operation. Moreover, extensive use of SpecConstr
+ transformations can be done on pairing heaps, especially when we're only constructing them
+ to immediately be unrolled.
+
+ On purely random sequences of length 50000, with no RTS options, I get the following statistics,
+ in which heapsort is about 42.5% faster:
+
+ Times (ms) min mean +/sd median max
+ to/from list: 103.802 108.572 7.487 106.436 143.339
+ unstable heapsort: 60.686 62.968 4.275 61.187 79.151
+
+ Heapsort, it would seem, is less of a memory hog than Data.List.sortBy. The gap is narrowed
+ when more memory is available, but heapsort still wins, 15% faster, with +RTS H128m:
+
+ Times (ms) min mean +/sd median max
+ to/from list: 42.692 45.074 2.596 44.600 56.601
+ unstable heapsort: 37.100 38.344 3.043 37.715 55.526
+
+ In addition, on strictly increasing sequences the gap is even wider than normal; heapsort is
+ 68.5% faster with no RTS options:
+ Times (ms) min mean +/sd median max
+ to/from list: 52.236 53.574 1.987 53.034 62.098
+ unstable heapsort: 16.433 16.919 0.931 16.681 21.622
+
+ This may be attributed to the elegant nature of the pairing heap.
+
+ wasserman.louis@gmail.com, 7/20/09
+
+
+  /O(n log n)/. 'sort' sorts the specified 'Seq' by the natural ordering of its elements. The sort is stable.
+ If a stable sort is not required, 'unstableSort' can be considerably faster, and in particular uses less memory.
+sort :: Ord a => Seq a > Seq a
+sort = sortBy compare
+
+  /O(n log n)/. 'sortBy' sorts the specified 'Seq' according to the specified comparator. The sort is stable.
+ If a stable sort is not required, 'unstableSortBy' can be considerably faster, and in particular uses less memory.
+sortBy :: (a > a > Ordering) > Seq a > Seq a
+ fromList . Data.List.sortBy cmp . toList doesn't actually deforest well, so I did so manually and got a moderate
+ performance boost.
+sortBy cmp xs = case foldr (\ x > ([x]:)) [] xs of
+ [] > empty
+ ys:yss > fromList2 (length xs) (merger0 ys yss)
+ where xs@(x:xs1) <> ys@(y:ys1) = case cmp x y of
+ GT > y:(xs <> ys1)
+ _ > x:(xs1 <> ys)
+ [] <> ys = ys
+ xs <> [] = xs
+ merger (xs1:xs2:xss) = (xs1 <> xs2) : merger xss
+ merger xss = xss
+ merger0 xs1 (xs2:xss) = merger0 (xs1 <> xs2) (merger xss)
+ merger0 xs [] = xs
+
+  /O(n log n)/. 'unstableSort' sorts the specified 'Seq' by the natural ordering of its elements, but the sort is not stable.
+ This algorithm is frequently faster and uses less memory than 'sort', and performs extremely well  frequently twice as fast as
+ 'sort'  when the sequence is already nearly sorted.
+unstableSort :: Ord a => Seq a > Seq a
+unstableSort = unstableSortBy compare
+
+  /O(n log n)/. A generalization of 'unstableSort', 'unstableSortBy' takes an arbitrary comparator and sorts the specified sequence.
+ The sort is not stable. This algorithm is frequently faster and uses less memory than 'sortBy', and performs extremely well 
+ frequently twice as fast as 'sortBy'  when the sequence is already nearly sorted.
+unstableSortBy :: (a > a > Ordering) > Seq a > Seq a
+unstableSortBy cmp (Seq xs) = fromList2 (size xs) $ maybe [] (unrollPQ cmp) $ toPQ cmp (\ (Elem x) > PQueue x Nil) xs
+
+fromList2 :: Int > [a] > Seq a
+ fromList2, given a list and its length, constructs a completely balanced Seq whose elements are that list
+ using the applicativeTree generalization.
+fromList2 n xs = Seq (execState (applicativeTree n 1 (State (\ (x:xs) > (xs, Elem x)))) xs)
+
+  A 'PQueue' is a simple pairing heap.
+data PQueue e = PQueue e (PQL e)
+data PQL e = Nil  {# UNPACK #} !(PQueue e) :& PQL e
+
+infixr 8 :&
+
+#if TESTING
+
+instance Functor PQueue where
+ fmap f (PQueue x ts) = PQueue (f x) (fmap f ts)
+
+instance Functor PQL where
+ fmap f (q :& qs) = fmap f q :& fmap f qs
+ fmap _ Nil = Nil
+
+instance Show e => Show (PQueue e) where
+ show = unlines . draw . fmap show
+
+ borrowed wholesale from Data.Tree, as Data.Tree actually depends on Data.Sequence
+draw :: PQueue String > [String]
+draw (PQueue x ts0) = x : drawSubTrees ts0
+ where drawSubTrees Nil = []
+ drawSubTrees (t :& Nil) =
+ "" : shift "` " " " (draw t)
+ drawSubTrees (t :& ts) =
+ "" : shift "+ " " " (draw t) ++ drawSubTrees ts
+
+ shift first other = Data.List.zipWith (++) (first : repeat other)
+#endif
+
+  'unrollPQ', given a comparator function, unrolls a 'PQueue' into a sorted list.
+unrollPQ :: (e > e > Ordering) > PQueue e > [e]
+unrollPQ cmp = unrollPQ' where
+ {# INLINE unrollPQ' #}
+ unrollPQ' (PQueue x ts) = x:mergePQs0 ts
+ (<>) = mergePQ cmp
+ mergePQs0 Nil = []
+ mergePQs0 (t :& Nil) = unrollPQ' t
+ mergePQs0 (t1 :& t2 :& ts) = mergePQs (t1 <> t2) ts
+ mergePQs t ts = t `seq` case ts of
+ Nil > unrollPQ' t
+ t1 :& Nil > unrollPQ' (t <> t1)
+ t1 :& t2 :& ts > mergePQs (t <> (t1 <> t2)) ts
+
+  'toPQ', given an ordering function and a mechanism for queueifying elements, converts a 'FingerTree' to a 'PQueue'.
+toPQ :: (e > e > Ordering) > (a > PQueue e) > FingerTree a > Maybe (PQueue e)
+toPQ _ _ Empty = Nothing
+toPQ _ f (Single x) = Just (f x)
+toPQ cmp f (Deep _ pr m sf) = Just (maybe (pr' <> sf') ((pr' <> sf') <>) (toPQ cmp fNode m)) where
+ fDigit d = case fmap f d of
+ One a > a
+ Two a b > a <> b
+ Three a b c > a <> b <> c
+ Four a b c d > (a <> b) <> (c <> d)
+ (<>) = mergePQ cmp
+ fNode = fDigit . nodeToDigit
+ pr' = fDigit pr
+ sf' = fDigit sf
+
+  'mergePQ' merges two 'PQueue's.
+mergePQ :: (a > a > Ordering) > PQueue a > PQueue a > PQueue a
+mergePQ cmp q1@(PQueue x1 ts1) q2@(PQueue x2 ts2)
+  cmp x1 x2 == GT = PQueue x2 (q1 :& ts2)
+  otherwise = PQueue x1 (q2 :& ts1)
+
#if TESTING

hunk ./Data/Sequence.hs 1722
instance Arbitrary a => Arbitrary (Seq a) where
arbitrary = liftM Seq arbitrary
 coarbitrary (Seq x) = coarbitrary x
+ shrink (Seq x) = map Seq (shrink x)
instance Arbitrary a => Arbitrary (Elem a) where
arbitrary = liftM Elem arbitrary
hunk ./Data/Sequence.hs 1726
 coarbitrary (Elem x) = coarbitrary x
instance (Arbitrary a, Sized a) => Arbitrary (FingerTree a) where
arbitrary = sized arb
hunk ./Data/Sequence.hs 1734
arb 1 = liftM Single arbitrary
arb n = liftM3 deep arbitrary (arb (n `div` 2)) arbitrary
 coarbitrary Empty = variant 0
 coarbitrary (Single x) = variant 1 . coarbitrary x
 coarbitrary (Deep _ pr m sf) =
 variant 2 . coarbitrary pr . coarbitrary m . coarbitrary sf
+ shrink (Deep _ (One a) Empty (One b)) = [Single a, Single b]
+ shrink (Deep _ pr m sf) = [deep pr' m sf  pr' < shrink pr] ++ [deep pr m' sf  m' < shrink m] ++ [deep pr m sf'  sf' < shrink sf]
+ shrink (Single x) = map Single (shrink x)
+ shrink Empty = []
instance (Arbitrary a, Sized a) => Arbitrary (Node a) where
arbitrary = oneof [
hunk ./Data/Sequence.hs 1744
liftM2 node2 arbitrary arbitrary,
liftM3 node3 arbitrary arbitrary arbitrary]
 coarbitrary (Node2 _ a b) = variant 0 . coarbitrary a . coarbitrary b
 coarbitrary (Node3 _ a b c) =
 variant 1 . coarbitrary a . coarbitrary b . coarbitrary c
+ shrink (Node2 _ a b) = [node2 a' b  a' < shrink a] ++ [node2 a b'  b' < shrink b]
+ shrink (Node3 _ a b c) = [node2 a b, node2 a c, node2 b c] ++
+ [node3 a' b c  a' < shrink a] ++ [node3 a b' c  b' < shrink b] ++ [node3 a b c'  c' < shrink c]
instance Arbitrary a => Arbitrary (Digit a) where
arbitrary = oneof [
hunk ./Data/Sequence.hs 1754
liftM2 Two arbitrary arbitrary,
liftM3 Three arbitrary arbitrary arbitrary,
liftM4 Four arbitrary arbitrary arbitrary arbitrary]

 coarbitrary (One a) = variant 0 . coarbitrary a
 coarbitrary (Two a b) = variant 1 . coarbitrary a . coarbitrary b
 coarbitrary (Three a b c) =
 variant 2 . coarbitrary a . coarbitrary b . coarbitrary c
 coarbitrary (Four a b c d) =
 variant 3 . coarbitrary a . coarbitrary b . coarbitrary c . coarbitrary d
+
+ shrink (One a) = map One (shrink a)
+ shrink (Two a b) = [One a, One b]
+ shrink (Three a b c) = [Two a b, Two a c, Two b c]
+ shrink (Four a b c d) = [Three a b c, Three a b d, Three a c d, Three b c d]

 Valid trees
hunk ./Data/Sequence.hs 1780
s == size pr + size m + size sf && valid pr && valid m && valid sf
instance (Sized a, Valid a) => Valid (Node a) where
 valid (Node2 s a b) = s == size a + size b && valid a && valid b
 valid (Node3 s a b c) =
 s == size a + size b + size c && valid a && valid b && valid c
+ valid node = (size node == foldl1 (+) (fmap size node)) && (all valid node)
instance Valid a => Valid (Digit a) where
hunk ./Data/Sequence.hs 1783
 valid (One a) = valid a
 valid (Two a b) = valid a && valid b
 valid (Three a b c) = valid a && valid b && valid c
 valid (Four a b c d) = valid a && valid b && valid c && valid d
+ valid = all valid
#endif
hunk ./containers.cabal 23
location: http://darcs.haskell.org/packages/containers/
Library {
 builddepends: base, array
+ builddepends: base >= 4.0.0.0, array
exposedmodules:
Data.Graph
Data.IntMap
}
Context:
[Use left/right rather than old/new to describe the arguments to unionWithKey
Ian Lynagh **20090208192132
Fixes trac #3002.
]
[help nhc98 by making import decl more explicit
Malcolm.Wallace@cs.york.ac.uk**20090203142144]
[Add instance Data.Traversable for IntMap
Matti Niemenmaa **20090116190353
Ignorethis: df88a286935926aecec3f8a5dd291699
]
[Require Cabal version >= 1.6
Ian Lynagh **20090122011256]
[Add "bugreports" and "sourcerepository" info to the Cabal file
Ian Lynagh **20090121182106]
[Fix warnings in containers
Ian Lynagh **20090116200251]
[optimize IntMap/IntSet findMin/findMax
sedillard@gmail.com**20081002152055]
[O(n) fromAscList IntSet / IntMap
sedillard@gmail.com**20080521195941
Added algorithm by Scott Dillard and Bertram Felgenhauer to build IntSets and
IntMaps from sorted input in linear time. Also changed quickcheck prop_Ordered
(no longer a tautology!) to include negative and duplicate keys.
]
[correct type for IntMap.intersectionWith[Key]
sedillard@gmail.com**20081002144828]
[Export mapAccumRWithKey from Map and IntMap (Trac #2769)
matti.niemenmaa+darcs@iki.fi**20081210160205]
[Bump the version number to 0.2.0.1, to workaround cabalinstall problems
Ian Lynagh **20081212201829]
[Fix #2760: change mkNorepType to mkNoRepType
'Jose Pedro Magalhaes '**20081202083424]
[Doc fix, from hackage trac #378
Ian Lynagh **20081024143949]
[import Data.Data instead of Data.Generics.*, eliminating the dependency on syb
Ross Paterson **20081005002559]
[fixed typo in highestBitMask
sedillard@gmail.com**20081002215438]
[export Data.Map.toDescList, foldlWithKey, and foldrWithKey (trac ticket 2580)
qdunkan@gmail.com**20080922213200
toDescList was previously implemented, but not exported.
foldlWithKey was previously implemented, but not exported. It can be used to
implement toDescList.
foldrWithKey is already exported as foldWithKey, but foldrWithKey is explicitly
the mirror of foldlWithKey, and foldWithKey kept for compatibility.
]
[Bump version number to 0.2.0.0
Ian Lynagh **20080920160016]
[TAG 6.10 branch has been forked
Ian Lynagh **20080919123438]
[Fixed typo in updateMinWithKey / updateMaxWithKey
sedillard@gmail.com**20080704054350]
[follow library changes
Ian Lynagh **20080903223610]
[add include/Typeable.h to extrasourcefiles
Ross Paterson **20080831181402]
[fix cabal builddepends for nhc98
Malcolm.Wallace@cs.york.ac.uk**20080828104248]
[Add a dep on syb
Ian Lynagh **20080825214314]
[add category field
Ross Paterson **20080824003013]
[we depend on st, now split off from base
Ian Lynagh **20080823223053]
[specialize functions that fail in a Monad to Maybe (proposal #2309)
Ross Paterson **20080722154812
Specialize functions signatures like
lookup :: (Monad m, Ord k) => k > Map k a > m a
to
lookup :: (Ord k) => k > Map k a > Maybe a
for simplicity and safety. No information is lost, as each of these
functions had only one use of fail, which is now changed to Nothing.
]
[tighter description of split (addresses #2447)
Ross Paterson **20080717064838]
[Make warningclean with GHC again
Ian Lynagh **20080623232023
With any luck we have now converged on a solution that works everywhere!
]
[Undo more Data.Typeablerelated breakage for nonghc.
Malcolm.Wallace@cs.york.ac.uk**20080623092757]
[Placate GHC with explicit import lists
Ian Lynagh **20080620183926]
[undo breakage caused by Wall cleaning
Malcolm.Wallace@cs.york.ac.uk**20080620093922
The import of Data.Typeable is still required, at least for nonGHC.
]
[Make the package Wall clean
Ian Lynagh **20080618233627]
[List particular extensions rather than fglasgowexts
Ian Lynagh **20080616232035]
[Avoid using deprecated flags
Ian Lynagh **20080616145241]
[TAG 20080528
Ian Lynagh **20080528004309]
Patch bundle hash:
237a4101f4d9bd662e17f7523861d74e8d66097a