# lawful-halves Run a list of tasks in parallel in Bend. `H.all` splits the list into a balanced tree and runs the two halves of every node at the same time, and its results are exactly what calling the tasks one after another, in order, would give. That equality is a law that Bend's proof checker has verified, so you can write code with plain sequential meaning, run it in parallel, and still prove things about it. ## Use Bend fetches these imports from [BendHub](https://hub.bend-lang.com/name/lawful-halves) by name: ```python import lawful-halves@0.1.0.1/defs.bend as D # tasks and trees import lawful-halves@0.1.0.1/halves.bend as H # the parallel runner import lawful-halves@0.1.0.1/LAWS.bend as L # the laws, for your own proofs import lawful-halves@0.1.0.1/PROOF.bend as P # their proofs, which a proof using a law needs loaded ``` A task is a closure that carries its own input. Make one per piece of work and hand the list to `H.all`: ```python import Base import lawful-halves@0.1.0.1/defs.bend as D import lawful-halves@0.1.0.1/halves.bend as H # The sum of n numbers from lo. def sum(n: Nat, +lo: U32) -> U32: match n: case 0n: 0 case 1n+p: (lo + sum(p, (lo + 1 : U32)) : U32) # A task for the i-th block of 1000 numbers. def block(+i: U32) -> D.Task<&2, U32>: D.Task{u => sum(1000n, (i * 1000 : U32))} # Four blocks, summed in parallel; the sums come back in order. def main() -> List<&2, U32>: H.all(&2, U32, [block(0), block(1), block(2), block(3)]) ``` ``` $ bend main.bend [499500, 1499500, 2499500, 3499500] ``` Results can be of any kind: `&2` for data such as `U32`, `&1` for values used once, such as arrays. The halves hold the same number of tasks, so give tasks about the same amount of work. ## What's in it | name | signature (short) | what it does | |---|---|---| | `D.Task` | `Task{fn: Unit -> B}` | a task: a closure holding its own input | | `D.call` | `Task -> B` | runs one task | | `D.force` | `List -> List` | runs each task once, in order (the meaning of `H.all`) | | `H.all` | `List -> List` | runs the tasks in parallel; results in the tasks' order | | `H.tree` | `List -> Tree` | the balanced tree of a list (halves of `n / 2` and `n - n / 2`) | | `H.run` | `Tree -> Tree` | runs a tree of tasks, both subtrees of every node in parallel | | `D.Tree` | `Tip \| Leaf{x} \| Node{l, r}` | a binary tree of values | | `D.leaves` | `Tree -> List` | the values of a tree, left to right | | `D.shape` | `Tree -> Tree` | the tree with every value replaced by `Unit{}` | | `D.count` | `Tree -> Nat` | the number of leaves of a shape | | `D.balanced` | `Tree -> Bool` | whether at every node the right half has as many leaves as the left or one more, neither empty | The law you will usually want is `all_force`: `H.all(b, B, ts) == D.force(b, B, ts)`. A law is used like a function, so `L.all_force(b, B, ts)` is a proof of that equation (with `LAWS.bend` and `PROOF.bend` imported). `test/caller.bend` shows a full proof built on it. The laws are proven; below 1.0 they have been reviewed by AI models but not yet by a human expert. [docs/LAWS.md](https://github.com/luiscleto/lawful-halves/blob/main/docs/LAWS.md) lists them all in plain words. ## License MIT (see `LICENSE`).