Skip to content

Repository files navigation

Logic Batteries

CI crates.io

Pre-built Prolog rule modules for DataGrout logic cells. The npm of living software.

Insert a battery into a logic cell and it has new capabilities immediately — no schema setup, no migration, no boilerplate. Assert facts into the same namespace and the battery's predicates reason over them.

Batteries Included

Reasoning

Module Predicates Description
fsm 25 General-purpose finite state machine — reachability, shortest paths, cycle detection
temporal 12 Event ordering, overlap, gaps, and deadline reasoning over timestamped facts
taxonomy 10 Hierarchical classification with transitive membership and property inheritance
explain 6 Provenance meta-interpreter — why/2 returns the facts supporting any conclusion; explain/2 returns full proof trees
fixpoint 4 Bottom-up Datalog saturation — tabling's termination benefit without tabling; cyclic/left-recursive rules work verbatim

Probabilistic

Query exact probabilities instead of thresholds — zero LLM calls per tick. Weighted rules use ProbLog P::Head notation; on ISO cells they are reified through prob-core-iso, so probabilistic reasoning runs on SWI and Scryer alike.

Module Predicates Description
prob-core-iso 6 ProbLog-lite runtime in pure ISO — noisy-or psuccess/2, pmax/2, negation, conjunction, expected value over reified weighted rules
prob-decide 4 Decision theory over prob-core-iso — expected utility eu/2 and best_action/2 argmax across weighted outcomes
prob-loot 4 Drop probabilities and expected yields — layered on loot-tables
prob-detection 4 Guard perception and stealth probability from environment and alert state — requires combat
prob-economy 5 Market uncertainty: supply disruption and demand spike probabilities — requires economy
prob-npc 5 NPC trust and disposition probability from faction standing — requires npc-state and faction

Games

Module Predicates Description
inventory 7 Item carrying, weight, slot constraints
loot-tables 6 Drop weights, rarity, condition-gated loot
quests 6 Prerequisite chains, objectives, turn-in
progression 5 XP curves, level gates, stat scaling, prestige
combat 6 Damage types, resistances, status effects, turn order
economy 5 Crafting costs, supply/demand pricing
npc-state 5 Affinity tracking, dialogue availability
puzzle-fsm 5 FSM transitions, win conditions, hints
world 6 Time of day, weather, season, moon phase
faction 5 Reputation scores, standing tiers, area access
dialogue 5 Context-aware lines, gated choices, memory
crafting 5 Recipe knowledge, skill requirements, discovery
permissions 5 Role-based access, inheritance, ownership
ai-director 5 Pacing states, spawn eligibility, difficulty scaling
dungeon 5 Room connectivity, key locks, clearance tracking

Business

Module Predicates Description
lead-scoring 5 Weighted scoring, tier derivation, disqualification
invoice-rules 5 Overdue detection, late fees, escalation levels
approval-chains 5 Multi-step approvals, delegation, rejection tracking
inventory-mgmt 5 Stock levels, reorder triggers, supplier selection
pricing-rules 5 Tier pricing, discounts, bulk breaks, floor/ceiling
loyalty 5 Points balance, tier benefits, redemption costs
scheduling 5 Slot availability, conflicts, advance windows
compliance 5 Policy checks, retention windows, consent registry

d20

The SRD 5.1 rules layer as composing batteries. All stat blocks and mechanics from the Systems Reference Document 5.1, published under CC BY 4.0 by Wizards of the Coast LLC. Dice stay client-side — the cell adjudicates, the caller rolls. Pure ISO: runs on SWI and Scryer alike.

Module Predicates Description
d20-core 10 Ability modifiers (floored), proficiency, skills with expertise and stat-block overrides, saves, checks vs DC, opposed contests
d20-conditions 8 All 15 SRD conditions plus cumulative exhaustion as queryable effect data
d20-combat 14 Natural 1/20 hit resolution, damage with resistances (RAW round-down), crits, initiative, advantage/disadvantage with RAW cancellation, death saves, instant death
d20-monsters 5 16 stat blocks CR 0–13 with full SRD attack profiles (d20_monster_attack/5)
d20-xp 6 XP by CR 0–30, encounter difficulty thresholds, action-economy multipliers

