CBCL / DocumentationDocs v0.3.1

Write a poll dialect

In this example, a participant can propose lunch. Each voter can change a previous vote. The poll shows the initial question, the available choices, and the current ballot for each voter. The dialect uses fields for these three values.

Save this definition as lunch-vote.cbcl. The JavaScript and Rust receivers use this file.

(define lunch-vote (cbcl) @anuna
  (:state-bounds (max-string 280) (max-list 24))

  (extend propose (to question options)
    (tell to :question question :options options))
  (extend vote (to choice clock)
    (tell to :choice choice :clock clock))

  (shape propose (require :question string) (require :options list))
  (shape vote (require :choice string) (require :clock hlc))

  (protocol (then begin propose) (then propose vote))

  (state
    (question (initial propose :question))
    (options (initial propose :options))
    (domain vote :choice options)
    (ballots (lww-per-signer vote :choice :clock))
    (tally (histogram ballots))))
On this page

Decide what a message carries

extend gives each verb an expansion into the base language. The two messages use tell to send information to a recipient. In the expansion, choice names a parameter and :choice names the field that contains its value. Only a string can satisfy the shape for that field. Quoted "Sushi" is a string, while bare Sushi is a symbol.

The bounds limit strings and lists carried by state-bearing messages. Change max-list to 1. The installer accepts this dialect. The two-choice opener in the walkthrough now fails when the host checks its shape.

Decide which messages can follow

begin marks the start of a thread. A proposal can open a thread and a vote can cite that proposal. Two participants can vote independently against the same proposal. The host can establish the causal relationship from the cited act’s address even if the messages arrived in reverse order.

Decide what counts

The first two state fields read the opener. The domain rule uses its options as the permitted choices. The binder refuses a local intent for a choice outside this domain. Admission can accept a vote for a choice that is absent from the domain. The state rules then exclude that vote from the tally.

If Bob changes his vote, a count of vote messages includes two votes for Bob. The ballots map selects a current value for each authenticated signer. The histogram counts values in that map. Define ballots before tally because derived fields refer to earlier fields.

Use this file in a JavaScript host or a Rust host. For a different state model, see Select a state rule.

For all definition clauses, see the CBCL language reference. For complete definitions with commentary, see Annotated dialects.