I'm posting this as a top-level comment, but it's really a reply to the discussion downthread about compilers being able to work magic if we let them. Better still, why not help them?<p>Something I took for granted for the longest time about Haskell (which remains the only language I know of with the feature) is the ability to write user-defined "rewrite rules". You can say, "okay, GHC, I know for a fact that if I use these functions in such-and-such way, you can replace it by <i>this</i> instead".<p><pre><code> {-# RULES
foo x (bar x) =
superOptimizedFooBar x
#-}
</code></pre>
A rule like this would be based on the programmer's knowledge of FooBar theory, which tells her that such an equality holds. The compiler hasn't studied lax monoidal FooBaroids and cannot be expected to infer this on its own. :)<p>Now, anywhere a user of this code writes something like<p><pre><code> foo [1,2,3] (bar [1,2,3])
</code></pre>
the compiler will substitute<p><pre><code> superOptimizedFooBar [1,2,3]
</code></pre>
in its place. This is a nice way to bring the compiler "closer" to the programmer, and allow the library author to integrate domain-specific knowledge into the compiler's optimizations.<p>You can also "specialize" by using faster implementations in certain cases. For example,<p><pre><code> timesFour :: Num a => a -> a
timesFour = a + a + a + a
timesFourInt :: Int -> Int
timesFourInt x
= rightShift x 2
{-# RULES
timesFour :: Int -> Int
= timesFourInt
#-}
</code></pre>
If you call timesFour on a double, it will use addition (ha!) but using it on an Int uses bitshifting instead because this rule fires.<p>High-performance Haskell libraries like vector, bytestring, text, pipes, or conduit <i>capitalize</i> on this feature, among other techniques. When compiling code written using libraries like this, this is how it goes:<p>- rule #1 fires somewhere
- it rewrites the code into something that matches rule #2, "clearing the way" for it to fire
- rule #2 fires
- rule #3 fires
- rule #1 fires again
- rule #4 fires<p>and so on, triggering a "cascade" of optimizations.<p>The promise of Haskell is that we already have a "sufficiently smart compiler": <i>today</i>, with good libraries, GHC is capable of turning clear, high-level, reusable functional code with chains of function compositions and folds and so on into tight, fast loops.<p>--<p>I must add, though, that getting rewrite rules to fire in cascades to get "mad gainz" requires one to grok how the GHC inliner/specializer works.<p><a href="http://mpickering.github.io/posts/2017-03-20-inlining-and-specialisation.html" rel="nofollow">http://mpickering.github.io/posts/2017-03-20-inlining-and-sp...</a><p>Data.Vector also utilizes an internal representation that makes fusion explicit and hence predictable (inevitable, even) called a "bundle":<p><a href="https://www.stackage.org/haddock/lts-8.16/vector-0.11.0.0/Data-Vector-Fusion-Bundle.html" rel="nofollow">https://www.stackage.org/haddock/lts-8.16/vector-0.11.0.0/Da...</a><p>but this relies on rewrite rules too, e.g. the previous module contains this rule:<p><pre><code> {-# RULES
"zipWithM xs xs [Vector.Stream]" forall f xs.
zipWithM f xs xs = mapM (\x -> f x x) xs #-}</code></pre>