CBCL / DocumentationDocs v0.3.1

Annotated CBCL dialects

These examples use complete definitions from the repository. Each code block contains one dialect. Save a block as a .cbcl file to check it with the CLI.

For complete protocols that use any and all, see Protocols and roles.

On this page

Bounded progress

Two hosts can receive concurrent ticks. Each host shows the greatest position that its evidence establishes.

(define count-to-one-hundred (cbcl) @anuna
  (extend tick (to) (tell to))
  (protocol (then begin (repeat 100 tick)))
  (state (number (progress tick))))
  1. Verb. The tick template sends a core tell to its recipient. It carries no numeric field.
  2. Protocol. The first tick follows begin. Each subsequent tick cites a predecessor that establishes the previous position.
  3. State. The field is named number. Its progress rule selects the greatest established position.

If two participants independently send a tick after position 99, each tick can reach position 100. The displayed number stays at 100. A count of distinct tick messages could exceed 100. See Protocols and roles.

Download count-to-one-hundred.cbcl

A checklist with roles

A checklist has one owner and a group of members. Members can change an item. Only the owner can remove an item.

(define checklist (cbcl) @anuna
  (:resource-requirements
    ((max-depth 16)
     (max-expansion-size 4096)))

  (:roles (owner (* member)))

  (extend open (to title) :from owner :to (owner member)
    (tell to :title title))

  (extend check (to item done clock) :from member :to (owner member)
    (tell to :item item :done done :clock clock))

  (extend drop (to item) :from owner :to (owner member)
    (tell to :item item))

  (shape open
    (require :title string))
  (shape check
    (require :item string)
    (require :done bool)
    (require :clock hlc))
  (shape drop
    (require :item string))

  (protocol
    (then begin open)
    (then open check)
    (then open drop))

  (state
    (title (initial open :title))
    (items (lww-map check :item :done :clock drop))))
  1. Cast. The root assigns the owner key and the member keys. Each verb sends to the owner and the members so participants can see the opener.
  2. Fields. A check carries an item name, a Boolean value, and a clock. A drop must specify the item name.
  3. Order. The verbs check and drop can follow the same open. A subsequent edit can refer to an earlier write through its replacement metadata.
  4. State. The title comes from the opener. The lww-map uses the item name as its key. The binder supplies the list of observed writes to replace.

Suppose Bob marks “venue” false. He sees his first write and changes the value to true. The owner then sees the revision and drops the item. The map no longer shows “venue”. A concurrent write can survive if the owner did not see it. See State language for map and removal rules.

Download checklist.cbcl

Transfer rights between accounts

A record of credits and debits gives the balance. A holder must have the rights to spend the amount in a transfer.

(define example-coin (cbcl) @coin-author
  (:resource-requirements ((max-depth 16) (max-expansion-size 4096)))
  (:roles (issuer (* holder)))
  (extend open (to name allocations) :from issuer :to (issuer holder)
    (tell to :name name :allocations allocations))
  (extend mint (to amount) :from issuer :to (issuer holder)
    (tell to :amount amount))
  (extend burn (to amount) :from holder :to (issuer holder)
    (tell to :amount amount))
  (extend pay (to recipient amount) :from holder :to (issuer holder)
    (tell to :recipient recipient :amount amount))
  (shape open (require :name string) (require :allocations list))
  (shape mint (require :amount integer))
  (shape burn (require :amount integer))
  (shape pay (require :recipient symbol) (require :amount integer))
  (protocol (then begin open) (then open mint) (then open burn) (then open pay))
  (state
    (name (initial open :name))
    (coins (bounded-counter
      (floor 0)
      (initial-rights open :allocations)
      (issuers issuer)
      (increment mint :amount)
      (decrement burn :amount)
      (transfer pay :recipient :amount)))))
  1. Open. The issuer supplies an allocation list. Each entry assigns integer base units to an account.
  2. Mint or burn. The issuer can mint. A holder can burn units subject to the available rights. The amount field must be an integer.
  3. Pay. The recipient of the rights is the :recipient field. The message itself goes to the issuer and holder roles.
  4. State. The floor is zero. The transfer moves rights between accounts. It preserves the total number of units.

If Alice has five units and pays Bob two, Alice has three and Bob gains two. With the default host-gate profile, the host must serialize issuance for each account. A field of type integer alone cannot authorize a payment. See Resource options and bounds for certification profiles.

Download example-coin.cbcl