Morpholog

Write business rules once. Build less software around them. Morpholog is the open-source foundation for correct, reliable and auditable business software.

Open source under Apache 2.0, on Rust and PostgreSQL.

Morpholog on GitHub

AI writes the code faster. The rules still have to hold.

Business software is full of rules. An order must not use more stock than exists. A payment needs approval. A trade must stay within its limits.

A typical application copies these rules into its services, queries, APIs and batch jobs. The copies drift apart. A mistake may only show after money, stock or authority has moved.

More code, written faster, does not help if someone still has to check every copy.

Who comes out ahead

Teams that keep their rules in one place can let AI build around them. Each new screen, adapter or report calls the same approved actions. Nothing new needs its own copy of the rules.

Teams whose rules live in every service get another copy with each feature an agent writes. Review becomes the bottleneck. The gaps show up in production.

Build less software

Morpholog makes business rules explicit, enforces them in one place and removes much of the code that repeats them. The rulebook defines the key records, the business actions and the rules they must respect. Ordinary code handles the interface, outside calculations and connections to other systems.

Agents propose. An approved rulebook decides. A person, an application or an AI agent proposes a change. The rules apply the same way, whoever proposed it. No switch skips them.

The result is business software that is cheaper to build and change. Its most important decisions are controlled outside the code that proposes them.

Morpholog is built to be the core of large business systems. The business model runs as code, and every application, person and agent that works on it must obey it.

A small example: a double-entry ledger

A programme declares the kinds of record, the rules they must keep, and the actions that change them. Here is part of a ledger.

predicate JournalEntry(entry_id: Subject, posting_date: Subject, period: Subject)
    unique by (entry_id)
predicate JournalLine(entry_id: Subject, line_no: Decimal, account: Subject, debit_amount: Decimal, credit_amount: Decimal)
predicate PeriodClosed(period: Subject)

invariant balanced_posted_entry:
    let debits = (sum(d | JournalLine(entry_id: entry, debit_amount: d, ..)))
    let credits = (sum(c | JournalLine(entry_id: entry, credit_amount: c, ..)))
    JournalEntry(entry, _, _) implies debits = credits

transformation post_simple_entry(
    entry_id, posting_date, period,
    debit_account, credit_account, amount
):
    require entry_id_unused: not JournalEntry(entry_id, _, _)
    require period_is_open: not PeriodClosed(period)
    admit JournalEntry(entry_id, posting_date, period)
    admit JournalLine(entry_id, 1, debit_account, amount, 0)
    admit JournalLine(entry_id, 2, credit_account, 0, amount)
    emit JournalEntryPosted(entry_id)
  • Each predicate is a kind of record: an entry, a line, a closed period.
  • The invariant is a rule every change is checked against: each entry's debits equal its credits.
  • The transformation is an action, and the only way records change. It checks the entry is new and its period open, then admits the entry and its lines.

An entry that does not balance is refused, and the database stays as it was. The refusal names the rule and the figures that disagreed:

{
  "status": "rejected",
  "rule": "balanced_posted_entry",
  "witness": [{ "var": "entry", "value": { "type": "subject", "value": "entry_002" } }],
  "compared": { "op": "=", "left": { "type": "decimal", "value": "100" }, "right": { "type": "decimal", "value": "90" } }
}

The whole ledger also closes periods, restates entries, and rebuilds a trial balance at any past moment.

How it differs from a rules engine, a policy engine and a database CHECK constraint

Your application proposes actions. Morpholog owns the decisions the application must be able to defend.

A database CHECK constraint
It sees one row. A Morpholog rule can span many records, and its refusal says which case broke it.
A rules engine or a policy engine such as OPA or Cedar
It answers a question about the inputs it is given. Morpholog checks the record itself, as part of the change, and keeps the history that results.
A workflow engine
It orders steps. Morpholog decides whether each step that changes the record is allowed.

A commodity trade, governed

In the trade lifecycle example, a trade is captured, confirmed, corrected and settled as governed actions. A trade does not have a status. It has a history. Morpholog refuses:

  • a confirmation by a desk with no authority for the trade's commodity;
  • a settlement before the trade is confirmed, or with no terms in force by its date;
  • a settlement of 110 lots while the terms in force say 100;
  • a second settlement under an id already used;
  • a capture or amendment that takes the desk's net position past its limit.

The trading system keeps its screens, its pricing and its mark-to-market. It proposes each step through Morpholog, and learns of each accepted step from its notifications.

What it does

The check and the change in one transaction. A change is saved only if every rule still holds, in the same database transaction. A refused change leaves the record as it was.

Refusals that say why. A refusal names the rule and the values that failed it, so the proposer can fix the proposal.

An audit record someone else can check. It holds each change, who proposed it and the rules it passed. The history exports as a tamper-evident file that an outsider can verify offline.

The record at any past moment. You can rebuild it exactly as it stood, with the rules that accepted each change.

A rule change rehearsed first. rehearse re-decides past proposals under a candidate rule and lists each one decided differently.

Code generated from the rules. Morpholog generates a typed Python client and SQL views, so nobody defines the model again by hand.

Fast. A saved change takes about 9 ms. Where rules compile to SQL, a change checks only the records it touches. It stays under 10 ms whether there are 1,000 records or 100,000.

Where it fits

Beside the systems you have. Its tables can sit in your existing PostgreSQL database, in their own schema. Only Morpholog's own database role writes them, so every change goes through the rules.

Your application keeps its language. It stays in Python, Rust or whatever it is written in, and proposes actions.

Reached the way your team works. Use the command line, the generated Python client, the Rust libraries or the SQL views.

See it work

Each example below runs end to end against PostgreSQL. Nothing is mocked.

Stock allocation. A shipment never allocates more stock than exists.

Trade lifecycle. Desk authority, a position limit, and settlement against the official price in force.

Margin call run. A day of margin calls is refused if it misses an account or gets an amount wrong.

Double-entry ledger. Every entry balances, a closed period takes no new postings, and the trial balance rebuilds at any past date.

Glasshouse is an open ETRM core for European power, built on Morpholog.

Get started

Download the binary for Linux or macOS from the releases page. morpholog check validates a programme without a database. The developer introduction builds a governed ledger end to end on PostgreSQL.

The code and documentation are on GitHub. Running it in production, or about to? Talk to us about support.

Contact us

Building or changing business software with AI, and its rules must hold? Talk to us.

Contact us

Or write to contact@a115.co.uk