Skip to content

Choice

Control flow in a distributed program is only meaningful if the roles that branch actually know which way to go. KlorPy enforces this: a branch is reachable only by roles that know the choice.

if

The guard must be located somewhere (a variable holding a scalar/agreement value). Two regimes:

Guard location Behavior
universal (every role) each role evaluates the guard locally and runs its own branch — no communication
a subset of roles one role in the subset decides and broadcasts the chosen branch to every other role
# universal guard: x is an agreement (unannotated) parameter
big = x >= 10
if big:
    r = A("big")
else:
    r = A("small")
# subset guard: A decides, B is told
is_even = A(x % 2 == 0)
if is_even:
    r = move(x, A, B)     # B only learns x on this path
else:
    r = B(abs(x))

The type checker additionally requires that a variable assigned inside branches gets the same location on every branch (see Types & parameters).

Guard spelling

The guard must be a variable, e.g. if big: — write the condition first: big = x >= 10. A bare if x >= 10: is rejected.

match / case

Python's match statement is supported over an agreement subject (every role evaluates it locally and runs the matching arm — no broadcast), with constant patterns and the _ wildcard:

match n:                  # n is an agreement (unannotated) parameter
    case 1:
        t = A("one"); tag = t
    case 2:
        t = A("two"); tag = t
    case _:
        t = A("many"); tag = t

If no arm matches and there is no wildcard, the whole match is a no-op.

Patterns

Only constant patterns (1, "x", True, None) and the _ wildcard are supported. Match guards (case 1 if cond:), OR-patterns, and structural patterns are rejected with a clear message.

Knowledge of choice, faithfully

The rule from Klor is: a role may participate in a branch only if it is part of the guard's agreement set. KlorPy implements the pragmatic version — a subset guard is decided by one role and broadcast, so even roles outside the guard set can participate (they're told which way it went). Universal guards skip the broadcast entirely. Both regimes keep every role in step.