Skip to main content

lemma/
lib.rs

1//! # Lemma Engine
2//!
3//! **Rules for man and machine**
4//!
5//! Lemma is a declarative programming language for expressing rules, facts, and business logic
6//! in a way that is both human-readable and machine-executable.
7//!
8//! ## Quick Start
9//!
10//! ```rust,no_run
11//! use lemma::{Engine, SourceType};
12//! use std::collections::HashMap;
13//!
14//! let mut engine = Engine::new();
15//!
16//! // Load Lemma code
17//! engine.load(r#"
18//!     spec example
19//!     fact price: 100
20//!     fact quantity: 5
21//!     rule total: price * quantity
22//! "#, SourceType::Labeled("example.lemma")).expect("failed to load");
23//!
24//! // Evaluate the spec (all rules, no fact values)
25//! let now = lemma::DateTimeValue::now();
26//! let response = engine.run("example", Some(&now), HashMap::new(), false).unwrap();
27//! ```
28//!
29//! ## Core Concepts
30//!
31//! ### Specs
32//! A spec is a collection of facts and rules. Specs can reference
33//! other specs to build composable logic.
34//!
35//! ### Facts
36//! Facts are named values: numbers, text, dates, booleans, or typed units
37//! like `50 kilograms` or `100`.
38//!
39//! ### Rules
40//! Rules compute values based on facts and other rules. They support
41//! conditional logic through "unless" clauses.
42//!
43//! ### Types
44//! Lemma has a rich type system including units (mass, length, time, money)
45//! with automatic conversions.
46
47#[cfg(test)]
48mod tests;
49
50pub(crate) mod computation;
51pub mod engine;
52pub mod error;
53pub mod evaluation;
54pub mod formatting;
55pub mod inversion;
56pub mod limits;
57pub(crate) mod literals;
58pub mod parsing;
59pub mod planning;
60pub mod registry;
61pub mod serialization;
62pub mod spec_id;
63
64#[cfg(target_arch = "wasm32")]
65pub mod wasm;
66
67pub use engine::{Context, Engine, Errors, SourceType};
68pub use error::{Error, RequestErrorKind};
69pub use evaluation::explanation;
70pub use evaluation::operations::{
71    ComputationKind, OperationKind, OperationRecord, OperationResult,
72};
73pub use evaluation::response::{Facts, Response, RuleResult};
74pub use formatting::{format_source, format_specs};
75pub use inversion::{Bound, Domain, InversionResponse, Solution, Target, TargetOp};
76pub use limits::ResourceLimits;
77pub use parsing::ast::{
78    DateTimeValue, DepthTracker, LemmaFact, LemmaRule, LemmaSpec, MetaField, MetaValue, Span,
79    TypeDef,
80};
81pub use parsing::parse;
82pub use parsing::ParseResult;
83pub use parsing::Source;
84pub use planning::semantics::{
85    is_same_spec, FactPath, LemmaType, LiteralValue, RatioUnit, RatioUnits, RulePath, ScaleUnit,
86    ScaleUnits, SemanticDurationUnit, TypeDefiningSpec, TypeSpecification, ValueKind,
87};
88pub use planning::{ExecutionPlan, PlanningResult, SpecPlanningResult, SpecSchema};
89#[cfg(feature = "registry")]
90pub use registry::LemmaBase;
91pub use registry::{
92    resolve_registry_references, Registry, RegistryBundle, RegistryError, RegistryErrorKind,
93};
94pub use spec_id::parse_spec_id;