Skip to content

Building Blocks Reference

Samuele95 edited this page Jan 31, 2026 · 1 revision

Building Blocks Reference

The building blocks are self-stabilising library functions built on top of the field calculus primitives. They are the recommended API for writing aggregate programs.

G Block — Gradient

Computes the shortest-path distance from source devices.

dist = gradient(ctx, is_source)
  • Sources return 0.0
  • Non-sources return the minimum neighbour_distance + edge_weight
  • Converges in O(diameter) rounds
  • Alias: dist_to(ctx, source)

Broadcast (G-derived)

Propagates a value outward from source devices:

value = broadcast(ctx, is_source, "hello")

Every device receives the value from its nearest source.

C Block — Collection

Aggregates data inward along a potential field (toward lower potential).

total = collect(ctx, potential, lambda a, b: a + b, local_value, 0)
  • potential — a distance field (data flows downhill)
  • acc — associative binary operator
  • local — this device's contribution
  • null — identity element for acc

S Block — Sparse (Leader Election)

Elects uniformly-spaced leaders across the network.

is_leader = sparse(ctx, grain=3.0)
  • Returns True for elected leaders, False otherwise
  • Leaders are separated by at least grain distance
  • Ties broken by device ID

T Block — Time

Timer

Countdown timer that decreases each round.

remaining = timer(ctx, duration=10.0)  # counts down to 0

Decay

Exponential smoothing of a value.

smoothed = decay(ctx, raw_value, rate=0.9)

Composite Patterns

Channel

Logical corridor between source and destination regions.

on_channel = channel(ctx, is_source, is_destination, width=0.5)

Composition: G(source) + G(destination) <= shortest_path + width

Partition

Divides the network into regions using sparse leaders.

leader_id = partition(ctx, grain=4.0)

Distributed Average

Computes the regional average of a sensor value.

avg = distributed_average(ctx, sensor_value)

Clone this wiki locally