Lemma Engine
A language that means business.
Lemma Engine is the Rust crate behind the Lemma language. It lets you parse, validate, and evaluate Lemma docs from your own applications while keeping the same natural, auditable semantics that the CLI exposes.
Status
Lemma is still early-stage and not yet recommended for production use. Expect breaking changes, evolving semantics, and incomplete tooling while the project matures.
Why Lemma?
- Readable by business stakeholders – rules look like the policies people already write
- Deterministic and auditable – every evaluation returns a full trace explaining the result
- Type-aware – dates, percentages, units, and automatic conversions are first-class
- Composable – documents extend and reference each other without boilerplate
- Multi-platform – use the engine from Rust, power the CLI/HTTP server, or ship via WebAssembly
Quick start
Add the crate:
[]
= "0.7.3"
Minimal example
use Engine;
let mut engine = new;
engine.add_lemma_code?;
let response = engine.evaluate?;
for result in response.results
Providing values at runtime
use Engine;
use HashMap;
let mut engine = new;
engine.add_lemma_code?;
let mut values = new;
values.insert;
values.insert;
let response = engine.evaluate?;
Inverse reasoning
Inversion allows you to find what input values produce a desired output. This is useful for questions like "What quantity gives me a 30% discount?" or "What salary produces a total compensation of €100,000?"
Basic example
use ;
use HashMap;
use Decimal;
let mut engine = new;
engine.add_lemma_code?;
// Find what quantity gives a 30% discount
use Decimal;
let response = engine.invert?;
// Response contains solutions showing: is_vip must be true
API variants
1. invert() - String-based values (user-friendly)
Accepts string values that are automatically parsed based on document types:
let mut values = new;
values.insert;
let response = engine.invert?;
2. invert_json() - JSON input (convenience)
Accepts JSON bytes directly:
let json = br#"{"is_vip": true}"#;
let response = engine.invert_json?;
Target specification
Use Target to specify the desired outcome:
use ;
// Exact value (equality)
value
// Comparison operators
with_op // > 100
with_op // <= 50
// Find any veto
any_veto
// Find specific veto message
veto
Response structure
InversionResponse contains:
solutions: Concrete domain constraints for each free variableshape: Symbolic representation of the solution space (piecewise function)free_variables: Facts that are not fully determinedis_fully_constrained: Whether all facts have concrete values
let response = engine.invert?;
if response.is_fully_constrained else
for in &response.solutions
Features
- Rich type system – percentages, mass, length, duration, temperature, pressure, power, energy, frequency, and data sizes
- Automatic unit conversions – convert between units inside expressions without extra code
- Document composition – extend documents, override facts, and reuse rules across modules
- Audit trail – every evaluation returns the operations that led to each result
- Inverse reasoning – find what inputs produce desired outputs
- WebAssembly build –
npm install @benrogmans/lemma-engineto run Lemma in browsers and at the edge
Installation options
As a library
CLI tool
HTTP server
WebAssembly
Documentation
- Language guide: https://benrogmans.github.io/lemma/
- API documentation: https://docs.rs/lemma-engine
- Examples: https://github.com/benrogmans/lemma/tree/main/documentation/examples
- CLI usage: https://github.com/benrogmans/lemma/blob/main/documentation/CLI.md
- Roadmap: https://github.com/benrogmans/lemma/blob/main/documentation/roadmap.md
Use cases
- Compensation plans and employment contracts
- Pricing, shipping, and discount policies
- Tax and finance calculations
- Insurance eligibility and premium rules
- Compliance and validation logic
- SLA and service-level calculations
Contributing
Contributions are very welcome! See documentation/contributing.md and the project roadmap for ideas.
License
Apache 2.0