[Haskell-cafe] missing optimization for (++)

Ben Franksen ben.franksen at online.de
Sun Mar 4 09:32:50 UTC 2018


I found that in base [1] we have

(++) []     ys = ys
(++) (x:xs) ys = x : xs ++ ys

I had expected there to be a clause

(++) xs     [] = xs

at the top, which would avoid the list traversal in this common case.

Is this somehow magically taken care of by the

{-# RULES
"++"    [~1] forall xs ys. xs ++ ys = augment (\c n -> foldr c n xs) ys
  #-}

? No, inlining @augment g xs = g (:) xs@ gives me

(\c n -> foldr c n xs) (:) ys

= foldr (:) ys xs

which still traverses xs even if ys=[].

Any thoughts?

[1]
http://hackage.haskell.org/package/base-4.10.1.0/docs/src/GHC.Base.html#%2B%2B

Cheers
Ben



More information about the Haskell-Cafe mailing list