Skip to content

defines

defines

Declare reusable pure functions shared across all compute steps in a machine. Functions defined here are hoisted: they can call each other (including mutual recursion), and they are available in every compute step body without imports or qualification. This eliminates code duplication when multiple compute steps need the same transformation logic.

defines is Level B of the 4-level function system: A (step-local arrow functions), B (machine-level defines), C (importable from other machines via uses), D (stdlib function krates).

When to use

Use defines when you have:

  • Transformation logic reused across multiple compute steps in the same machine
  • Complex expressions that would make inline compute bodies hard to read
  • Functions that you want to test and reason about independently
  • Shared formatting, classification, or validation helpers

If the function is only needed in a single compute step, use a step-local arrow function (let f = (x) => ...) instead. If the function should be shared across multiple machines, extract it to a separate machine and import it with uses (Level C, planned).

Syntax

defines
<name>(<params>) => <expression>
<name>(<params>) => <multi-line expression>

Each function definition uses arrow syntax. The function name is a bare identifier, parameters are comma-separated, and the body is a single expression (which may span multiple lines using let bindings and ternary operators).

Rules

  • All functions must be pure: no I/O, no state mutation, no side effects. The same constraints as compute expressions apply.
  • Functions are hoisted. Order of declaration does not matter; any function can call any other.
  • Functions are injected into every compute step’s scope as anonymous function bindings at compile time.
  • Functions can use pattern matching via match:
defines
process(input) => match typeof(input)
"string" => input.trim().toLowerCase()
"list" => input.map(item => process(item))
_ => input

Examples

Simple helper functions

machine ticket_router
accepts
message as text, is required
responds with
team as text
confidence as decimal
defines
classify(text) =>
text.toLowerCase().includes("billing") ? "billing" :
text.includes("tech") ? "technical" : "general"
format_result(category, score) =>
{category: category, confidence: score}
implements
compute route
{team: classify(input.message), confidence: 0.85}
compute formatted
format_result(steps.route.team, steps.route.confidence)

Transducer pattern (reusable transform chains)

machine data_cleaner
accepts
records as list, is required
responds with
cleaned as list
invalid_count as number
defines
normalize(text) =>
text.trim().toLowerCase()
validate(record) =>
record.name && record.name.length > 0 && record.email && record.email.includes("@")
clean_record(record) =>
{
name: normalize(record.name),
email: normalize(record.email),
valid: validate(record)
}
implements
compute process
let results = input.records.map(r => clean_record(r))
{
cleaned: results.filter(r => r.valid),
invalid_count: results.filter(r => !r.valid).length
}

Functions calling other functions

machine formatter
defines
currency(amount) =>
"$" + amount.toFixed(2)
percentage(value) =>
(value * 100).toFixed(1) + "%"
format_line(item) =>
item.name + ": " + currency(item.price) + " (tax: " + percentage(item.tax_rate) + ")"
implements
compute build_receipt
{lines: input.items.map(item => format_line(item))}

Canonical ordering

defines appears between the contract sections (accepts / responds with) and implements:

machine name
has ...
uses ...
achieves ...
accepts ...
responds with ...
defines <-- here
implements ...
expresses ...
ensures ...

Governance

defines functions are pure by construction, the same as compute step bodies. They cannot perform I/O, call external services, or access the network. No governance checks are required. Functions declared in defines are invisible to the governance interpreter; only the compute steps that call them produce behavioral ledger entries.

Translations

LanguageKeyword
Englishdefines
Spanishdefine
Frenchdefinit
Germandefiniert
Japanese定義
Chinese定义
Korean정의

See also

  • compute - Steps where defined functions are available
  • implements - The behavior section that uses these functions
  • uses - Import functions from other machines (Level C, planned)