import Base
import ./defs.bend as D
# lawful-halves: run a list of tasks in parallel, as a balanced fork-join.
#
# H.all(b, B, ts): the results of the tasks ts (D.Task values), in order
#
# H.tree splits the list into halves, down to single tasks; H.run runs a tree
# with one parallel call per Node, so k tasks take ceil(log2(k)) levels of
# forks. Bend's scheduler hands each forked call to one core and never moves
# it, so tasks of about the same cost keep the cores busy.
#
# The laws pin the results and the tree; check.sh pins that run forks at
# every Node (the parallel let in run, and its fork in the compiled C).
#
# Work on one core: before the first fork, measure walks the list once and
# build splits it once per level (O(k log k) steps for k tasks); after the
# last join, D.leaves flattens the results with nested appends (O(k log k)
# again). For a few dozen tasks this is a few hundred steps.
# n / 2, rounded down.
def half(n: Nat) -> Nat:
match n:
case 0n:
0n
case 1n+p:
match p:
case 0n:
0n
case 1n+q:
1n+half(q)
# n - n / 2: n / 2 rounded up.
def hi(n: Nat) -> Nat:
match n:
case 0n:
0n
case 1n+p:
match p:
case 0n:
1n
case 1n+q:
1n+hi(q)
# h in front of the first list of p.
def split.put(a, -A: Kind(a), h: A, p: List & List) -> List & List:
match p:
case (l, r):
(h <> l, r)
# The first k values of xs (all of them if fewer) and the rest, in one pass
# (the values may be ones that can be used only once).
def split(a, -A: Kind(a), xs: List, +k: Nat) -> List & List:
match xs k:
case Nil{} _:
(Nil{}, Nil{})
case h <> t 0n:
(Nil{}, h <> t)
case h <> t 1n+p:
split.put(a, A, h, split(a, A, t, p))
# One more value counted, h in front of the list.
def measure.put(a, -A: Kind(a), h: A, p: Nat & List) -> Nat & List:
match p:
case (n, t):
(1n+n, h <> t)
# The length of xs, and xs.
def measure(a, -A: Kind(a), xs: List) -> Nat & List:
match xs:
case Nil{}:
(0n, Nil{})
case h <> t:
measure.put(a, A, h, measure(a, A, t))
# x in front of the tree s: Leaf{x} if s is empty, else a Node.
def cons(a, -A: Kind(a), x: A, s: D.Tree) -> D.Tree:
match s:
case D.Tip{}:
D.Leaf{x}
case D.Leaf{y}:
D.Node{D.Leaf{x}, D.Leaf{y}}
case D.Node{l, r}:
D.Node{D.Leaf{x}, D.Node{l, r}}
# The values of xs as a right spine: Tip, a Leaf, or a Leaf and the rest.
def spine(a, -A: Kind(a), xs: List) -> D.Tree:
match xs:
case Nil{}:
D.Tip{}
case h <> t:
cons(a, A, h, spine(a, A, t))
# The values of p's first list, then its second.
def join(a, -A: Kind(a), p: List & List) -> List:
match p:
case (l, r):
List.append(a, A, l, r)
# The tree of the values of p = (l, r): l and r hold n / 2 and n - n / 2
# values (big: n >= 2), each split again, down to single values. f bounds the
# depth (f >= n is enough). The values stay in order whatever f and n are; a
# spine takes what is left at the bottom.
def build(a, -A: Kind(a), +f: Nat, big: Bool, +n: Nat, p: List & List) -> D.Tree:
match f big:
case 0n _:
spine(a, A, join(a, A, p))
case 1n+q False{}:
spine(a, A, join(a, A, p))
case 1n+q True{}:
(l, r) = p
+h = half(n)
+m = hi(n)
D.Node{build(a, A, q, Nat.is_lt(1n, h), h, split(a, A, l, half(h))),
build(a, A, q, Nat.is_lt(1n, m), m, split(a, A, r, half(m)))}
# The tree of the n values of xs, p = (n, xs).
def tree.k(a, -A: Kind(a), p: Nat & List) -> D.Tree:
(+n, xs) = p
build(a, A, n, Nat.is_lt(1n, n), n, split(a, A, xs, half(n)))
# The balanced tree of xs.
def tree(a, -A: Kind(a), xs: List) -> D.Tree:
tree.k(a, A, measure(a, A, xs))
# Every task of t called, the two subtrees of each Node in parallel (the
# parallel let below; check.sh requires it).
def run(b, -B: Kind(b), t: D.Tree<&1, D.Task>) -> D.Tree:
match t:
case D.Tip{}:
D.Tip{}
case D.Leaf{k}:
D.Leaf{D.call(b, B, k)}
case D.Node{l, r}:
x y = run(b, B, l) run(b, B, r)
D.Node{x, y}
# The results of the tasks ts, in order, run in parallel: the balanced tree of
# ts, run (law all_tree, proven by {==}; check.sh requires that proof).
def all(b, -B: Kind(b), ts: List<&1, D.Task>) -> List:
D.leaves(b, B, run(b, B, tree(&1, D.Task, ts)))