batteries.* MCP Toolsuite

The batteries.* toolsuite lets agents search, install, and manage batteries directly from an LC session — no manual Prolog URL handling needed.

Tool Description
batteries.search(query?, category?, tags?) Search by keyword, category, or tags — omit query to browse all
batteries.install_many(ids[], namespace) Install one or more batteries into a namespace
batteries.installed(namespace) List all batteries currently installed in a namespace
batteries.remove(id, namespace) Retract a battery's rules from a namespace

Installation state is tracked directly in the LC: attribute('_batteries', battery_id, version). This makes batteries.installed a plain lc.query call and keeps state co-located with the rules.

Install

battery CLI (any Prolog project — no DataGrout required)

Batteries are plain ISO/SWI Prolog files, so they also work in bare swipl / scryer-prolog projects. The battery CLI copies a battery's rule files into your project directory and content-hashes them into batteries.lock.json:

$ battery install prob-core-iso prob-decide --dir my-app/
✓ installed prob-core-iso 1.0.0 (2 files)
✓ installed prob-decide 1.0.0 (1 file)
$ battery remove prob-decide --dir my-app/

remove only deletes files whose checksum still matches what was installed — a battery you've modified is kept (and warned about) unless you pass -f. battery installed lists what's in a directory and flags modified entries; battery list shows the registry. Install with cargo install logic-batteries, or build from cli/.

Tether (Luau / Roblox)

Tether is the Lua client for DataGrout. The dg object in module README examples is a Tether client instance.

dg:batteries().install("inventory", "my-namespace", function(result)
  print(result.installed_count .. " batteries installed")
end)

Any DG client

{
  "tool": "batteries.install_many",
  "ids": ["inventory"],
  "namespace": "my-namespace"
}

What Is This

A DataGrout logic cell is a persistent, queryable Prolog fact store — the reasoning substrate for your application or agent. You assert facts into it (logic.assert) and query them (logic.query). It's the brain.

Logic Batteries provides pre-built reasoning engines that run inside that brain. Each battery is a self-contained Prolog library that operates over facts you assert into the same namespace. Load the FSM battery and your LC becomes a finite state machine engine. Load the game batteries and it becomes your game's rules engine.

Batteries are designed to compose. A game using quests + inventory + loot-tables + npc-state has four batteries sharing a single fact namespace — quest completion triggers dialogue unlocks, loot items land in inventory, NPC disposition gates quest availability. The composition happens through Prolog unification over shared facts. No event bus. No integration code. Just facts and rules.

These aren't examples or recipes. They're programs you load into a living reasoning substrate.

Contributing Batteries

See CONTRIBUTING.md for the full authoring guide and contribution terms (DCO sign-off + contribution license grant).

The short version: every battery declares a manifest in its rule file — battery_module(Id, Version, Mode) plus one battery_export(Id, 'pred/arity', 'doc string') per public predicate (read by batteries.describe and the CLI as authoritative documentation) — and declares its input predicates :- dynamic(...) so standalone (consult) users can assert facts after loading. Batteries must stay within the LC runtime's predicate whitelist: no process execution, file I/O, networking, global mutable state, or runtime fact modification. make lint enforces this on every push.

License

Three deliberate tiers — permissive tooling and runtime, protected content:

What License Why
Content batteries (default) Elastic License 2.0 Free to use — including vendored into your projects via the CLI — but can't seed a competing managed Logic Cell service
prob-core-iso Apache-2.0 Core runtime, not content — embed it anywhere, no restrictions
battery CLI MIT Commodity tooling — the batteries it installs carry their own license
d20 category Elastic 2.0, incorporating SRD 5.1 content under CC BY 4.0 Stat blocks, tables, and mechanics from the Systems Reference Document 5.1, © Wizards of the Coast LLC — attribution travels with every module and the registry

Registry entries may carry an explicit license field; when absent, the repository default (Elastic License 2.0) applies.

Releases

Contributors

Languages