Skip to content

2. Verification by design

This chapter describes the design. The program below is checked by len check and planned by len plan today; nothing yet proves it or runs its tests.

Some requirements cannot be seen in one run of a program. That an order is placed once, however many times the request arrives, is a statement about every pair of requests, including two that arrive at the same moment. A test can show a case where it holds. Only an argument shows that it always does.

Verification by design means the requirement is written down first, as a claim, and the design is then made to answer it in two ways:

  • by a proof, in a logic, that the rule the design follows implies the requirement;
  • by a program with its tests, in a programming language, as evidence that the code follows the rule.

Neither replaces the other. The proof is about the rule and says nothing about the code. The tests are about the code and cover only the cases they run.

The running example: an order placed once

A client sends a request to place an order. The network may deliver it twice, or the client may retry. Each request carries an idempotency key, and the requirement is that requests with one key create one order.

The ontology says what there is. The claim says what is required:

module orders

type Request
type Key
type Order

rel carries(request: Request, key: Key)
rel creates(request: Request, order: Order)
rel same_order(a: Order, b: Order)

// Requests that carry one key create one order.
rel idempotent(r: Rel(Request, Order))

claim PlacedOnce {
    idempotent(creates)
}

A context in first-order logic says what the claim means, and a goal asks for the proof:

context Spec {
    logic: fol
    use Request, Key, Order, carries, creates, same_order
}

formal PlacedOnce in Spec {
    forall a, b: Request, k: Key, x, y: Order {
        (carries(a, k) and carries(b, k) and creates(a, x) and creates(b, y))
        implies same_order(x, y)
    }
}

goal OrdersPlacedOnce {
    prove PlacedOnce in Spec
}

len check accepts the program. len plan lists the proof, and says plainly that nothing can run it yet:

Run:
  x no run    prove PlacedOnce in Spec   not runnable: no backend for logic `fol`
  = 0 results current

Write:
  = 0 resources unchanged

Plan: 0 to run, 1 not runnable, nothing to write.

Where the chapter goes

These sections are planned:

  • The rule the design follows: a key is stored with its order, and the store refuses a second order for a stored key. Stated as assumptions of the context.
  • The proof that the rule implies PlacedOnce.
  • The same claim in a programming language: the signature of the function that places an order, and tests that send one request twice and two requests at once.
  • What the tests are worth: evidence for the claim, and one failing case as evidence against it.
  • Concurrency: why two requests at the same moment are the case a test is least able to cover, and what the proof assumes about the store.
  • A second example: an order is never shipped before it is paid, where the types of the program carry the claim.