import Base import ./defs.bend as D import ./halves.bend as H # lawful-halves: running a list of tasks in parallel gives what calling them # one after another gives. A task (D.Task) holds a closure Unit -> B with its # own input; D.force calls each task once, in order, and is the meaning of # every parallel run here. Results may be of any kind (b: &1 for values used # once, such as arrays; &2 for Data). # # The laws pin the results (all_force), the tree H.all runs (all_tree, which # holds by definition) and that tree's shape (tree_leaves, tree_balanced). No # equation can say that work runs in parallel: the checker reads a parallel # call as two plain calls. check.sh pins that run forks at every Node: the # proof of all_tree must be exactly {==}, halves.bend must keep run's parallel # let, and the compiled C must fork in run. # # For callers: all_force, all_force_back, all_append and all_length. The other # laws describe the parts. A task list can be used only once, in proofs too, # so each law uses up the list it is applied to; all_force_back hands the # list back (as us, equal to ts), so a caller can go on to other facts. # ========================================================================== # Running tasks # ========================================================================== # The results of H.all are the tasks' results, in the tasks' order. law all_force: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> {H.all(b, B, ts) == D.force(b, B, ts) : List} # all_force, handing the tasks back: us is ts, for the caller's next fact. law all_force_back: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> exs us: List<&1, D.Task> {us == ts : List<&1, D.Task>} & {H.all(b, B, ts) == D.force(b, B, ts) : List} # H.all is the balanced tree of the tasks, run. (The definition of H.all; # check.sh requires its proof to be {==}, so H.all cannot become anything # else while check.sh passes.) law all_tree: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> {H.all(b, B, ts) == D.leaves(b, B, H.run(b, B, H.tree(&1, D.Task, ts))) : List} # Running any tree of tasks gives the results of its tasks, left to right. law run_leaves: for -b: Quant for -B: Kind(b) for t: D.Tree<&1, D.Task> {D.leaves(b, B, H.run(b, B, t)) == D.force(b, B, D.leaves(&1, D.Task, t)) : List} # Running a tree keeps its shape. law run_shape: for -b: Quant for -B: Kind(b) for t: D.Tree<&1, D.Task> {D.shape(b, B, H.run(b, B, t)) == D.shape(&1, D.Task, t) : D.Tree<&2, Unit>} # Running two lists of tasks one after the other: the results of the first, # then those of the second. law all_append: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> for us: List<&1, D.Task> {H.all(b, B, List.append(&1, D.Task, ts, us)) == List.append(b, B, H.all(b, B, ts), H.all(b, B, us)) : List} # One result per task. law all_length: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> {List.length(b, B, H.all(b, B, ts)) == List.length(&1, D.Task, ts) : Nat} # ========================================================================== # The tree # ========================================================================== # The tree of a list holds the list's values, in order. law tree_leaves: for -a: Quant for -A: Kind(a) for xs: List {D.leaves(a, A, H.tree(a, A, xs)) == xs : List} # The tree of a list is balanced: at every Node, the halves have n / 2 and # n - n / 2 leaves (n / 2 rounded down), none empty. law tree_balanced: for -a: Quant for -A: Kind(a) for xs: List {D.balanced(D.shape(a, A, H.tree(a, A, xs))) == True{} : Bool} # The leaves D.count counts are the values D.leaves lists: so the tree of a # list of n values has n leaves (with tree_leaves). law count_leaves: for -a: Quant for -A: Kind(a) for t: D.Tree {D.count(D.shape(a, A, t)) == List.length(a, A, D.leaves(a, A, t)) : Nat} # ========================================================================== # Forcing # ========================================================================== # Forcing two lists one after the other: the results of the first, then those # of the second. law force_append: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> for us: List<&1, D.Task> {D.force(b, B, List.append(&1, D.Task, ts, us)) == List.append(b, B, D.force(b, B, ts), D.force(b, B, us)) : List} # One result per task. law force_length: for -b: Quant for -B: Kind(b) for ts: List<&1, D.Task> {List.length(b, B, D.force(b, B, ts)) == List.length(&1, D.Task, ts) : Nat}