Lemma (lemma_engine v0.9.0)

Copy Markdown View Source

Lemma rules engine for Elixir.

Wraps the Lemma engine (Rust) via NIFs. Create an engine, load sources, run evaluations, and show specs.

Example

{:ok, engine} = Lemma.new()
:ok = Lemma.load(engine, "spec foo\ndata x: 1\nrule y: x + 1")
{:ok, response} = Lemma.run(engine, %{spec: "foo"}, %{data: %{}})
# response is a map from decoded JSON

Engine 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

Formats Lemma source code. Does not require an engine instance.

Lists loaded specs grouped by repository (metadata only).

Loads Lemma source(s).

Creates a new engine. Optionally pass a map of resource limits; omitted keys use defaults.

Removes a temporal spec slice.

Returns spec interface and temporal window at effective.

Returns formatted canonical Lemma source.

Types

engine()

@type engine() :: reference()

limits_map()

@type limits_map() :: %{required(String.t()) => pos_integer()} | nil

repository()

@type repository() :: String.t() | nil

spec_name()

@type spec_name() :: String.t()

Functions

format(code)

@spec format(String.t()) :: {:ok, String.t()} | {:error, term()}

Formats Lemma source code. Does not require an engine instance.

list(engine)

@spec list(engine()) :: {:ok, [map()]} | {:error, term()}

Lists loaded specs grouped by repository (metadata only).

load(engine, code)

@spec load(engine(), String.t()) :: :ok | {:error, [map()]}
@spec load(engine(), map() | [{String.t(), String.t()}]) :: :ok | {:error, [map()]}

Loads Lemma source(s).

  • binary → one volatile workspace source
  • map or [{label, code}, ...] → labeled sources in one planning pass

new(limits \\ nil)

@spec new(limits_map()) :: {:ok, engine()} | {:error, term()}

Creates a new engine. Optionally pass a map of resource limits; omitted keys use defaults.

remove(engine, repository, spec, effective)

@spec remove(engine(), repository(), spec_name(), String.t() | nil) ::
  :ok | {:error, term()}

Removes a temporal spec slice.

Positional args: repository, spec, effective.

run(engine, target, options \\ %{})

@spec run(engine(), map(), map()) :: {:ok, map()} | {:error, term()}

Runs a spec.

target is %{repo: repository | nil, spec: name, effective: datetime | nil}. options is %{data: map, rules: [String.t()] | nil, explain: boolean} (defaults apply when omitted).

Each rule result may include missing_data (unbound input keys as strings). Types, prefilled literals, and suggestions are on show/4 only — not on the evaluate response.

show(engine, repository, spec, effective \\ nil)

@spec show(engine(), repository(), spec_name(), String.t() | nil) ::
  {:ok, map()} | {:error, term()}

Returns spec interface and temporal window at effective.

Positional args: repository, spec, effective.

source(engine, repository, spec \\ nil, effective \\ nil)

@spec source(engine(), repository(), spec_name() | nil, String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Returns formatted canonical Lemma source.

Omit spec for whole-repository text. When spec is set, effective selects the slice.