13

The documentation for scanl says "this function will fuse". What exactly does fuse mean here? Is it bad?

Rob Agar
  • 12,337
  • 5
  • 48
  • 63
  • 2
    For some insight into how it works, [check out the paper](http://www.cse.unsw.edu.au/~dons/papers/stream-fusion.pdf). It's a good read. – hammar May 17 '11 at 21:59

3 Answers3

13

http://www.haskell.org/haskellwiki/Short_cut_fusion

(and, for more information: http://www.haskell.org/haskellwiki/Correctness_of_short_cut_fusion)

If a function will fuse, that is a good thing. It means that a chain of functions can be merged into one function, which means less allocation, less stack, and more speed! Awesome!

Here's a trivial fusion: map f . map g ----> map (f . g).

As detailed above, there are many others that get applied by the RULES of the standard library as well.

sclv
  • 38,665
  • 7
  • 99
  • 204
3

It's kind of like coroutines - if you compose two functions they can execute on a fused stream instead of one being fully "evaluated" first then the second.

What is Haskell's Stream Fusion

Community
  • 1
  • 1
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
  • As that thread details, we don't have stream fusion by default. Instead, we have soemthing called build/foldr fusion (or short cut fusion) -- on which, see the links I've provided above. Fusion is awesome, and stream fusion is more awesome than build/fold, but as I recall there are a few unresolved edge cases that has kept it from being adopted in the standard libs. Meanwhile, other libs such as `Text` and `Vector`, as I reall, *do* use stream fusion. – sclv May 17 '11 at 21:16
2

I would assume it's referring to stream fusion optimization techniques, and stating that they can be applied to the function. Essentially, if you have two functions that transform sequences, the optimizer will merge the transformations together so only one traversal is required.

In short, no, it's not bad. Quite the opposite!

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302