Principles/03 of 06

Rules, not code.

You say what is allowed and what follows from it. Mandala derives the rest: the state a record keeps, the screens to work on it, the API, the tables and the search. The rule is the program, and anyone on the team can read it.

Planet Express · Monday, 10:12

Fry finds 120 crates of waffle irons bound for the Moon and decides to save everyone time by shipping them now.

He can’t. Neither can the accomplice, and neither can Hermes. The delivery contract says nothing goes to the Moon until a second person confirms it, and the contract is checked before anything happens.

Nobody wrote code to stop Fry. Hermes wrote one line about the Moon, months ago, and Leela approved it.

Accomplice · 10:12
Shipping 120 crates of waffle irons for Lumon to the Moon.
Refused · rule 5 of Ship

Nothing goes to the Moon until a second person confirms it. I’ve asked Leela.

state.status == Confirmed or state.destination != "Moon"
Waiting for Leelaexplain · com.planetexpress.delivery
01 · Declare

Say what is allowed.

A contract is a short text in DHL, a small language made for business rules. It names the state a record keeps, the actions that may change it, and what must hold for each one. That is all there is to write.

No back door

The check happens before anything is written, for people, the accomplice and outside apps alike. There is no admin screen that skips it.

You don’t have to write it

Describe the rule in your own words. Your accomplice drafts the DHL, tries it on a scratch copy and shows what it would accept and refuse. A person signs it before it is installed.

Rules change by rule

A new version names the exact version it replaces and how existing records move across. The move is itself an action, checked and recorded.

com.planetexpress.deliveryv3 · DHL
# Nothing ships to the Moon, or over 200 units,
# without a second person confirming (Leela).
action Ship()
    authorize dhl
    validate
        state.paid or state.credit_terms
        state.crates_checked == state.crates
        state.status == Requested or state.status == Confirmed
        state.status == Confirmed or state.units <= 200
        state.status == Confirmed or state.destination != "Moon"
    apply
        state.status = Shipped
The whole contract (65 lines)
aggregate Delivery
    state
        public customer: Text(len=200)
        public destination: Text(len=100)
        public units: Int = 0
        public crates: Int = 0
        public crates_checked: Int = 0
        public paid: Bool = false
        public credit_terms: Bool = false
        public requested_by: Identity
        public confirmed_by: Identity
        public status: Enum(New, Requested, Confirmed, Shipped) = New
 
action Request(customer: Text(len=200), destination: Text(len=100), units: Int, crates: Int, credit_terms: Bool)
    authorize dhl
    validate
        state.status == New
        units > 0
        crates > 0
    apply
        state.customer = customer
        state.destination = destination
        state.units = units
        state.crates = crates
        state.credit_terms = credit_terms
        state.requested_by = context.signer
        state.status = Requested
 
action CheckCrate()
    authorize dhl
    validate
        state.status == Requested
        state.crates_checked < state.crates
    apply
        state.crates_checked = state.crates_checked + 1
 
action Confirm()
    authorize dhl
    validate
        state.status == Requested
        context.signer != state.requested_by
    apply
        state.confirmed_by = context.signer
        state.status = Confirmed
 
action Pay()
    authorize dhl
    validate
        state.status != New
        state.paid == false
    apply
        state.paid = true
 
# Nothing ships to the Moon, or over 200 units,
# without a second person confirming (Leela).
action Ship()
    authorize dhl
    validate
        state.paid or state.credit_terms
        state.crates_checked == state.crates
        state.status == Requested or state.status == Confirmed
        state.status == Confirmed or state.units <= 200
        state.status == Confirmed or state.destination != "Moon"
    apply
        state.status = Shipped

Real DHL, exactly as it runs: this contract compiles. Every action is checked against it before it happens, for people and AI alike.

02 · Derive

Everything else follows.

In an ordinary application the rule above would be spread over a database schema, validation code, an API layer, permission checks, an admin screen and an audit table, and they would drift apart. Here they are all read off the one contract.

State

What a record is now: its facts, added up.

today
Lifecycle

Named steps, and only the moves the actions allow.

today
Tables and record pages

Lists you can filter and sort, a page per record, a button per action.

today
API

REST, GraphQL and gRPC descriptions generated from the contract.

today
Views and totals

Queries and projections declared next to the rules, kept current.

today
Text search

Ranked search over the fields you name.

today
Tools for the accomplice

Each action and query becomes something it can ask for, under the same check.

today
Vector, graph and time indexes

Declared in the contract like the rest.

coming
Delivery · statusEnum(New, Requested, Confirmed, Shipped)
  1. New
  2. Requestanyone on the crew
  3. Requested
  4. Confirmnot the one who asked
  5. Confirmed
  6. Shippaid or on credit, every crate checked
  7. Shipped

A small delivery may go straight from Requested to Shipped. Over 200 units, or anything bound for the Moon, has to pass Confirmed first.

std.commerce.published_publicationsexcerpt
projection PublishedPublications
    trigger action.*
    source state
    batch_window 0ms
    query
        std.commerce.storefront_publication
        | where published == true
        | select subject, seq, property, market, channel, product_subject, category_subject, slug, subtitle, description, image_path, featured, sort_order
    emit upsert published_publications

Real: a projection declared in a contract. Every action on a storefront publication updates the published list, with no delay.

03 · Replay

Same history, same answer.

A rule is evaluated the same way everywhere: no clock it reads on its own, no network call, no randomness, no AI model inside a decision. Give the same contract the same facts in the same order and it reaches the same state, on your laptop, on your server, or on an auditor’s machine.

A contract has a fingerprint

Compiling a contract gives it an id made from its exact content. A name alone never identifies what runs.

Views can be rebuilt

Throw every table and index away and the facts rebuild them, identically. Today a restart does exactly that.

Exact where it decides

When the new index kinds arrive, a decision may only use exact ones. Approximate search is for reading, never for deciding.

com.planetexpress.delivery · v3.0.0compiled
source65 lines of DHL
compiles toa schema and a small program
artifact2756b4d993223d59b2c21a4a63b403cce008502c9c882cffb5a07030832acb8e

The real fingerprint of this contract. Change one character of a rule and it changes. Every record says which fingerprint decided it, so “which rules were in force?” has an exact answer.

Status

What works today, and what’s coming.

today

DHL contracts with state, actions, validation, lifecycles, queries and projections; refusals that name the rule; a screen file per contract that gives tables, record pages and plain sentences for each rule; generated API descriptions; upgrades with migrations; a language server and the Workbench to write and try contracts, with diagnostics and completion.

coming

Contracts that declare vector, graph and time-series indexes and query them together (built on a branch, not released). Installing a contract without a restart.

to know

A rule is only as good as what its authors thought of. Mandala guarantees that the rule you wrote is the rule that ran. It can’t guarantee you wrote the right rule, which is why rules are short, readable and signed by a person.

Code says how. A rule says what must be true.

Is DHL a programming language?

A small one, on purpose. It can say what a record holds, who may do what, and what must hold. It can’t open a socket or loop forever, and that is what makes it checkable.

What about everything rules can’t say?

Talking to the outside world is done by connectors, with their own declared access and approvals. The pages of a public website stay in a frontend framework, with Mandala as the backend behind it. How Dharma works

Can I see more real DHL?

The Dharma and Karma pages show contracts from the standard library, and the docs have the language reference.