Expand description
§Lemma Engine
Rules for man and machine
Lemma is a declarative programming language for expressing rules, data, and business logic in a way that is both human-readable and machine-executable.
§Quick Start
use lemma::{Engine, SourceType};
let mut engine = Engine::new();
// Load Lemma code
engine.load(r#"
spec example
data price: 100
data quantity: 5
rule total: price * quantity
"#, SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("example.lemma")))).expect("failed to load");
// Evaluate the spec (all rules, no data values)
let now = lemma::DateTimeValue::now();
let response = engine.run(None, "example", Some(&now), std::collections::HashMap::new(), false, None).unwrap();§Core Concepts
§Specs
A spec is a collection of data and rules. Specs can reference other Specs to build composable logic.
§Data
Data are named values: numbers, text, dates, booleans, or typed units
like 50 kilograms or 100.
§Rules
Rules compute values based on data and other rules. They support conditional logic through “unless” clauses.
§Types
Lemma has a rich type system including units (mass, length, time, money) with automatic conversions.
Modules§
- deps
- Dependency layout under
<workdir>/lemma_deps/, shared by CLI fetch and LSP.
Structs§
- Cause
- Context
- Ordered store of specs keyed by
(repository, name)and grouped intoLemmaSpecSets. - Conversion
Trace Step - Data
Entry - A spec’s public interface: its data (inputs) and rules (outputs) with full structured type information.
- Data
Group - Grouped data from a specific spec (semantics types only).
- Data
Overlay - User-provided data values resolved against a plan’s type declarations.
- Data
Path - Resolved path to a data (created during planning from AST DataReference)
- Date
Time Value - Engine
- Engine for evaluating Lemma rules.
- Error
Details - Detailed error information with optional source location.
- Errors
- Load failure: errors plus the source texts we attempted to load.
- Execution
Plan - A complete execution plan ready for the evaluator
- Execution
Plan Serialized - The serializable form of an
ExecutionPlan. - Explanation
- Instructions
- Compiled normalized equation for authoritative evaluation.
- Lemma
Base - The LemmaBase registry fetches Lemma source text from LemmaBase.
- Lemma
Repository - A Lemma repository header. Identity carrier; never owns specs.
- Lemma
Spec - A Lemma spec containing data and rules.
- Lemma
Spec Set - All spec versions sharing a (repository, name) identity, keyed by effective_from.
- Lemma
Type - Resolved type after planning
- Lexer
- Literal
Value - Literal value with type. The single value type in semantics.
- Parse
Result - Planning
Source - Positional source location: source id and span.
- Quantity
Unit - A single unit within a Quantity type.
- Quantity
Units - Ratio
Unit - Ratio
Units - Registry
Bundle - A bundle of Lemma source text returned by the Registry.
- Registry
Error - An error returned by a Registry implementation.
- Resolved
Repository - A loaded repository with all its spec sets.
- Resource
Limits - Limits to prevent abuse and enable predictable resource usage
- Response
- Response from evaluating a Lemma spec
- Rule
Path - Resolved path to a rule (created during planning from RuleReference)
- Rule
Result - Result of evaluating a single rule. Struct fields match the API JSON shape.
- Source
- Positional source location: source id and span.
- Span
- Span representing a location in source code
- SpecRef
- A spec reference written in source.
- Spec
Schema - Timezone
Value
Enums§
- Binding
Data Value - Response/UI row for spec data:
LemmaTypeplus optional bound literal (mirrors parse-timeDefinition). - Computation
Kind - The kind of computation performed
- Conversion
Trace Role - Data
Definition - Resolved data value for the execution plan: aligned with
DataValuebut with source per variant. - Data
Value - Parse-time data value (before type resolution)
- Data
Value Input - Typed data value from a client (CLI/WASM). JSON parsing stays outside [
parse_data_value]. - Date
Granularity - Effective
Date - Error
- Error types for the Lemma system with source location tracking
- Error
Kind - Classification of an
Error. Serialized as thekindfield on the flat object returned to JavaScript from WASM (engine/src/wasm.rs,JsError). - Explanation
Node - Instruction
- One compiled operation in a rule’s instruction stream.
- Operation
Result - Result of an operation (evaluating a rule or expression)
- Registry
Error Kind - The kind of failure that occurred during a Registry operation.
- Request
Error Kind - Distinguishes HTTP 404 (not found) from 400 (bad request) for request errors.
- Source
Type - Token
Kind - Type
Specification - Value
Kind - Value payload (shape of a literal). No type attached. Quantity unit is required; Ratio unit is optional (see plan ratio-units-optional.md).
- Veto
Type - Why an operation yielded no value (domain veto).
Constants§
- EMBEDDED_
STDLIB_ REPOSITORY - Repository name reserved for the embedded standard library (
repo lemma,spec units). UserEngine::load/Engine::load_batchmust not target this name. - INSTRUCTIONS_
VERSION - MAX_
DATA_ NAME_ LENGTH - MAX_
RULE_ NAME_ LENGTH - MAX_
SPEC_ NAME_ LENGTH - UNITS_
LEMMA
Traits§
- Registry
- Trait for resolving external repository references.
Functions§
- collect_
lemma_ sources - Collect
.lemmasource texts from filesystem paths (paths and one-level directories). Does not touch anEngine; pair withEngine::load/Engine::load_batch. - format_
explanation - format_
expression - format_
parse_ result - Format a
ParseResult(repository groups + specs) into canonical Lemma source. - format_
source - Parse a source string and format it to canonical Lemma source.
- format_
specs - Format a sequence of parsed specs into canonical Lemma source.
- parse
- parse_
spec_ set_ id - Validate and normalize a spec set identifier (a workspace spec name).
- plan
- Build execution plans for one or more Lemma specs.
- resolve_
registry_ references - Resolve every
usesreference that carries a registry repository qualifier in the loaded specs. - type_
detail_ lines - Produce a human-readable summary of type constraints, or
Nonewhen there are no constraints worth showing (e.g. bareboolean). Returns one formatted string per constraint or property of the type specification. Usesrational_to_display_strfor all rational bounds so they render as decimals, not as raw fractions. - validate_
instruction_ jumps - Planning-time invariant: every jump is patched and lands on a real instruction. Panics on violation — compiled output failing this check is a compiler bug.
- validate_
instructions - Validate a compiled instruction stream against its operand pools: version,
jump targets, register indices, constant/data/veto-message table indices,
and the trailing
Instruction::Return.