Lemma rules engine for Elixir.
Wraps the Lemma engine (Rust) via NIFs. Create an engine, load specs from string or paths, run evaluations, and introspect schemas.
Example
{:ok, engine} = Lemma.new()
:ok = Lemma.load(engine, "spec foo\nfact x: 1\nrule y: x + 1", "my_spec.lemma")
{:ok, response} = Lemma.run(engine, "foo", [])
# response is a map from decoded JSONEngine lifecycle
Each engine is an opaque resource. Do not share the same engine ref across processes unless you serialize access (e.g. via a GenServer).
Summary
Functions
Returns the serialized execution plan for a spec as a map.
Formats Lemma source code. Does not require an engine instance.
Generates an OpenAPI 3.1 JSON document for the loaded specs (Lemma HTTP API shape).
Inverts a rule to find input domains that produce a desired outcome.
Lists all loaded specs. Each item is a map with :name, :effective_from,
:effective_to, and :schema.
Loads a spec from a string. Source label is used for error reporting (e.g. "my_spec.lemma"). Use "inline" when no path.
Loads specs from paths (files and/or directories). Directories are expanded one level; only .lemma files are loaded.
Creates a new engine. Optionally pass a map of resource limits; omitted keys use defaults.
Removes a spec from the engine by name and effective datetime.
Runs a spec. Options: :effective (datetime string or nil), :data (map).
Returns the schema for a spec.
Temporal version options for API docs (same boundaries as lemma serve / Scalar).
Types
@type engine() :: reference()
@type limits_map() :: %{required(String.t()) => pos_integer()} | nil
@type spec_name() :: String.t()
Functions
Returns the serialized execution plan for a spec as a map.
Options: :effective (datetime string or nil).
Formats Lemma source code. Does not require an engine instance.
Example
{:ok, formatted} = Lemma.format("spec foo\nfact x: 1\nrule y: x + 1")
Generates an OpenAPI 3.1 JSON document for the loaded specs (Lemma HTTP API shape).
Options: :explanations (boolean, default false), :effective (datetime string or nil for "now").
@spec invert(engine(), spec_name(), String.t() | nil, String.t(), map(), map()) :: {:ok, map()} | {:error, term()}
Inverts a rule to find input domains that produce a desired outcome.
effective is a datetime string or nil.
Target is a map with :outcome ("value" | "veto" | "any_value" | "any_veto"), |
optionally :op ("eq" | "neq" | "lt" | etc.), and for "value"/"veto": :value or :message. |
Lists all loaded specs. Each item is a map with :name, :effective_from,
:effective_to, and :schema.
Temporal versions form a half-open [effective_from, effective_to) range:
:effective_fromisnilwhen the spec has no declared start date (the first version is unbounded at the start).:effective_toisnilwhen the spec has no later version (this row is the latest and stays valid forward indefinitely).- Otherwise
:effective_toequals the next version's:effective_from(exclusive end of this row's validity).
:schema is the decoded [Lemma.schema/3] envelope for this version so
callers never need a second round-trip.
Loads a spec from a string. Source label is used for error reporting (e.g. "my_spec.lemma"). Use "inline" when no path.
Loads specs from paths (files and/or directories). Directories are expanded one level; only .lemma files are loaded.
@spec new(limits_map()) :: {:ok, engine()} | {:error, term()}
Creates a new engine. Optionally pass a map of resource limits; omitted keys use defaults.
Options (limits map keys)
max_files- max .lemma files per load_from_pathsmax_loaded_bytes- max total bytes to loadmax_file_size_bytes- max single file sizemax_total_expression_count- max expression nodesmax_expression_depth- max nesting depthmax_expression_count- max expressions per filemax_data_value_bytes- max data value size
Examples
{:ok, engine} = Lemma.new()
{:ok, engine} = Lemma.new(%{max_files: 100})
Removes a spec from the engine by name and effective datetime.
Runs a spec. Options: :effective (datetime string or nil), :data (map).
Returns decoded JSON response.
Returns the schema for a spec.
Options: :effective (datetime string or nil).
Temporal version options for API docs (same boundaries as lemma serve / Scalar).
Returns a list of maps with string keys "title" and "slug". Slug "now" means the
latest interface at request time; other slugs match effective strings for generate_openapi/2.