| @@ -44,11 +44,13 @@ A direct call of a recursive higher-order function whose closure argument is a k |
| 44 | 44 | ### Fusion, and purity (issue #198) |
| 45 | 45 | Rules on `std/list` pipeline shapes, fired only when every function argument of the stages involved is pure and the intermediate list is used once: |
| 46 | 46 | |
| 47 | | −- `map(map(xs, f), g)` → `map(xs, fn(x) { g(f(x)) })`; `filter(map(xs, f), p)` → `filter(xs, fn(x) { p(f(x)) })` |
| 47 | +- `map(map(xs, f), g)` → `map(xs, fn(x) { g(f(x)) })`; `each(map(xs, f), g)` likewise |
| 48 | 48 | - `fold(map(xs, f), z, g)` → `fold(xs, z, fn(a, x) { g(a, f(x)) })` |
| 49 | 49 | - `fold(filter(xs, p), z, g)` → `fold(xs, z, fn(a, x) { if p(x) { g(a, x) } else { a } })` |
| 50 | 50 | - `fold(range(a, b), z, g)` → a counting loop |
| 51 | | −- `length(map(xs, f))` → `length(xs)`, and `each(map(xs, f), g)` likewise |
| 51 | +- `length(map(xs, f))` → `length(xs)` |
| 52 | + |
| 53 | +There is no `filter ∘ map` rule: `filter(map(xs, f), p)` is not `filter(xs, p ∘ f)` — the map changes the *elements*, not just the predicate. (The epic's pipeline fuses completely anyway: `fold` of a `filter` of a `map` of a `range` fires `fold ∘ filter`, then `fold ∘ map`, then `fold ∘ range`.) Fusing a map through a filter is stream fusion's business (issue #199). |
| 52 | 54 | |
| 53 | 55 | Not fused: an effectful stage, `try_map`, `flat_map`, `zip`, or an intermediate used twice. A `perform`, `handle`, `region` or `defer` anywhere in a stage's function makes it effectful for this gate (a local `handle` that masks its own effect would do — the purity analysis is deliberately intraprocedural there, and can be revisited if real pipelines need it). |
| 54 | 56 | |