Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE CPP, NoImplicitPrelude, ScopedTypeVariables, MagicHash #-}{-# LANGUAGE BangPatterns #-}------------------------------------------------------------------------------- |-- Module : GHC.List-- Copyright : (c) The University of Glasgow 1994-2002-- License : see libraries/base/LICENSE---- Maintainer : cvs-ghc@haskell.org-- Stability : internal-- Portability : non-portable (GHC Extensions)---- The List data type and its operations-------------------------------------------------------------------------------moduleGHC.List(-- [] (..), -- built-in syntax; can't be used in export listmap,(++),filter,concat,head,last,tail,init,uncons,null,length,(!!),foldl,foldl',foldl1,foldl1',scanl,scanl1,scanl',foldr,foldr1,scanr,scanr1,iterate,iterate',repeat,replicate,cycle,take,drop,sum,product,maximum,minimum,splitAt,takeWhile,dropWhile,span,break,reverse,and,or,any,all,elem,notElem,lookup,concatMap,zip,zip3,zipWith,zipWith3,unzip,unzip3,errorEmptyList,)whereimportData.MaybeimportGHC.BaseimportGHC.Num(Num(..))importGHC.Integer(Integer)infixl9!!infix4`elem`,`notElem`---------------------------------------------------------------- List-manipulation functions---------------------------------------------------------------- | \(\mathcal{O}(1)\). Extract the first element of a list, which must be non-empty.head::[a]->ahead :: [a] -> ahead(ax:[a]_)=axhead[]=aforall a. abadHead{-# NOINLINE[1]head#-}badHead::abadHead :: abadHead=String -> aforall a. String -> aerrorEmptyListString"head"-- This rule is useful in cases like-- head [y | (x,y) <- ps, x==t]{-# RULES"head/build"forall(g::forallb.(a->b->b)->b->b).head(buildg)=g(\x_->x)badHead"head/augment"forallxs(g::forallb.(a->b->b)->b->b).head(augmentgxs)=g(\x_->x)(headxs)#-}-- | \(\mathcal{O}(1)\). Decompose a list into its head and tail. If the list is-- empty, returns 'Nothing'. If the list is non-empty, returns @'Just' (x, xs)@,-- where @x@ is the head of the list and @xs@ its tail.---- @since 4.8.0.0uncons::[a]->Maybe(a,[a])uncons :: [a] -> Maybe (a, [a])uncons[]=Maybe (a, [a])forall a. Maybe aNothinguncons(ax:[a]xs)=(a, [a]) -> Maybe (a, [a])forall a. a -> Maybe aJust(ax,[a]xs)-- | \(\mathcal{O}(1)\). Extract the elements after the head of a list, which-- must be non-empty.tail::[a]->[a]tail :: [a] -> [a]tail(a_:[a]xs)=[a]xstail[]=String -> [a]forall a. String -> aerrorEmptyListString"tail"-- | \(\mathcal{O}(n)\). Extract the last element of a list, which must be-- finite and non-empty.last::[a]->a#if defined(USE_REPORT_PRELUDE)last[x]=xlast(_:xs)=lastxslast[]=errorEmptyList"last"#else-- Use foldl to make last a good consumer.-- This will compile to good code for the actual GHC.List.last.-- (At least as long it is eta-expaned, otherwise it does not, #10260.)last :: [a] -> alast[a]xs=(a -> a -> a) -> a -> [a] -> aforall a b. (b -> a -> b) -> b -> [a] -> bfoldl(\a_ax->ax)aforall a. alastError[a]xs{-# INLINElast#-}-- The inline pragma is required to make GHC remember the implementation via-- foldl.lastError::alastError :: alastError=String -> aforall a. String -> aerrorEmptyListString"last"#endif-- | \(\mathcal{O}(n)\). Return all the elements of a list except the last one.-- The list must be non-empty.init::[a]->[a]#if defined(USE_REPORT_PRELUDE)init[x]=[]init(x:xs)=x:initxsinit[]=errorEmptyList"init"#else-- eliminate repeated casesinit :: [a] -> [a]init[]=String -> [a]forall a. String -> aerrorEmptyListString"init"init(ax:[a]xs)=a -> [a] -> [a]forall t. t -> [t] -> [t]init'ax[a]xswhereinit' :: t -> [t] -> [t]init't_[]=[]init'ty(tz:[t]zs)=tyt -> [t] -> [t]forall t. t -> [t] -> [t]:t -> [t] -> [t]init'tz[t]zs#endif-- | \(\mathcal{O}(1)\). Test whether a list is empty.null::[a]->Boolnull :: [a] -> Boolnull[]=BoolTruenull(a_:[a]_)=BoolFalse-- | \(\mathcal{O}(n)\). 'length' returns the length of a finite list as an-- 'Int'. It is an instance of the more general 'Data.List.genericLength', the-- result type of which may be any kind of number.{-# NOINLINE[1]length#-}length::[a]->Intlength :: [a] -> Intlength[a]xs=[a] -> Int -> Intforall a. [a] -> Int -> IntlenAcc[a]xsInt0lenAcc::[a]->Int->IntlenAcc :: [a] -> Int -> IntlenAcc[]Intn=IntnlenAcc(a_:[a]ys)Intn=[a] -> Int -> Intforall a. [a] -> Int -> IntlenAcc[a]ys(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# RULES"length"[~1]forallxs.lengthxs=foldrlengthFBidLengthxs0"lengthList"[1]foldrlengthFBidLength=lenAcc#-}-- The lambda form turns out to be necessary to make this inline-- when we need it to and give good performance.{-# INLINE[0]lengthFB#-}lengthFB::x->(Int->Int)->Int->IntlengthFB :: x -> (Int -> Int) -> Int -> IntlengthFBx_Int -> Intr=\!Inta->Int -> Intr(IntaInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINE[0]idLength#-}idLength::Int->IntidLength :: Int -> IntidLength=Int -> Intforall a. a -> aid-- | \(\mathcal{O}(n)\). 'filter', applied to a predicate and a list, returns-- the list of those elements that satisfy the predicate; i.e.,---- > filter p xs = [ x | x <- xs, p x]---- >>> filter odd [1, 2, 3]-- [1,3]{-# NOINLINE[1]filter#-}filter::(a->Bool)->[a]->[a]filter :: (a -> Bool) -> [a] -> [a]filtera -> Bool_pred[]=[]filtera -> Boolpred(ax:[a]xs)|a -> Boolpredax=axa -> [a] -> [a]forall t. t -> [t] -> [t]:(a -> Bool) -> [a] -> [a]forall a. (a -> Bool) -> [a] -> [a]filtera -> Boolpred[a]xs|Boolotherwise=(a -> Bool) -> [a] -> [a]forall a. (a -> Bool) -> [a] -> [a]filtera -> Boolpred[a]xs{-# INLINE[0]filterFB#-}-- See Note [Inline FB functions]filterFB::(a->b->b)->(a->Bool)->a->b->bfilterFB :: (a -> b -> b) -> (a -> Bool) -> a -> b -> bfilterFBa -> b -> bca -> Boolpaxbr|a -> Boolpax=axa -> b -> b`c`br|Boolotherwise=br{-# RULES"filter"[~1]forallpxs.filterpxs=build(\cn->foldr(filterFBcp)nxs)"filterList"[1]forallp.foldr(filterFB(:)p)[]=filterp"filterFB"forallcpq.filterFB(filterFBcp)q=filterFBc(\x->qx&&px)#-}-- Note the filterFB rule, which has p and q the "wrong way round" in the RHS.-- filterFB (filterFB c p) q a b-- = if q a then filterFB c p a b else b-- = if q a then (if p a then c a b else b) else b-- = if q a && p a then c a b else b-- = filterFB c (\x -> q x && p x) a b-- I originally wrote (\x -> p x && q x), which is wrong, and actually-- gave rise to a live bug report. SLPJ.-- | 'foldl', applied to a binary operator, a starting value (typically-- the left-identity of the operator), and a list, reduces the list-- using the binary operator, from left to right:---- > foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn---- The list must be finite.foldl::forallab.(b->a->b)->b->[a]->b{-# INLINEfoldl#-}foldl :: (b -> a -> b) -> b -> [a] -> bfoldlb -> a -> bkbz0[a]xs=(a -> (b -> b) -> b -> b) -> (b -> b) -> [a] -> b -> bforall a b. (a -> b -> b) -> b -> [a] -> bfoldr(\(av::a)(b -> bfn::b->b)->(b -> b) -> b -> boneShot(\(bz::b)->b -> bfn(b -> a -> bkbzav)))(b -> bforall a. a -> aid::b->b)[a]xsbz0-- See Note [Left folds via right fold]{-Note [Left folds via right fold]Implementing foldl et. al. via foldr is only a good idea if the compiler canoptimize the resulting code (eta-expand the recursive "go"). See #7994.We hope that one of the two measure kick in: * Call Arity (-fcall-arity, enabled by default) eta-expands it if it can see all calls and determine that the arity is large. * The oneShot annotation gives a hint to the regular arity analysis that it may assume that the lambda is called at most once. See [One-shot lambdas] in CoreArity and especially [Eta expanding thunks] in CoreArity.The oneShot annotations used in this module are correct, as we only use them inarguments to foldr, where we know how the arguments are called.Note [Inline FB functions]~~~~~~~~~~~~~~~~~~~~~~~~~~After fusion rules successfully fire, we are usually left with one or more callsto list-producing functions abstracted over cons and nil. Here we call themFB functions because their names usually end with 'FB'. It's a good idea toinline FB functions because:* They are higher-order functions and therefore benefits from inlining.* When the final consumer is a left fold, inlining the FB functions is the only way to make arity expansion to happen. See Note [Left fold via right fold].For this reason we mark all FB functions INLINE [0]. The [0] phase-specifierensures that calls to FB functions can be written back to the original formwhen no fusion happens.Without these inline pragmas, the loop in perf/should_run/T13001 won't beallocation-free. Also see #13001.-}-- ------------------------------------------------------------------------------ | A strict version of 'foldl'.foldl'::forallab.(b->a->b)->b->[a]->b{-# INLINEfoldl'#-}foldl' :: (b -> a -> b) -> b -> [a] -> bfoldl'b -> a -> bkbz0[a]xs=(a -> (b -> b) -> b -> b) -> (b -> b) -> [a] -> b -> bforall a b. (a -> b -> b) -> b -> [a] -> bfoldr(\(av::a)(b -> bfn::b->b)->(b -> b) -> b -> boneShot(\(bz::b)->bzb -> b -> b`seq`b -> bfn(b -> a -> bkbzav)))(b -> bforall a. a -> aid::b->b)[a]xsbz0-- See Note [Left folds via right fold]-- | 'foldl1' is a variant of 'foldl' that has no starting value argument,-- and thus must be applied to non-empty lists.foldl1::(a->a->a)->[a]->afoldl1 :: (a -> a -> a) -> [a] -> afoldl1a -> a -> af(ax:[a]xs)=(a -> a -> a) -> a -> [a] -> aforall a b. (b -> a -> b) -> b -> [a] -> bfoldla -> a -> afax[a]xsfoldl1a -> a -> a_[]=String -> aforall a. String -> aerrorEmptyListString"foldl1"-- | A strict version of 'foldl1'foldl1'::(a->a->a)->[a]->afoldl1' :: (a -> a -> a) -> [a] -> afoldl1'a -> a -> af(ax:[a]xs)=(a -> a -> a) -> a -> [a] -> aforall a b. (b -> a -> b) -> b -> [a] -> bfoldl'a -> a -> afax[a]xsfoldl1'a -> a -> a_[]=String -> aforall a. String -> aerrorEmptyListString"foldl1'"-- ------------------------------------------------------------------------------- List sum and product-- | The 'sum' function computes the sum of a finite list of numbers.sum::(Numa)=>[a]->a{-# INLINEsum#-}sum :: [a] -> asum=(a -> a -> a) -> a -> [a] -> aforall a b. (b -> a -> b) -> b -> [a] -> bfoldla -> a -> aforall a. Num a => a -> a -> a(+)a0-- | The 'product' function computes the product of a finite list of numbers.product::(Numa)=>[a]->a{-# INLINEproduct#-}product :: [a] -> aproduct=(a -> a -> a) -> a -> [a] -> aforall a b. (b -> a -> b) -> b -> [a] -> bfoldla -> a -> aforall a. Num a => a -> a -> a(*)a1-- | \(\mathcal{O}(n)\). 'scanl' is similar to 'foldl', but returns a list of-- successive reduced values from the left:---- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]---- Note that---- > last (scanl f z xs) == foldl f z xs.-- This peculiar arrangement is necessary to prevent scanl being rewritten in-- its own right-hand side.{-# NOINLINE[1]scanl#-}scanl::(b->a->b)->b->[a]->[b]scanl :: (b -> a -> b) -> b -> [a] -> [b]scanl=(b -> a -> b) -> b -> [a] -> [b]forall b a. (b -> a -> b) -> b -> [a] -> [b]scanlGowherescanlGo::(b->a->b)->b->[a]->[b]scanlGo :: (b -> a -> b) -> b -> [a] -> [b]scanlGob -> a -> bfbq[a]ls=bqb -> [b] -> [b]forall t. t -> [t] -> [t]:(case[a]lsof[]->[]ax:[a]xs->(b -> a -> b) -> b -> [a] -> [b]forall b a. (b -> a -> b) -> b -> [a] -> [b]scanlGob -> a -> bf(b -> a -> bfbqax)[a]xs)-- Note [scanl rewrite rules]{-# RULES"scanl"[~1]forallfabs.scanlfabs=build(\cn->a`c`foldr(scanlFBfc)(constScanln)bsa)"scanlList"[1]forallf(a::a)bs.foldr(scanlFBf(:))(constScanl[])bsa=tail(scanlfabs)#-}{-# INLINE[0]scanlFB#-}-- See Note [Inline FB functions]scanlFB::(b->a->b)->(b->c->c)->a->(b->c)->b->cscanlFB :: (b -> a -> b) -> (b -> c -> c) -> a -> (b -> c) -> b -> cscanlFBb -> a -> bfb -> c -> cc=\abb -> cg->(b -> c) -> b -> coneShot(\bx->letb' :: bb'=b -> a -> bfbxabinbb'b -> c -> c`c`b -> cgbb')-- See Note [Left folds via right fold]{-# INLINE[0]constScanl#-}constScanl::a->b->aconstScanl :: a -> b -> aconstScanl=a -> b -> aforall a b. a -> b -> aconst-- | \(\mathcal{O}(n)\). 'scanl1' is a variant of 'scanl' that has no starting-- value argument:---- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]scanl1::(a->a->a)->[a]->[a]scanl1 :: (a -> a -> a) -> [a] -> [a]scanl1a -> a -> af(ax:[a]xs)=(a -> a -> a) -> a -> [a] -> [a]forall b a. (b -> a -> b) -> b -> [a] -> [b]scanla -> a -> afax[a]xsscanl1a -> a -> a_[]=[]-- | \(\mathcal{O}(n)\). A strictly accumulating version of 'scanl'{-# NOINLINE[1]scanl'#-}scanl'::(b->a->b)->b->[a]->[b]-- This peculiar form is needed to prevent scanl' from being rewritten-- in its own right hand side.scanl' :: (b -> a -> b) -> b -> [a] -> [b]scanl'=(b -> a -> b) -> b -> [a] -> [b]forall b a. (b -> a -> b) -> b -> [a] -> [b]scanlGo'wherescanlGo'::(b->a->b)->b->[a]->[b]scanlGo' :: (b -> a -> b) -> b -> [a] -> [b]scanlGo'b -> a -> bf!bq[a]ls=bqb -> [b] -> [b]forall t. t -> [t] -> [t]:(case[a]lsof[]->[]ax:[a]xs->(b -> a -> b) -> b -> [a] -> [b]forall b a. (b -> a -> b) -> b -> [a] -> [b]scanlGo'b -> a -> bf(b -> a -> bfbqax)[a]xs)-- Note [scanl rewrite rules]{-# RULES"scanl'"[~1]forallfabs.scanl'fabs=build(\cn->a`c`foldr(scanlFB'fc)(flipSeqScanl'n)bsa)"scanlList'"[1]forallfabs.foldr(scanlFB'f(:))(flipSeqScanl'[])bsa=tail(scanl'fabs)#-}{-# INLINE[0]scanlFB'#-}-- See Note [Inline FB functions]scanlFB'::(b->a->b)->(b->c->c)->a->(b->c)->b->cscanlFB' :: (b -> a -> b) -> (b -> c -> c) -> a -> (b -> c) -> b -> cscanlFB'b -> a -> bfb -> c -> cc=\abb -> cg->(b -> c) -> b -> coneShot(\bx->let!b' :: bb'=b -> a -> bfbxabinbb'b -> c -> c`c`b -> cgbb')-- See Note [Left folds via right fold]{-# INLINE[0]flipSeqScanl'#-}flipSeqScanl'::a->b->aflipSeqScanl' :: a -> b -> aflipSeqScanl'aa!b_b=aa{-Note [scanl rewrite rules]~~~~~~~~~~~~~~~~~~~~~~~~~~In most cases, when we rewrite a form to one that can fuse, we try to rewrite itback to the original form if it does not fuse. For scanl, we do something alittle different. In particular, we rewritescanl f a bstobuild (\c n -> a `c` foldr (scanlFB f c) (constScanl n) bs a)When build is inlined, this becomesa : foldr (scanlFB f (:)) (constScanl []) bs aTo rewrite this form back to scanl, we would need a rule that looked likeforall f a bs. a : foldr (scanlFB f (:)) (constScanl []) bs a = scanl f a bsThe problem with this rule is that it has (:) at its head. This would have theeffect of changing the way the inliner looks at (:), not only here buteverywhere. In most cases, this makes no difference, but in some cases itcauses it to come to a different decision about whether to inline something.Based on nofib benchmarks, this is bad for performance. Therefore, we insteadmatch on everything past the :, which is just the tail of scanl.-}-- foldr, foldr1, scanr, and scanr1 are the right-to-left duals of the-- above functions.-- | 'foldr1' is a variant of 'foldr' that has no starting value argument,-- and thus must be applied to non-empty lists.foldr1::(a->a->a)->[a]->afoldr1 :: (a -> a -> a) -> [a] -> afoldr1a -> a -> af=[a] -> agowherego :: [a] -> ago[ax]=axgo(ax:[a]xs)=a -> a -> afax([a] -> ago[a]xs)go[]=String -> aforall a. String -> aerrorEmptyListString"foldr1"{-# INLINE[0]foldr1#-}-- | \(\mathcal{O}(n)\). 'scanr' is the right-to-left dual of 'scanl'.-- Note that---- > head (scanr f z xs) == foldr f z xs.{-# NOINLINE[1]scanr#-}scanr::(a->b->b)->b->[a]->[b]scanr :: (a -> b -> b) -> b -> [a] -> [b]scanra -> b -> b_bq0[]=[bq0]scanra -> b -> bfbq0(ax:[a]xs)=a -> b -> bfaxbqb -> [b] -> [b]forall t. t -> [t] -> [t]:[b]qswhereqs :: [b]qs@(bq:[b]_)=(a -> b -> b) -> b -> [a] -> [b]forall a b. (a -> b -> b) -> b -> [a] -> [b]scanra -> b -> bfbq0[a]xs{-# INLINE[0]strictUncurryScanr#-}strictUncurryScanr::(a->b->c)->(a,b)->cstrictUncurryScanr :: (a -> b -> c) -> (a, b) -> cstrictUncurryScanra -> b -> cf(a, b)pair=case(a, b)pairof(ax,by)->a -> b -> cfaxby{-# INLINE[0]scanrFB#-}-- See Note [Inline FB functions]scanrFB::(a->b->b)->(b->c->c)->a->(b,c)->(b,c)scanrFB :: (a -> b -> b) -> (b -> c -> c) -> a -> (b, c) -> (b, c)scanrFBa -> b -> bfb -> c -> cc=\ax~(br,cest)->(a -> b -> bfaxbr,brb -> c -> c`c`cest)-- This lazy pattern match on the tuple is necessary to prevent-- an infinite loop when scanr recieves a fusable infinite list,-- which was the reason for #16943.-- See Note [scanrFB and evaluation] below{-# RULES"scanr"[~1]forallfq0ls.scanrfq0ls=build(\cn->strictUncurryScanrc(foldr(scanrFBfc)(q0,n)ls))"scanrList"[1]forallfq0ls.strictUncurryScanr(:)(foldr(scanrFBf(:))(q0,[])ls)=scanrfq0ls#-}{- Note [scanrFB and evaluation]In a previous Version, the pattern match on the tuple in scanrFB used to bestrict. If scanr is called with a build expression, the following would happen:The rule "scanr" would fire, and we obtain build (\c n -> strictUncurryScanr c (foldr (scanrFB f c) (q0,n) (build g))))The rule "foldr/build" now fires, and the second argument of strictUncurryScanrwill be the expression g (scanrFB f c) (q0,n)which will be evaluated, thanks to strictUncurryScanr.The type of (g :: (a -> b -> b) -> b -> b) allows us to apply parametricity:Either the tuple is returned (trivial), or scanrFB is called: g (scanrFB f c) (q0,n) = scanrFB ... (g' (scanrFB f c) (q0,n))Notice that thanks to the strictness of scanrFB, the expressiong' (scanrFB f c) (q0,n) gets evaluated aswell. In particular, if g' is arecursive case of g, parametricity applies again and we will again have apossible call to scanrFB. In short, g (scanrFB f c) (q0,n) will end up beingcompletely evaluated. This is resource consuming for large lists and if therecursion has no exit condition (and this will be the case in functions likerepeat or cycle), the program will crash (see #16943).The solution: Don't make scanrFB strict in its last argument. Doing so willremove the cause for the chain of evaluations, and all is well.-}-- | \(\mathcal{O}(n)\). 'scanr1' is a variant of 'scanr' that has no starting-- value argument.scanr1::(a->a->a)->[a]->[a]scanr1 :: (a -> a -> a) -> [a] -> [a]scanr1a -> a -> a_[]=[]scanr1a -> a -> a_[ax]=[ax]scanr1a -> a -> af(ax:[a]xs)=a -> a -> afaxaqa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]qswhereqs :: [a]qs@(aq:[a]_)=(a -> a -> a) -> [a] -> [a]forall a. (a -> a -> a) -> [a] -> [a]scanr1a -> a -> af[a]xs-- | 'maximum' returns the maximum value from a list,-- which must be non-empty, finite, and of an ordered type.-- It is a special case of 'Data.List.maximumBy', which allows the-- programmer to supply their own comparison function.maximum::(Orda)=>[a]->a{-# INLINABLEmaximum#-}maximum :: [a] -> amaximum[]=String -> aforall a. String -> aerrorEmptyListString"maximum"maximum[a]xs=(a -> a -> a) -> [a] -> aforall a. (a -> a -> a) -> [a] -> afoldl1a -> a -> aforall a. Ord a => a -> a -> amax[a]xs-- We want this to be specialized so that with a strict max function, GHC-- produces good code. Note that to see if this is happending, one has to-- look at -ddump-prep, not -ddump-core!{-# SPECIALIZEmaximum::[Int]->Int#-}{-# SPECIALIZEmaximum::[Integer]->Integer#-}-- | 'minimum' returns the minimum value from a list,-- which must be non-empty, finite, and of an ordered type.-- It is a special case of 'Data.List.minimumBy', which allows the-- programmer to supply their own comparison function.minimum::(Orda)=>[a]->a{-# INLINABLEminimum#-}minimum :: [a] -> aminimum[]=String -> aforall a. String -> aerrorEmptyListString"minimum"minimum[a]xs=(a -> a -> a) -> [a] -> aforall a. (a -> a -> a) -> [a] -> afoldl1a -> a -> aforall a. Ord a => a -> a -> amin[a]xs{-# SPECIALIZEminimum::[Int]->Int#-}{-# SPECIALIZEminimum::[Integer]->Integer#-}-- | 'iterate' @f x@ returns an infinite list of repeated applications-- of @f@ to @x@:---- > iterate f x == [x, f x, f (f x), ...]---- Note that 'iterate' is lazy, potentially leading to thunk build-up if-- the consumer doesn't force each iterate. See 'iterate'' for a strict-- variant of this function.{-# NOINLINE[1]iterate#-}iterate::(a->a)->a->[a]iterate :: (a -> a) -> a -> [a]iteratea -> afax=axa -> [a] -> [a]forall t. t -> [t] -> [t]:(a -> a) -> a -> [a]forall a. (a -> a) -> a -> [a]iteratea -> af(a -> afax){-# INLINE[0]iterateFB#-}-- See Note [Inline FB functions]iterateFB::(a->b->b)->(a->a)->a->biterateFB :: (a -> b -> b) -> (a -> a) -> a -> biterateFBa -> b -> bca -> afax0=a -> bgoax0wherego :: a -> bgoax=axa -> b -> b`c`a -> bgo(a -> afax){-# RULES"iterate"[~1]forallfx.iteratefx=build(\c_n->iterateFBcfx)"iterateFB"[1]iterateFB(:)=iterate#-}-- | 'iterate'' is the strict version of 'iterate'.---- It ensures that the result of each application of force to weak head normal-- form before proceeding.{-# NOINLINE[1]iterate'#-}iterate'::(a->a)->a->[a]iterate' :: (a -> a) -> a -> [a]iterate'a -> afax=letx' :: ax'=a -> afaxinax'a -> [a] -> [a]`seq`(axa -> [a] -> [a]forall t. t -> [t] -> [t]:(a -> a) -> a -> [a]forall a. (a -> a) -> a -> [a]iterate'a -> afax'){-# INLINE[0]iterate'FB#-}-- See Note [Inline FB functions]iterate'FB::(a->b->b)->(a->a)->a->biterate'FB :: (a -> b -> b) -> (a -> a) -> a -> biterate'FBa -> b -> bca -> afax0=a -> bgoax0wherego :: a -> bgoax=letx' :: ax'=a -> afaxinax'a -> b -> b`seq`(axa -> b -> b`c`a -> bgoax'){-# RULES"iterate'"[~1]forallfx.iterate'fx=build(\c_n->iterate'FBcfx)"iterate'FB"[1]iterate'FB(:)=iterate'#-}-- | 'repeat' @x@ is an infinite list, with @x@ the value of every element.repeat::a->[a]{-# INLINE[0]repeat#-}-- The pragma just gives the rules more chance to firerepeat :: a -> [a]repeatax=[a]xswherexs :: [a]xs=axa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]xs{-# INLINE[0]repeatFB#-}-- ditto -- See Note [Inline FB functions]repeatFB::(a->b->b)->a->brepeatFB :: (a -> b -> b) -> a -> brepeatFBa -> b -> bcax=bxswherexs :: bxs=axa -> b -> b`c`bxs{-# RULES"repeat"[~1]forallx.repeatx=build(\c_n->repeatFBcx)"repeatFB"[1]repeatFB(:)=repeat#-}-- | 'replicate' @n x@ is a list of length @n@ with @x@ the value of-- every element.-- It is an instance of the more general 'Data.List.genericReplicate',-- in which @n@ may be of any integral type.{-# INLINEreplicate#-}replicate::Int->a->[a]replicate :: Int -> a -> [a]replicateIntnax=Int -> [a] -> [a]forall a. Int -> [a] -> [a]takeIntn(a -> [a]forall a. a -> [a]repeatax)-- | 'cycle' ties a finite list into a circular one, or equivalently,-- the infinite repetition of the original list. It is the identity-- on infinite lists.cycle::[a]->[a]cycle :: [a] -> [a]cycle[]=String -> [a]forall a. String -> aerrorEmptyListString"cycle"cycle[a]xs=[a]xs'wherexs' :: [a]xs'=[a]xs[a] -> [a] -> [a]forall a. [a] -> [a] -> [a]++[a]xs'-- | 'takeWhile', applied to a predicate @p@ and a list @xs@, returns the-- longest prefix (possibly empty) of @xs@ of elements that satisfy @p@:---- > takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2]-- > takeWhile (< 9) [1,2,3] == [1,2,3]-- > takeWhile (< 0) [1,2,3] == []--{-# NOINLINE[1]takeWhile#-}takeWhile::(a->Bool)->[a]->[a]takeWhile :: (a -> Bool) -> [a] -> [a]takeWhilea -> Bool_[]=[]takeWhilea -> Boolp(ax:[a]xs)|a -> Boolpax=axa -> [a] -> [a]forall t. t -> [t] -> [t]:(a -> Bool) -> [a] -> [a]forall a. (a -> Bool) -> [a] -> [a]takeWhilea -> Boolp[a]xs|Boolotherwise=[]{-# INLINE[0]takeWhileFB#-}-- See Note [Inline FB functions]takeWhileFB::(a->Bool)->(a->b->b)->b->a->b->btakeWhileFB :: (a -> Bool) -> (a -> b -> b) -> b -> a -> b -> btakeWhileFBa -> Boolpa -> b -> bcbn=\axbr->ifa -> Boolpaxthenaxa -> b -> b`c`brelsebn-- The takeWhileFB rule is similar to the filterFB rule. It works like this:-- takeWhileFB q (takeWhileFB p c n) n =-- \x r -> if q x then (takeWhileFB p c n) x r else n =-- \x r -> if q x then (\x' r' -> if p x' then x' `c` r' else n) x r else n =-- \x r -> if q x then (if p x then x `c` r else n) else n =-- \x r -> if q x && p x then x `c` r else n =-- takeWhileFB (\x -> q x && p x) c n{-# RULES"takeWhile"[~1]forallpxs.takeWhilepxs=build(\cn->foldr(takeWhileFBpcn)nxs)"takeWhileList"[1]forallp.foldr(takeWhileFBp(:)[])[]=takeWhilep"takeWhileFB"forallcnpq.takeWhileFBq(takeWhileFBpcn)n=takeWhileFB(\x->qx&&px)cn#-}-- | 'dropWhile' @p xs@ returns the suffix remaining after 'takeWhile' @p xs@:---- > dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3]-- > dropWhile (< 9) [1,2,3] == []-- > dropWhile (< 0) [1,2,3] == [1,2,3]--dropWhile::(a->Bool)->[a]->[a]dropWhile :: (a -> Bool) -> [a] -> [a]dropWhilea -> Bool_[]=[]dropWhilea -> Boolpxs :: [a]xs@(ax:[a]xs')|a -> Boolpax=(a -> Bool) -> [a] -> [a]forall a. (a -> Bool) -> [a] -> [a]dropWhilea -> Boolp[a]xs'|Boolotherwise=[a]xs-- | 'take' @n@, applied to a list @xs@, returns the prefix of @xs@-- of length @n@, or @xs@ itself if @n > 'length' xs@:---- > take 5 "Hello World!" == "Hello"-- > take 3 [1,2,3,4,5] == [1,2,3]-- > take 3 [1,2] == [1,2]-- > take 3 [] == []-- > take (-1) [1,2] == []-- > take 0 [1,2] == []---- It is an instance of the more general 'Data.List.genericTake',-- in which @n@ may be of any integral type.take::Int->[a]->[a]#if defined(USE_REPORT_PRELUDE)taken_|n<=0=[]take_[]=[]taken(x:xs)=x:take(n-1)xs#else{- We always want to inline this to take advantage of a known length argumentsign. Note, however, that it's important for the RULES to grab take, ratherthan trying to INLINE take immediately and then letting the RULES grabunsafeTake. Presumably the latter approach doesn't grab it early enough; it ledto an allocation regression in nofib/fft2. -}{-# INLINE[1]take#-}take :: Int -> [a] -> [a]takeIntn[a]xs|Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<Intn=Int -> [a] -> [a]forall a. Int -> [a] -> [a]unsafeTakeIntn[a]xs|Boolotherwise=[]-- A version of take that takes the whole list if it's given an argument less-- than 1.{-# NOINLINE[1]unsafeTake#-}unsafeTake::Int->[a]->[a]unsafeTake :: Int -> [a] -> [a]unsafeTake!Int_[]=[]unsafeTakeInt1(ax:[a]_)=[ax]unsafeTakeIntm(ax:[a]xs)=axa -> [a] -> [a]forall t. t -> [t] -> [t]:Int -> [a] -> [a]forall a. Int -> [a] -> [a]unsafeTake(IntmInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)[a]xs{-# RULES"take"[~1]forallnxs.takenxs=build(\cnil->if0<nthenfoldr(takeFBcnil)(flipSeqTakenil)xsnelsenil)"unsafeTakeList"[1]forallnxs.foldr(takeFB(:)[])(flipSeqTake[])xsn=unsafeTakenxs#-}{-# INLINE[0]flipSeqTake#-}-- Just flip seq, specialized to Int, but not inlined too early.-- It's important to force the numeric argument here, even though-- it's not used. Otherwise, take n [] doesn't force n. This is-- bad for strictness analysis and unboxing, and leads to increased-- allocation in T7257.flipSeqTake::a->Int->aflipSeqTake :: a -> Int -> aflipSeqTakeax!Int_n=ax{-# INLINE[0]takeFB#-}-- See Note [Inline FB functions]takeFB::(a->b->b)->b->a->(Int->b)->Int->b-- The \m accounts for the fact that takeFB is used in a higher-order-- way by takeFoldr, so it's better to inline. A good example is-- take n (repeat x)-- for which we get excellent code... but only if we inline takeFB-- when given four argumentstakeFB :: (a -> b -> b) -> b -> a -> (Int -> b) -> Int -> btakeFBa -> b -> bcbnaxInt -> bxs=\Intm->caseIntmofInt1->axa -> b -> b`c`bnInt_->axa -> b -> b`c`Int -> bxs(IntmInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)#endif-- | 'drop' @n xs@ returns the suffix of @xs@-- after the first @n@ elements, or @[]@ if @n > 'length' xs@:---- > drop 6 "Hello World!" == "World!"-- > drop 3 [1,2,3,4,5] == [4,5]-- > drop 3 [1,2] == []-- > drop 3 [] == []-- > drop (-1) [1,2] == [1,2]-- > drop 0 [1,2] == [1,2]---- It is an instance of the more general 'Data.List.genericDrop',-- in which @n@ may be of any integral type.drop::Int->[a]->[a]#if defined(USE_REPORT_PRELUDE)dropnxs|n<=0=xsdrop_[]=[]dropn(_:xs)=drop(n-1)xs#else /* hack away */{-# INLINEdrop#-}drop :: Int -> [a] -> [a]dropIntn[a]ls|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=[a]ls|Boolotherwise=Int -> [a] -> [a]forall a. Int -> [a] -> [a]unsafeDropIntn[a]lswhere-- A version of drop that drops the whole list if given an argument-- less than 1unsafeDrop::Int->[a]->[a]unsafeDrop :: Int -> [a] -> [a]unsafeDrop!Int_[]=[]unsafeDropInt1(a_:[a]xs)=[a]xsunsafeDropIntm(a_:[a]xs)=Int -> [a] -> [a]forall a. Int -> [a] -> [a]unsafeDrop(IntmInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)[a]xs#endif-- | 'splitAt' @n xs@ returns a tuple where first element is @xs@ prefix of-- length @n@ and second element is the remainder of the list:---- > splitAt 6 "Hello World!" == ("Hello ","World!")-- > splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])-- > splitAt 1 [1,2,3] == ([1],[2,3])-- > splitAt 3 [1,2,3] == ([1,2,3],[])-- > splitAt 4 [1,2,3] == ([1,2,3],[])-- > splitAt 0 [1,2,3] == ([],[1,2,3])-- > splitAt (-1) [1,2,3] == ([],[1,2,3])---- It is equivalent to @('take' n xs, 'drop' n xs)@ when @n@ is not @_|_@-- (@splitAt _|_ xs = _|_@).-- 'splitAt' is an instance of the more general 'Data.List.genericSplitAt',-- in which @n@ may be of any integral type.splitAt::Int->[a]->([a],[a])#if defined(USE_REPORT_PRELUDE)splitAtnxs=(takenxs,dropnxs)#elsesplitAt :: Int -> [a] -> ([a], [a])splitAtIntn[a]ls|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=([],[a]ls)|Boolotherwise=Int -> [a] -> ([a], [a])forall a. Int -> [a] -> ([a], [a])splitAt'Intn[a]lswheresplitAt'::Int->[a]->([a],[a])splitAt' :: Int -> [a] -> ([a], [a])splitAt'Int_[]=([],[])splitAt'Int1(ax:[a]xs)=([ax],[a]xs)splitAt'Intm(ax:[a]xs)=(axa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]xs',[a]xs'')where([a]xs',[a]xs'')=Int -> [a] -> ([a], [a])forall a. Int -> [a] -> ([a], [a])splitAt'(IntmInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)[a]xs#endif /* USE_REPORT_PRELUDE */-- | 'span', applied to a predicate @p@ and a list @xs@, returns a tuple where-- first element is longest prefix (possibly empty) of @xs@ of elements that-- satisfy @p@ and second element is the remainder of the list:---- > span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4])-- > span (< 9) [1,2,3] == ([1,2,3],[])-- > span (< 0) [1,2,3] == ([],[1,2,3])---- 'span' @p xs@ is equivalent to @('takeWhile' p xs, 'dropWhile' p xs)@span::(a->Bool)->[a]->([a],[a])span :: (a -> Bool) -> [a] -> ([a], [a])spana -> Bool_xs :: [a]xs@[]=([a]xs,[a]xs)spana -> Boolpxs :: [a]xs@(ax:[a]xs')|a -> Boolpax=let([a]ys,[a]zs)=(a -> Bool) -> [a] -> ([a], [a])forall a. (a -> Bool) -> [a] -> ([a], [a])spana -> Boolp[a]xs'in(axa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]ys,[a]zs)|Boolotherwise=([],[a]xs)-- | 'break', applied to a predicate @p@ and a list @xs@, returns a tuple where-- first element is longest prefix (possibly empty) of @xs@ of elements that-- /do not satisfy/ @p@ and second element is the remainder of the list:---- > break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4])-- > break (< 9) [1,2,3] == ([],[1,2,3])-- > break (> 9) [1,2,3] == ([1,2,3],[])---- 'break' @p@ is equivalent to @'span' ('not' . p)@.break::(a->Bool)->[a]->([a],[a])#if defined(USE_REPORT_PRELUDE)breakp=span(not.p)#else-- HBC version (stolen)break :: (a -> Bool) -> [a] -> ([a], [a])breaka -> Bool_xs :: [a]xs@[]=([a]xs,[a]xs)breaka -> Boolpxs :: [a]xs@(ax:[a]xs')|a -> Boolpax=([],[a]xs)|Boolotherwise=let([a]ys,[a]zs)=(a -> Bool) -> [a] -> ([a], [a])forall a. (a -> Bool) -> [a] -> ([a], [a])breaka -> Boolp[a]xs'in(axa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]ys,[a]zs)#endif-- | 'reverse' @xs@ returns the elements of @xs@ in reverse order.-- @xs@ must be finite.reverse::[a]->[a]#if defined(USE_REPORT_PRELUDE)reverse=foldl(flip(:))[]#elsereverse :: [a] -> [a]reverse[a]l=[a] -> [a] -> [a]forall a. [a] -> [a] -> [a]rev[a]l[]whererev :: [a] -> [a] -> [a]rev[][a]a=[a]arev(ax:[a]xs)[a]a=[a] -> [a] -> [a]rev[a]xs(axa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]a)#endif-- | 'and' returns the conjunction of a Boolean list. For the result to be-- 'True', the list must be finite; 'False', however, results from a 'False'-- value at a finite index of a finite or infinite list.and::[Bool]->Bool#if defined(USE_REPORT_PRELUDE)and=foldr(&&)True#elseand :: [Bool] -> Booland[]=BoolTrueand(Boolx:[Bool]xs)=BoolxBool -> Bool -> Bool&&[Bool] -> Booland[Bool]xs{-# NOINLINE[1]and#-}{-# RULES"and/build"forall(g::forallb.(Bool->b->b)->b->b).and(buildg)=g(&&)True#-}#endif-- | 'or' returns the disjunction of a Boolean list. For the result to be-- 'False', the list must be finite; 'True', however, results from a 'True'-- value at a finite index of a finite or infinite list.or::[Bool]->Bool#if defined(USE_REPORT_PRELUDE)or=foldr(||)False#elseor :: [Bool] -> Boolor[]=BoolFalseor(Boolx:[Bool]xs)=BoolxBool -> Bool -> Bool||[Bool] -> Boolor[Bool]xs{-# NOINLINE[1]or#-}{-# RULES"or/build"forall(g::forallb.(Bool->b->b)->b->b).or(buildg)=g(||)False#-}#endif-- | Applied to a predicate and a list, 'any' determines if any element-- of the list satisfies the predicate. For the result to be-- 'False', the list must be finite; 'True', however, results from a 'True'-- value for the predicate applied to an element at a finite index of a finite or infinite list.any::(a->Bool)->[a]->Bool#if defined(USE_REPORT_PRELUDE)anyp=or.mapp#elseany :: (a -> Bool) -> [a] -> Boolanya -> Bool_[]=BoolFalseanya -> Boolp(ax:[a]xs)=a -> BoolpaxBool -> Bool -> Bool||(a -> Bool) -> [a] -> Boolforall a. (a -> Bool) -> [a] -> Boolanya -> Boolp[a]xs{-# NOINLINE[1]any#-}{-# RULES"any/build"forallp(g::forallb.(a->b->b)->b->b).anyp(buildg)=g((||).p)False#-}#endif-- | Applied to a predicate and a list, 'all' determines if all elements-- of the list satisfy the predicate. For the result to be-- 'True', the list must be finite; 'False', however, results from a 'False'-- value for the predicate applied to an element at a finite index of a finite or infinite list.all::(a->Bool)->[a]->Bool#if defined(USE_REPORT_PRELUDE)allp=and.mapp#elseall :: (a -> Bool) -> [a] -> Boolalla -> Bool_[]=BoolTruealla -> Boolp(ax:[a]xs)=a -> BoolpaxBool -> Bool -> Bool&&(a -> Bool) -> [a] -> Boolforall a. (a -> Bool) -> [a] -> Boolalla -> Boolp[a]xs{-# NOINLINE[1]all#-}{-# RULES"all/build"forallp(g::forallb.(a->b->b)->b->b).allp(buildg)=g((&&).p)True#-}#endif-- | 'elem' is the list membership predicate, usually written in infix form,-- e.g., @x \`elem\` xs@. For the result to be-- 'False', the list must be finite; 'True', however, results from an element-- equal to @x@ found at a finite index of a finite or infinite list.elem::(Eqa)=>a->[a]->Bool#if defined(USE_REPORT_PRELUDE)elemx=any(==x)#elseelem :: a -> [a] -> Boolelema_[]=BoolFalseelemax(ay:[a]ys)=axa -> a -> Boolforall a. Eq a => a -> a -> Bool==ayBool -> Bool -> Bool||a -> [a] -> Boolforall a. Eq a => a -> [a] -> Boolelemax[a]ys{-# NOINLINE[1]elem#-}{-# RULES"elem/build"forallx(g::forallb.Eqa=>(a->b->b)->b->b).elemx(buildg)=g(\yr->(x==y)||r)False#-}#endif-- | 'notElem' is the negation of 'elem'.notElem::(Eqa)=>a->[a]->Bool#if defined(USE_REPORT_PRELUDE)notElemx=all(/=x)#elsenotElem :: a -> [a] -> BoolnotElema_[]=BoolTruenotElemax(ay:[a]ys)=axa -> a -> Boolforall a. Eq a => a -> a -> Bool/=ayBool -> Bool -> Bool&&a -> [a] -> Boolforall a. Eq a => a -> [a] -> BoolnotElemax[a]ys{-# NOINLINE[1]notElem#-}{-# RULES"notElem/build"forallx(g::forallb.Eqa=>(a->b->b)->b->b).notElemx(buildg)=g(\yr->(x/=y)&&r)True#-}#endif-- | \(\mathcal{O}(n)\). 'lookup' @key assocs@ looks up a key in an association-- list.---- >>> lookup 2 [(1, "first"), (2, "second"), (3, "third")]-- Just "second"lookup::(Eqa)=>a->[(a,b)]->Maybeblookup :: a -> [(a, b)] -> Maybe blookupa_key[]=Maybe bforall a. Maybe aNothinglookupakey((ax,by):[(a, b)]xys)|akeya -> a -> Boolforall a. Eq a => a -> a -> Bool==ax=b -> Maybe bforall a. a -> Maybe aJustby|Boolotherwise=a -> [(a, b)] -> Maybe bforall a b. Eq a => a -> [(a, b)] -> Maybe blookupakey[(a, b)]xys-- | Map a function over a list and concatenate the results.concatMap::(a->[b])->[a]->[b]concatMap :: (a -> [b]) -> [a] -> [b]concatMapa -> [b]f=(a -> [b] -> [b]) -> [b] -> [a] -> [b]forall a b. (a -> b -> b) -> b -> [a] -> bfoldr([b] -> [b] -> [b]forall a. [a] -> [a] -> [a](++)([b] -> [b] -> [b]) -> (a -> [b]) -> a -> [b] -> [b]forall b c a. (b -> c) -> (a -> b) -> a -> c.a -> [b]f)[]{-# NOINLINE[1]concatMap#-}{-# RULES"concatMap"forallfxs.concatMapfxs=build(\cn->foldr(\xb->foldrcb(fx))nxs)#-}-- | Concatenate a list of lists.concat::[[a]]->[a]concat :: [[a]] -> [a]concat=([a] -> [a] -> [a]) -> [a] -> [[a]] -> [a]forall a b. (a -> b -> b) -> b -> [a] -> bfoldr[a] -> [a] -> [a]forall a. [a] -> [a] -> [a](++)[]{-# NOINLINE[1]concat#-}{-# RULES"concat"forallxs.concatxs=build(\cn->foldr(\xy->foldrcyx)nxs)-- We don't bother to turn non-fusible applications of concat back into concat#-}-- | List index (subscript) operator, starting from 0.-- It is an instance of the more general 'Data.List.genericIndex',-- which takes an index of any integral type.(!!)::[a]->Int->a#if defined(USE_REPORT_PRELUDE)xs!!n|n<0=errorWithoutStackTrace"Prelude.!!: negative index"[]!!_=errorWithoutStackTrace"Prelude.!!: index too large"(x:_)!!0=x(_:xs)!!n=xs!!(n-1)#else-- We don't really want the errors to inline with (!!).-- We may want to fuss around a bit with NOINLINE, and-- if so we should be careful not to trip up known-bottom-- optimizations.tooLarge::Int->atooLarge :: Int -> atooLargeInt_=String -> aforall a. String -> aerrorWithoutStackTrace(Stringprel_list_strString -> String -> Stringforall a. [a] -> [a] -> [a]++String"!!: index too large")negIndex::anegIndex :: anegIndex=String -> aforall a. String -> aerrorWithoutStackTrace(String -> a) -> String -> aforall a b. (a -> b) -> a -> b$Stringprel_list_strString -> String -> Stringforall a. [a] -> [a] -> [a]++String"!!: negative index"{-# INLINABLE(!!)#-}[a]xs!! :: [a] -> Int -> a!!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=aforall a. anegIndex|Boolotherwise=(a -> (Int -> a) -> Int -> a) -> (Int -> a) -> [a] -> Int -> aforall a b. (a -> b -> b) -> b -> [a] -> bfoldr(\axInt -> arIntk->caseIntkofInt0->axInt_->Int -> ar(IntkInt -> Int -> Intforall a. Num a => a -> a -> a-Int1))Int -> aforall a. Int -> atooLarge[a]xsIntn#endif---------------------------------------------------------------- The zip family--------------------------------------------------------------foldr2::(a->b->c->c)->c->[a]->[b]->cfoldr2 :: (a -> b -> c -> c) -> c -> [a] -> [b] -> cfoldr2a -> b -> c -> ckcz=[a] -> [b] -> cgowherego :: [a] -> [b] -> cgo[][b]_ys=czgo[a]_xs[]=czgo(ax:[a]xs)(by:[b]ys)=a -> b -> c -> ckaxby([a] -> [b] -> cgo[a]xs[b]ys){-# INLINE[0]foldr2#-}-- See Note [Fusion for foldrN]foldr2_left::(a->b->c->d)->d->a->([b]->c)->[b]->dfoldr2_left :: (a -> b -> c -> d) -> d -> a -> ([b] -> c) -> [b] -> dfoldr2_lefta -> b -> c -> d_kdza_x[b] -> c_r[]=dzfoldr2_lefta -> b -> c -> dkd_zax[b] -> cr(by:[b]ys)=a -> b -> c -> dkaxby([b] -> cr[b]ys)-- foldr2 k z xs ys = foldr (foldr2_left k z) (\_ -> z) xs ys{-# RULES-- See Note [Fusion for foldrN]"foldr2/left"forallkzys(g::forallb.(a->b->b)->b->b).foldr2kz(buildg)ys=g(foldr2_leftkz)(\_->z)ys#-}foldr3::(a->b->c->d->d)->d->[a]->[b]->[c]->dfoldr3 :: (a -> b -> c -> d -> d) -> d -> [a] -> [b] -> [c] -> dfoldr3a -> b -> c -> d -> dkdz=[a] -> [b] -> [c] -> dgowherego :: [a] -> [b] -> [c] -> dgo[][b]_[c]_=dzgo[a]_[][c]_=dzgo[a]_[b]_[]=dzgo(aa:[a]as)(bb:[b]bs)(cc:[c]cs)=a -> b -> c -> d -> dkaabbcc([a] -> [b] -> [c] -> dgo[a]as[b]bs[c]cs){-# INLINE[0]foldr3#-}-- See Note [Fusion for foldrN]foldr3_left::(a->b->c->d->e)->e->a->([b]->[c]->d)->[b]->[c]->efoldr3_left :: (a -> b -> c -> d -> e)-> e -> a -> ([b] -> [c] -> d) -> [b] -> [c] -> efoldr3_lefta -> b -> c -> d -> eke_zaa[b] -> [c] -> dr(bb:[b]bs)(cc:[c]cs)=a -> b -> c -> d -> ekaabbcc([b] -> [c] -> dr[b]bs[c]cs)foldr3_lefta -> b -> c -> d -> e_eza_[b] -> [c] -> d_[b]_[c]_=ez-- foldr3 k n xs ys zs = foldr (foldr3_left k n) (\_ _ -> n) xs ys zs{-# RULES-- See Note [Fusion for foldrN]"foldr3/left"forallkz(g::forallb.(a->b->b)->b->b).foldr3kz(buildg)=g(foldr3_leftkz)(\__->z)#-}{- Note [Fusion for foldrN]~~~~~~~~~~~~~~~~~~~~~~~~~~~We arrange that foldr2, foldr3, etc is a good consumer for its first(left) list argument. Here's how. See below for the second, thirdetc list arguments* The rule "foldr2/left" (active only before phase 1) does this: foldr2 k z (build g) ys = g (foldr2_left k z) (\_ -> z) ys thereby fusing away the 'build' on the left argument* To ensure this rule has a chance to fire, foldr2 has a NOINLINE[1] pragmaThere used to be a "foldr2/right" rule, allowing foldr2 to fuse with a buildform on the right. However, this causes trouble if the right list ends ina bottom that is only avoided by the left list ending at that spot. That is,foldr2 f z [a,b,c] (d:e:f:_|_), where the right list is produced by a buildform, would cause the foldr2/right rule to introduce bottom. Example: zip [1,2,3,4] (unfoldr (\s -> if s > 4 then undefined else Just (s,s+1)) 1)should produce [(1,1),(2,2),(3,3),(4,4)]but with the foldr2/right rule it would instead produce (1,1):(2,2):(3,3):(4,4):_|_Note [Fusion for zipN/zipWithN]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~We arrange that zip, zip3, etc, and zipWith, zipWit3 etc, are allgood consumers for their first (left) argument, and good producers.Here's how. See Note [Fusion for foldr2] for why it can't fuse itssecond (right) list argument.NB: Zips for larger tuples are in the List module.* Rule "zip" (active only before phase 1) rewrites zip xs ys = build (\c n -> foldr2 (zipFB c) n xs ys) See also Note [Inline FB functions] Ditto rule "zipWith".* To give this rule a chance to fire, we give zip a NOLINLINE[1] pragma (although since zip is recursive it might not need it)* Now the rules for foldr2 (see Note [Fusion for foldr2]) may fire, or rules that fuse the build-produced output of zip.* If none of these fire, rule "zipList" (active only in phase 1) rewrites the foldr2 call back to zip foldr2 (zipFB (:)) [] = zip This rule will only fire when build has inlined, which also happens in phase 1. Ditto rule "zipWithList".-}------------------------------------------------ | \(\mathcal{O}(\min(m,n))\). 'zip' takes two lists and returns a list of-- corresponding pairs.---- > zip [1, 2] ['a', 'b'] = [(1, 'a'), (2, 'b')]---- If one input list is short, excess elements of the longer list are-- discarded:---- > zip [1] ['a', 'b'] = [(1, 'a')]-- > zip [1, 2] ['a'] = [(1, 'a')]---- 'zip' is right-lazy:---- > zip [] _|_ = []-- > zip _|_ [] = _|_---- 'zip' is capable of list fusion, but it is restricted to its-- first list argument and its resulting list.{-# NOINLINE[1]zip#-}-- See Note [Fusion for zipN/zipWithN]zip::[a]->[b]->[(a,b)]zip :: [a] -> [b] -> [(a, b)]zip[][b]_bs=[]zip[a]_as[]=[]zip(aa:[a]as)(bb:[b]bs)=(aa,bb)(a, b) -> [(a, b)] -> [(a, b)]forall t. t -> [t] -> [t]:[a] -> [b] -> [(a, b)]forall a b. [a] -> [b] -> [(a, b)]zip[a]as[b]bs{-# INLINE[0]zipFB#-}-- See Note [Inline FB functions]zipFB::((a,b)->c->d)->a->b->c->dzipFB :: ((a, b) -> c -> d) -> a -> b -> c -> dzipFB(a, b) -> c -> dc=\axbycr->(ax,by)(a, b) -> c -> d`c`cr{-# RULES-- See Note [Fusion for zipN/zipWithN]"zip"[~1]forallxsys.zipxsys=build(\cn->foldr2(zipFBc)nxsys)"zipList"[1]foldr2(zipFB(:))[]=zip#-}------------------------------------------------ | 'zip3' takes three lists and returns a list of triples, analogous to-- 'zip'.-- It is capable of list fusion, but it is restricted to its-- first list argument and its resulting list.{-# NOINLINE[1]zip3#-}zip3::[a]->[b]->[c]->[(a,b,c)]-- Specification-- zip3 = zipWith3 (,,)zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]zip3(aa:[a]as)(bb:[b]bs)(cc:[c]cs)=(aa,bb,cc)(a, b, c) -> [(a, b, c)] -> [(a, b, c)]forall t. t -> [t] -> [t]:[a] -> [b] -> [c] -> [(a, b, c)]forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]zip3[a]as[b]bs[c]cszip3[a]_[b]_[c]_=[]{-# INLINE[0]zip3FB#-}-- See Note [Inline FB functions]zip3FB::((a,b,c)->xs->xs')->a->b->c->xs->xs'zip3FB :: ((a, b, c) -> xs -> xs') -> a -> b -> c -> xs -> xs'zip3FB(a, b, c) -> xs -> xs'cons=\aabbccxsr->(aa,bb,cc)(a, b, c) -> xs -> xs'`cons`xsr{-# RULES-- See Note [Fusion for zipN/zipWithN]"zip3"[~1]forallasbscs.zip3asbscs=build(\cn->foldr3(zip3FBc)nasbscs)"zip3List"[1]foldr3(zip3FB(:))[]=zip3#-}-- The zipWith family generalises the zip family by zipping with the-- function given as the first argument, instead of a tupling function.------------------------------------------------ | \(\mathcal{O}(\min(m,n))\). 'zipWith' generalises 'zip' by zipping with the-- function given as the first argument, instead of a tupling function. For-- example, @'zipWith' (+)@ is applied to two lists to produce the list of-- corresponding sums:---- >>> zipWith (+) [1, 2, 3] [4, 5, 6]-- [5,7,9]---- 'zipWith' is right-lazy:---- > zipWith f [] _|_ = []---- 'zipWith' is capable of list fusion, but it is restricted to its-- first list argument and its resulting list.{-# NOINLINE[1]zipWith#-}-- See Note [Fusion for zipN/zipWithN]zipWith::(a->b->c)->[a]->[b]->[c]zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]zipWitha -> b -> cf=[a] -> [b] -> [c]gowherego :: [a] -> [b] -> [c]go[][b]_=[]go[a]_[]=[]go(ax:[a]xs)(by:[b]ys)=a -> b -> cfaxbyc -> [c] -> [c]forall t. t -> [t] -> [t]:[a] -> [b] -> [c]go[a]xs[b]ys-- zipWithFB must have arity 2 since it gets two arguments in the "zipWith"-- rule; it might not get inlined otherwise{-# INLINE[0]zipWithFB#-}-- See Note [Inline FB functions]zipWithFB::(a->b->c)->(d->e->a)->d->e->b->czipWithFB :: (a -> b -> c) -> (d -> e -> a) -> d -> e -> b -> czipWithFBa -> b -> ccd -> e -> af=\dxeybr->(dxd -> e -> a`f`ey)a -> b -> c`c`br{-# RULES-- See Note [Fusion for zipN/zipWithN]"zipWith"[~1]forallfxsys.zipWithfxsys=build(\cn->foldr2(zipWithFBcf)nxsys)"zipWithList"[1]forallf.foldr2(zipWithFB(:)f)[]=zipWithf#-}-- | The 'zipWith3' function takes a function which combines three-- elements, as well as three lists and returns a list of their point-wise-- combination, analogous to 'zipWith'.-- It is capable of list fusion, but it is restricted to its-- first list argument and its resulting list.{-# NOINLINE[1]zipWith3#-}zipWith3::(a->b->c->d)->[a]->[b]->[c]->[d]zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]zipWith3a -> b -> c -> dz=[a] -> [b] -> [c] -> [d]gowherego :: [a] -> [b] -> [c] -> [d]go(aa:[a]as)(bb:[b]bs)(cc:[c]cs)=a -> b -> c -> dzaabbccd -> [d] -> [d]forall t. t -> [t] -> [t]:[a] -> [b] -> [c] -> [d]go[a]as[b]bs[c]csgo[a]_[b]_[c]_=[]{-# INLINE[0]zipWith3FB#-}-- See Note [Inline FB functions]zipWith3FB::(d->xs->xs')->(a->b->c->d)->a->b->c->xs->xs'zipWith3FB :: (d -> xs -> xs') -> (a -> b -> c -> d) -> a -> b -> c -> xs -> xs'zipWith3FBd -> xs -> xs'consa -> b -> c -> dfunc=\aabbccxsr->(a -> b -> c -> dfuncaabbcc)d -> xs -> xs'`cons`xsr{-# RULES"zipWith3"[~1]forallfasbscs.zipWith3fasbscs=build(\cn->foldr3(zipWith3FBcf)nasbscs)"zipWith3List"[1]forallf.foldr3(zipWith3FB(:)f)[]=zipWith3f#-}-- | 'unzip' transforms a list of pairs into a list of first components-- and a list of second components.unzip::[(a,b)]->([a],[b]){-# INLINEunzip#-}-- Inline so that fusion `foldr` has an opportunity to fire.-- See Note [Inline @unzipN@ functions] in GHC/OldList.hs.unzip :: [(a, b)] -> ([a], [b])unzip=((a, b) -> ([a], [b]) -> ([a], [b]))-> ([a], [b]) -> [(a, b)] -> ([a], [b])forall a b. (a -> b -> b) -> b -> [a] -> bfoldr(\(aa,bb)~([a]as,[b]bs)->(aaa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]as,bbb -> [b] -> [b]forall t. t -> [t] -> [t]:[b]bs))([],[])-- | The 'unzip3' function takes a list of triples and returns three-- lists, analogous to 'unzip'.unzip3::[(a,b,c)]->([a],[b],[c]){-# INLINEunzip3#-}-- Inline so that fusion `foldr` has an opportunity to fire.-- See Note [Inline @unzipN@ functions] in GHC/OldList.hs.unzip3 :: [(a, b, c)] -> ([a], [b], [c])unzip3=((a, b, c) -> ([a], [b], [c]) -> ([a], [b], [c]))-> ([a], [b], [c]) -> [(a, b, c)] -> ([a], [b], [c])forall a b. (a -> b -> b) -> b -> [a] -> bfoldr(\(aa,bb,cc)~([a]as,[b]bs,[c]cs)->(aaa -> [a] -> [a]forall t. t -> [t] -> [t]:[a]as,bbb -> [b] -> [b]forall t. t -> [t] -> [t]:[b]bs,ccc -> [c] -> [c]forall t. t -> [t] -> [t]:[c]cs))([],[],[])---------------------------------------------------------------- Error code---------------------------------------------------------------- Common up near identical calls to `error' to reduce the number-- constant strings created when compiled:errorEmptyList::String->aerrorEmptyList :: String -> aerrorEmptyListStringfun=String -> aforall a. String -> aerrorWithoutStackTrace(Stringprel_list_strString -> String -> Stringforall a. [a] -> [a] -> [a]++StringfunString -> String -> Stringforall a. [a] -> [a] -> [a]++String": empty list")prel_list_str::Stringprel_list_str :: Stringprel_list_str=String"Prelude."
[8]ページ先頭