Skip to content

phyelds.calculus

phyelds.calculus

Core syntax of phyelds: the primitives aggregate programs are made of.

@aggregate marks a function as an aggregate function:

@aggregate
def my_function():
    ...

Inside it, the core syntax is:

  • remember: keeps a value across rounds.
  • remember_and_evolve: keeps a value across rounds, updating it with a function.
  • neighbors: shares a value with the neighbors and returns their values.
  • exchange: evolves a state and sends values (possibly distinct per neighbor) in one step.
  • align: scopes the calls inside it, so that they only align with the same scope on the neighbors.

aggregate(func)

Mark a function as an aggregate function, returning a wrapper that aligns its calls.

Each call enters a new alignment point named after the function, so the values a device shares inside it only meet those of neighbors running the same call. The branches of the if statements and conditional expressions in its body are aligned too, so devices on different branches do not exchange values.

Example
@aggregate
def my_function():
    ...

Parameters:

Name Type Description Default
func Callable

The function to wrap.

required

remember(init)

Keep a value across the rounds of the current device.

On the first round the state holds init; afterwards it holds the value last passed to the update function.

Example
count, set_count = remember(0)
count = set_count(count + 1)  # count is now 1 more than in the previous round

Parameters:

Name Type Description Default
init T

The value of the state on the first round.

required

Returns:

Type Description
Tuple[T, Callable[[T], T]]

A tuple (state, set_state): state is the current value, and set_state stores a new value for the next round and returns it.

remember_and_evolve(init, evolve_fn)

Keep a value across rounds, replacing it every round with evolve_fn of itself.

Example
rounds = remember_and_evolve(0, lambda count: count + 1)  # 1 on the first round

Parameters:

Name Type Description Default
init T

The value the state starts from, before the first evolution.

required
evolve_fn Callable[[T], T]

Function that receives the current value and returns the new value.

required

Returns:

Type Description
T

The value after this round's evolution: evolve_fn(init) on the first round.

neighbors(value)

Share value with the neighbors and gather the values they shared at this point.

Example
temperatures = neighbors(sense("temperature"))  # one temperature per neighbor
hottest = temperatures.max()

Parameters:

Name Type Description Default
value T

The local value to share; it cannot be a NeighborhoodField.

required

Returns:

Type Description
NeighborhoodField[T]

A NeighborhoodField mapping the current device and each neighbor that shared a value at the same alignment point to that value.

Raises:

Type Description
TypeError

If value is a NeighborhoodField.

exchange(init)

Exchange values with neighbors and across rounds at the current alignment point.

Parameters:

Name Type Description Default
init Union[T, NeighborhoodField[T]]

Initial value (or field) used when no previous value is available.

required

Returns:

Type Description
Tuple[T, NeighborhoodField[T], Callable[[Any], Any]]

A tuple (local, field, send) containing the local state value, the NeighborhoodField received in the current round, and the send function for the next round.

align_right()

Scope the else branch of a conditional, as align("right").

@aggregate already aligns the branches of if statements and conditional expressions; align_left and align_right do it by hand.

Example
if condition:
    with align_left():
        ...  # devices where condition holds
else:
    with align_right():
        ...  # the other devices

align_left()

Scope the if branch of a conditional, as align("left"); see align_right.