Fusion laws
A fusion law says that two passes over the data give the same result as one pass. Two maps in a row equal one map, and two filters in a row equal one filter. This page lists Garo's fusion laws, shows each one on data, and says exactly when each one holds.
The laws matter for two reasons. You can refactor with them, knowing the meaning stays the same. And an engine can use them to run a pipeline in one loop, without building the arrays in between.
Two kinds of law
A surface law has both sides written in Garo. You can run both sides, and they give the same value. Map-map fusion is one: the two maps and the one composed map are both programs.
An engine law has no Garo spelling. It says an engine may run a pipeline in fewer passes, but it changes nothing you can write. Map-reduce fusion is one: a sum of a map may run as one loop, and no program says "as one loop".
Each law below says which kind it is, and how it has been checked:
- checked:
garo lawsruns both sides on random data and compares them; - proved: the law is a theorem about the Lean interpreter, so it holds on every input that meets its conditions.
Every example on this page runs on the same interpreter as the REPL.
Map-map fusion
Two maps in a row equal one map that does both:
G기 (F기 data) = {G기 (F x)}기 data
G기 (F기 data) = (F.G)기 data when F gives one scalar per element
Add one, then double, in two passes:
{x × 2}기 ({x + 1}기 [1, 2, 3])The same, in one pass with one function:
{(x + 1) × 2}기 [1, 2, 3]F.G composes two functions, F first. It gives the same one pass:
({x + 1}.{x × 2})기 [1, 2, 3]The second line holds only when F gives one scalar for each element. The outer map hands G one element of F's result at a time, so when F gives an array, G runs on that array's elements, not on the whole array. Here F gives a pair for each element:
{[x, x]}기 ({[x, x × 2]}기 [1, 2])Composing hands G the whole pair, so the result differs:
({[x, x × 2]}.{[x, x]})기 [1, 2]The first line, {G기 (F x)}기, still holds: G maps over F's pair, as the outer map did.
Surface. Checked for Int and Float, the first line with functions that give arrays. Proved.
Select-select fusion
Two filters in a row equal one filter that asks both questions:
G니 (F니 data) = {F x and G x}니 data
Keep what is above 1, then what is below 4:
{x < 4}니 ({x > 1}니 [3, 1, 4, 2])One filter with both conditions. ⌊ is the smaller of two values, and on Bools it is "and", since false is less than true:
{(x > 1) ⌊ (x < 4)}니 [3, 1, 4, 2]Surface. Checked for Int. Proved.
Filter before map
Filtering first saves work, because the map then skips the elements the filter would drop. The two orders agree only when the filter's question has the same answer before and after the map:
F기 (P니 data) = P니 (F기 data) only when P (F x) = P x
Doubling keeps a number's sign, so "is it positive?" has the same answer either side of the map:
{x × 2}기 ({x > 0}니 [-1, 2, 3]){x > 0}니 ({x × 2}기 [-1, 2, 3])"Is it above 2?" changes when the number doubles, so the two orders differ:
{x × 2}기 ({x > 2}니 [1, 2, 3]){x > 2}니 ({x × 2}기 [1, 2, 3])When the filter keeps nothing, both orders give an empty array of the map's result type.
Surface, under the condition. Checked for Int. Proved under the condition.
Map-reduce fusion
When a map feeds a reduce, an engine may run the two as one loop: it applies the function and adds the result to the total in one traversal, and never builds the mapped array. This is the most important fusion law, because "transform, then total" is the most common pipeline.
The one loop does the same operations in the same order as the two passes. It starts at the reduce's identity, then folds left, combining each element's mapped value. So the result is identical, Float included.
*Engine. It has no Garo spelling: F다 means something else.*
One surface law covers max. For a function that never decreases, the max of the mapped data is the function of the max, so the function runs once instead of once per element:
닥 (F기 data) = F (닥 data) for non-decreasing F, and data with an element
Adding 3 never decreases a number:
닥 (긷 3 [3, 1, 4])긷 3 (닥 [3, 1, 4])Surface. Checked for Int with non-decreasing functions. Proved for every ordered type.
Fusion on rows, columns and groups
Map-map fusion holds for the other units a map can work on: the rows of a matrix (가), its columns (거), the rows of a table, and dispatch over the groups of a key (개).
G가 (F가 m) = {G (F x)}가 m
G개 k (F개 k d) = {G k (F k g)}개 k d
Here x is a whole row, so the functions work on vectors:
{x × 2}가 ({x + 1}가 [[1, 2], [3, 4]]){(x + 1) × 2}가 [[1, 2], [3, 4]]Both forms hand G the same units in the same order, so this law needs no condition on G. It does need F to give each unit back in a form the next pass can cut into units again. On rows, F must give every row a vector of one length and one type. A scalar per row drops the axis, and then the second pass sees a vector, not rows:
{닫 x}가 ({닫 x}가 [[1, 2], [3, 4]])The first pass sums each row into [3, 7], and the second sums that. Fused, each row's sum is summed again, and a reduce refuses a scalar:
{닫 (닫 x)}가 [[1, 2], [3, 4]]The input also needs at least one unit, and F must not fail.
Engine. Proved, for rows, columns, table rows and dispatch.
Partition-aggregate fusion
A keyed aggregate (the ㅐ flow) equals grouping first and then aggregating each group (the ㅔ flow):
keyed_f key data = grouped_f (data[첮 key data])
댇 sums by key in one step:
댇 [`a, `b, `a] [1, 2, 3]첮 builds the partition, indexing by it gives the groups, and 덷 sums each group:
덷 [1, 2, 3][첮 [`a, `b, `a] [1, 2, 3]]The keyed form is the one to write for a single aggregate. The grouped form pays off when several aggregates share one partition: build it once, then sum, count and sort within its groups. The law lets an engine fuse the grouped form into one pass, or share one partition among several consumers.
The two forms can fail differently. The keyed form checks the aggregate's type before it groups, so on bad data the two report different first errors. An engine that rewrites one into the other must report the error of the form you wrote.
Surface. Checked for every aggregate final, mean included, as a keyed reduce and a keyed scan. It holds by construction for programs that succeed, since both forms are one aggregate over one partition.
Merge fusion
Aligning two keyed tables and then combining their paired columns runs as one keyed traversal. The same goes for the keyed lift, (F @ G)백 a b, which joins the keys, fills each side's gaps by policy G, and combines with F.
When F is associative and commutative (+, ⌈, ⌊) and the fill is F's identity, the fused merge is associative and commutative too. Then a new batch of data can be merged into an existing result without recomputing from the original inputs. A mean is not associative, so it is kept as a sum and a count, merged separately, and divided at the end.
Engine. It has no Garo spelling.
When fusing changes the error
These laws are stated for functions that do not fail and whose results have one shape and one element type. Fusing reorders evaluation. Unfused, F runs on every element before G runs on any. Fused, the two take turns. So when F or G can fail, the two forms can meet a different error first:
{x + `a}기 ({1 ÷ x}기 [1, 0])Unfused, F runs on both elements first, and 1 ÷ 0 fails. Fused, G runs on F's first result before F reaches the zero, and adding a symbol to a number fails:
({1 ÷ x}.{x + `a})기 [1, 0]An engine that fuses must still report the unfused form's first error, so fusion never changes what a program says when it fails.
The other condition is about shape. Unfused, the first map stacks F's results into an array before G sees them, and stacking refuses results of different shapes and turns a mix of Ints and Floats into Floats. Fused, G gets each result unstacked. The two agree when F's results have one shape and one type.
The laws at a glance
| Law | Says | Kind | Status |
|---|---|---|---|
| Map-map | G기 (F기 d) = (F.G)기 d | surface | checked, proved |
| Select-select | G니 (F니 d) = {F x ⌊ G x}니 d | surface | checked, proved |
| Filter before map | F기 (P니 d) = P니 (F기 d) when P (F x) = P x | surface | checked, proved |
| Map-reduce | a reduce of a map may run as one loop | engine | — |
| Max of a map | 닥 (F기 d) = F (닥 d) for non-decreasing F | surface | checked, proved |
| Rows, columns, groups | G가 (F가 m) = {G (F x)}가 m | engine | proved |
| Partition-aggregate | 댇 k d = 덷 d[첮 k d] | surface | checked |
| Merge | align then combine runs as one keyed traversal | engine | — |
The spec's §6 states these laws in full, along with the sort, shape, scan and combine laws.