Last updated: 2026-11-15
Rule Stores, using Blocks and the Planner in PatLang 2.0
For new readers
PatLang has a logic engine built in: a program states facts and rules, asks questions that are answered by backward chaining, and asks a planner for a sequence of actions that reaches a goal. In v1 every rule went into one shared table. In v2 rules live in stores, which are values a program can make, name, combine and hand around, so two parts of a program can reason over separate knowledge without colliding. This page covers stores, the syntax that names them, using blocks, and the planner, which now always finds a cheapest plan when one exists.backward chaining traces goals to facts
The rules are in section 1.13 of the values-and-memory specification. The engine's older features, rule_add and solve, actions and goals, contracts recorded as facts, carry over; Goal-oriented programming and Declarative rule syntax introduce them.
Stores are values FoundationalKnowledge that endures for decades โ core principles
logic_store_new() gives a new, unnamed store, of class rule_store. It is an object with identity, so every holder sees what any of them adds, and it goes when its last holder does. logic_store_named(name) gives the store with that name, made the first time the name is used; it lives until store_delete(name), after which the name names a new, empty store.
Two stores always exist. The program's own rules are in "default", and the runtime records a contract_holds fact in "contracts" each time a contract clause holds. A store holds each ground fact, a rule with no body and no variables, once: adding a fact it already holds changes nothing.contracts store facts in a separate store
| Form | Store it uses |
|---|---|
rule_add(pred, args, body), fact(pred, a, b), rule head :- body. | Adds to "default" |
rule_add_to(store, pred, args, body), fact_in(store, pred, a, b) | Adds to the store given |
rule head in NAME :- body. | Adds to the store named NAME |
solve(pred, args), query | Reads "default" and "contracts" |
solve_in(stores, pred, args), query_in | Reads one store, or a list of stores in order |
solve_into(target, stores, pred, args) | Reads stores, and adds each solution to target as a fact |
rules_clear(), rules_clear_in(store) | Empties "default", or the store given; "contracts" is left alone |
When a goal is solved against a list of stores, it sees the rules of every store listed, store by store in the order listed and, within a store, in the order they were added. A rule's body goals are solved against the same stores, except a goal written pred(args) from NAME, which is solved against the store named NAME alone.
rule parent(alice, bob) in family.
rule parent(bob, carol) in family.
rule grandparent(X, Z) :- parent(X, Y) from family, parent(Y, Z) from family.
print(solve("grandparent", ["alice", "Z"]))
print(solve("parent", ["X", "Y"]))
print(solve_in(logic_store_named("family"), "parent", ["X", "Y"]))
let census = logic_store_new()
rule_add_to(census, "lives", ["carol", "york"], [])
rule_add_to(census, "lives", ["carol", "york"], [])
print(list_len(solve_in(census, "lives", ["P", "T"])))
print(type_of(census))
[[alice, carol]]
[]
[[alice, bob], [bob, carol]]
1
rule_store
The grandparent rule lives in the default store and draws its parent facts from family, which solve does not read directly, so the second query finds nothing. The census fact was added twice and is held once. Named stores are looked up by name when solving, so a rule never holds a store and no cycle of references can form between them.
using blocks Applied / MethodologicalKnowledge with a 5โ10 year half-life โ stable practice
Passing a store to every call is noisy when a whole passage of code works with one store. using kb as logic do ... end gives the plain logic forms inside the block the store kb in place of the default and contracts stores: rule_add, fact, solve, query, rules_clear, plan, plan_incremental, plan_with_state, and a rule declaration with no in.
let kb = logic_store_new()
using kb as logic do
fact("likes", "sam", "tea")
rule likes(jo, X) :- likes(sam, X).
print(solve("likes", ["jo", "X"]))
end
print(solve("likes", ["jo", "X"]))
print(solve_in(kb, "likes", ["W", "X"]))
[[jo, tea]]
[]
[[sam, tea], [jo, tea]]
The block is a rewrite of the text inside it, done when compiling, and that decides its reach:
- A closure written inside the block keeps the store after the block ends.
- A named function called from inside the block is untouched, and uses the default store, since the block's store is not in its text.
- A form that names its store, such as
solve_inorrule ... in NAME, keeps the store it names. - An inner block giving the same role uses its own store until it ends.
- The store expression is evaluated once, when the block starts.
logic is the one role so far. Event handlers, signals and the planner's actions are not yet store values, and any other role, or one role given twice, is refused when compiling:the list of roles is still short
error: line 2: unknown store role 'events' in a using block (the roles are: logic)
The planner FoundationalKnowledge that endures for decades โ core principles
action_add(name, preconditions, add_effects, delete_effects, cost) declares an action, and plan(goals) gives a cheapest sequence of action names that leads from the stores' ground facts to a state meeting every goal. plan_in(stores, goals) plans from the ground facts of the stores given; plan, pursue and plan_incremental plan from the default and contracts stores. action_clear() forgets every action.
action_add("walk", [["at", ["home"]]], [["at", ["park"]]], [["at", ["home"]]], 1)
action_add("bus", [["at", ["park"]]], [["at", ["work"]]], [["at", ["park"]]], 2)
let here = logic_store_new()
rule_add_to(here, "at", ["home"], [])
print(plan_in(here, [["at", ["work"]]]))
print(plan([["at", ["work"]]]))
[walk, bus]
[]
The second call plans from the default store, where nothing says where we are, and gives []. Facts are always nested, a predicate and a list of arguments, ["at", ["home"]]; v1 accepted a flat form in some places, and six demos that used it were rewritten.
The planner is an A* search1 with an estimate that never overestimates the remaining cost, and a hashed set of the states already expanded. An empty result therefore means that no sequence reaches the goals: the search has no limit that gives up early. The earlier planner did have one, and it answered "no plan" for problems that had a plan. That came to light when the project's test gates began ordering their own builds with it. For n goals that independent steps meet, planning time grows about as n3: 40 goals in about 0.2 seconds and 160 in about 9, compiled.admissible heuristic: guarantees optimal path
What a call costs Applied / MethodologicalKnowledge with a 5โ10 year half-life โ stable practice
Adding a rule takes about the same time however many rules there are. A goal's cost does not depend on the rules of other predicates, and finding its solutions takes time in proportion to their number. Each store keeps its rules in a vec, with a second index holding each predicate's rules in a vec of its own, so a goal visits only its own predicate's rules. A rule whose head holds a constant the goal contradicts is skipped before its variables are renamed, and a ground fact is never renamed at all.vec: contiguous array, O(1) append, O(n) search
Measured on the probe that found the problem: 8,000 rules added in 69 milliseconds, where it had taken 8,784, and a bound goal over 6,400 facts answered in 9.9 milliseconds, where it had taken 96. The package resolver, which states version constraints as rules, was waiting on this change, being slow before it.
See also
Goal-oriented programming and Declarative rule syntax introduce the logic engine. Packages, resolution and trust uses facts, rules and the planner to resolve and install packages. The v2 toolchain describes the test gates, whose staleness rule is a logic query and whose build order is a plan. Worlds give each world its own logic stores.
Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A formal basis for the heuristic determination of minimum cost paths. IEEE Transactions on Systems Science and Cybernetics, 4(2), 100โ107. https://doi.org/10.1109/TSSC.1968.300136 โฉ
Graduate Attributes
Modelled on this page
- Digitally Fluent: Digital information is handled responsibly by documenting the technical rationale for rejecting flat data forms in favour of nested structures.
- Researcher: Knowledge is investigated responsibly by explaining how empirical measurements of execution times validated the performance improvements.
- Critical Thinker: Reliable evidence is sought out by detailing the specific algorithmic constraints and search limits that determine planning outcomes.
University of Reading Graduate Attributes framework.