stableKanren: Logic with Stable Model Semantics
- stableKanren is a miniKanren extension that integrates negation and contradiction reasoning for normal logic programs under stable model semantics.
- It refines contradiction handling via compile-time macros to ensure backward compatibility with definite miniKanren relations while enabling non-monotonic reasoning.
- It further incorporates integrity constraints and numeric constraint handling to optimize combinatorial search and heuristic numeric computations.
stableKanren is a miniKanren extension for normal logic programs under stable model semantics. It preserves the small relational core of miniKanren while adding mechanisms for negation, contradiction reasoning, and, in later extensions, integrity constraints and numeric constraint handling. In that form, it occupies a distinctive position within the Kanren family: ordinary relational programming, non-monotonic reasoning, and declarative generate-and-test search coexist in a single top-down system (Guo et al., 2024, Guo et al., 2024, Guo et al., 5 Oct 2025).
1. Semantic basis and relation to miniKanren
miniKanren, in its standard form, offers a small set of relational primitives: unification ==, existential variables via fresh, disjunction via conde, and a query form run. stableKanren keeps those primitives and extends them with noto for negation and defineo for defining relations in a way that supports stable-model-style reasoning (Guo et al., 2024). This extension is not merely syntactic. Its intended semantic domain is the class of normal logic programs, rather than only definite programs.
The semantic backdrop is the standard stable-model construction. Given an input program , one grounds it into a propositional image , considers interpretations , and for each candidate forms a reduct by replacing negative literals with if , otherwise . If is the unique minimal model of 0, then 1 is a stable model (Guo et al., 2024). This is the mechanism that lets stableKanren represent non-monotonic reasoning: a program may have no stable model, exactly one stable model, or multiple stable models, depending on how negation and cyclic dependencies interact.
The relevant program classes are distinguished explicitly. A definite program has clauses of the form 2; a normal program has clauses 3; a stratified program is one whose unfounded sets contain no negation; and an unavoidable contradiction is an atom that can neither be proved true nor false because an odd negative loop forces contradiction (Guo et al., 2024). These distinctions are central to stableKanren’s behavior, because contradiction handling is necessary only in the fragment where negation can create non-monotonic dependencies.
As background, a certified semantic account of miniKanren formulates denotational semantics corresponding to the minimal Herbrand model for definite logic programs and an operational semantics for interleaving search, with soundness and completeness proved in Coq (Rozplokhas et al., 2020). This places stableKanren in a clear lineage: it builds on the ordinary miniKanren relational core and extends that core with stable-model-style treatment of negation and contradiction.
2. Core language and contradiction reasoning
The central operational distinction in stableKanren is that contradictions arise from negation. Standard miniKanren relations do not involve negation, and thus belong to the definite fragment; by contrast, stableKanren relations may introduce noto and thereby encode odd loops through negation, unsatisfiable programs, or multiple stable models (Guo et al., 2024). The canonical example is a relation p that depends on noto (p): the contradiction is unavoidable, and the whole program becomes unsatisfiable even if unrelated facts remain individually well formed.
This makes stableKanren qualitatively different from ordinary miniKanren. In miniKanren, the main semantic burden lies in relational search over substitutions. In stableKanren, search is coupled to a global requirement: answers must be compatible with stable model semantics, which means the implementation must account for hidden contradictions created by negation. The original run interface is therefore conservative. After finding a partial answer, it continues checking for unavoidable contradictions in the environment, because it is attempting to certify that no contradiction elsewhere invalidates the candidate result (Guo et al., 2024).
The significance of this design is twofold. First, it explains why stableKanren can express genuinely non-monotonic patterns unavailable to ordinary miniKanren. Second, it explains the practical tension that later work addresses: global contradiction checking is semantically appropriate for normal programs with negation, but overprotective for definite programs and many stratified programs. The distinction between positive and negative fragments is therefore not only theoretical; it determines both usability and asymptotic behavior.
3. Backward compatibility and refined contradiction handling
A major implementation issue identified in stableKanren is backward compatibility with ordinary miniKanren programs. Standard miniKanren relations that produce infinite results did not run as expected in stableKanren, because contradiction handling was too coarse. The original system added every relation defined with defineo to program-rules, and run could therefore trigger unavoidable-contradiction checking across the whole registered environment, even when the queried relation was purely definite and could not create contradictions at all (Guo et al., 2024).
The repair is a fine-grained narrowing of the checking scope. A compile-time syntax analysis macro, has-negation?, inspects the body of a relation and looks specifically for noto, including through nested conde and fresh forms. The improved defineo uses this test before adding a relation to program-rules, so only relations that syntactically contain negation are enrolled in the global contradiction machinery (Guo et al., 2024). Definite relations are excluded entirely. This restores the expected behavior of ordinary miniKanren code inside stableKanren because definite programs do not create unavoidable contradictions.
The second part of the repair is the addition of run-partial. This interface uses the stableKanren execution machinery, including the extended lambdag@ with negation counter and call stack, but stops after producing the reified answer stream and does not force a global contradiction test (Guo et al., 2024). In effect, run becomes the conservative full stable-model query interface, whereas run-partial is the lightweight answer-producing interface.
This distinction matters in several cases. Standard miniKanren relations defined with define or defineo can now produce answers even if the same environment contains non-terminating or unsatisfiable stableKanren relations. Stratified negation programs also benefit, because stratified programs do not contain unavoidable contradictions in the first place. The reported final-SCC example quantifies the effect: computing 100 final-SCC pairs with run-partial takes about 0.013 seconds and only 2 collections, while the original run takes about 32.7 seconds and 5597 collections for the same result (Guo et al., 2024).
A common misconception is that full contradiction checking is always the semantically correct default for stableKanren queries. The repair shows that this is too strong. Definite programs and stratified programs are handled by minimal model semantics and have the optimal substructure property; partial results are not misleading because no odd negation loop can later invalidate them (Guo et al., 2024). The proposed future direction is a dependency-graph analysis macro, tentatively has-stratified?, so that the query interface can decide automatically whether unavoidable-contradiction checking is required.
4. Integrity constraints and combinatorial search
A subsequent extension adds integrity constraints to stableKanren in order to support a more declarative style of combinatorial search. The motivating observation is that stableKanren already supports contradiction reasoning through stable model semantics, but earlier combinatorial encodings did not fully exploit that capability because the system lacked control over the predicate outcome during resolution (Guo et al., 2024). Integrity constraints, as used in answer set programming, supply precisely that missing control.
Formally, an integrity constraint is a headless normal clause,
4
This form is problematic for stableKanren because defineo creates a goal function from a clause head, and an integrity constraint has no valid head (Guo et al., 2024). Two standard approaches are reviewed and rejected. The bottom-up approach grounds the program first and then propagates constraints; this creates a heavy memory footprint, generates many constraints that may never be needed, and does not match stableKanren’s top-down execution style. The top-down approach inspired by s(CASP) introduces a head such as false :- ..., but the transformed constraints are then solved rather than properly verified, and the paper reports that forbidden combinations can still be accepted (Guo et al., 2024).
The proposed solution is to treat integrity constraints as exceptions during resolution. A constraint is decomposed into emitters and verifiers. An emitter is a predicate that emits values; a verifier is a boolean expression that receives and verifies those values; and a constraint handler is a concatenation of verifiers and 5 (Guo et al., 2024). The macro 1 makes this split explicit.
Operationally, the extension inserts two checkpoints into the hidden execution machinery: constraint-updater and constraint-checker. Emitters succeed and emit values; constraint-updater wraps those values into the handler using constraint-constructor; once enough values have arrived, constraint-checker evaluates the handler; and if the handler is true, the branch is terminated as contradictory (Guo et al., 2024). Partial handlers are retained when only some emitters have fired, so constraint propagation is incremental rather than fully grounded.
This exception-like interpretation gives stableKanren a uniform generate-and-test methodology for combinatorial search. The paper applies it to nqueens, graph coloring, and Hamiltonian cycle. In that setting, the system is described as a producer, checker, and prover without changing the underlying algorithm: it can generate all solutions, verify a candidate solution, or fill in missing parts of a partial solution (Guo et al., 2024). A common confusion is to equate these constraints with ordinary variable-domain constraints; the extension instead constrains predicate truth outcomes during top-down resolution.
5. Numeric computation and heuristic search control
A later development uses integrity constraints to encode numeric computations for problem solving and to infuse heuristic knowledge into stableKanren queries. The paper distinguishes three ways to extend a relational programming language with numeric computations: relational number representation, grounding numbers to symbols, and constraint store construction. stableKanren adopts the third approach (Guo et al., 5 Oct 2025).
Relational number representation, exemplified by bit-list arithmetic in the style of The Reasoned Schemer, is fully relational but verbose, difficult to read, and does not take advantage of CPU arithmetic. Grounding numbers to symbols is straightforward for SAT or ASP-style solving, but sacrifices top-down resolution and incurs a large memory footprint. stableKanren’s approach is constraint-store construction: numeric expressions are kept as expressions during resolution and evaluated only when enough values have been unified. The paper argues that this gives a straightforward numerical representation, avoids grounding all numbers to symbols, and has simpler syntax than other constraint store construction approaches (Guo et al., 5 Oct 2025).
The mechanism is again based on integrity constraints. Arithmetic relations such as pluso, minuso, multipo, and divido are specified by generating tuples from a numeric domain and then pruning them with numerical verifiers. Division, for example, is constrained by the side conditions that the divisor is nonzero, the remainder is less than the divisor, and 6 (Guo et al., 5 Oct 2025). The resulting style is neither purely symbolic grounding nor purely relational bit-level arithmetic; it is a hybrid symbolic-numeric evaluation discipline mediated by emitters and verifiers.
The principal case study is the cryptarithm
7
In the baseline encoding, a query for one solution took 4,670 seconds on a 2020 iMac (Guo et al., 5 Oct 2025). The paper then shows that solver performance depends strongly on how programs and queries are written. Reordering query variables changes the running time from about 14242 seconds when starting from s to about 1080 seconds when starting from o. A best ordering learned after knowing the answer, OMYENDRS, finds one solution in about 3 seconds, although exhaustive run* still takes over 5000 seconds (Guo et al., 5 Oct 2025).
A second improvement replaces one large arithmetic verifier with five smaller digit-wise integrity constraints using mod and floor expressions. This enables earlier pruning because each constraint becomes checkable sooner. Under this decomposition, the s-first order drops to 2708 seconds, the o-first order drops to 51 seconds, the best query order takes only about 0.46 seconds for one solution, and exhaustive search with that order drops to 311.48 seconds (Guo et al., 5 Oct 2025).
The paper then introduces the notion of critical emitters: emitters whose outcomes determine later constraints, notably the low-order digits that generate carries. From these dependencies it derives the query order YDENROSM, reducing exhaustive run* search to 15.69 seconds. Finally, an external oracle verifier encodes the human-derived facts 8, 9, and 0; with that hybrid verifier, YDENROSM drops to 5.17 seconds, and OSMYDENRO achieves exhaustive search in 1.89 seconds (Guo et al., 5 Oct 2025).
The broader implication is not that stableKanren has an automatically optimized numeric solver. The paper states the opposite: the system is brute-force at heart, and performance depends heavily on constraint granularity, query order, and the amount of heuristic knowledge infused through programs or queries (Guo et al., 5 Oct 2025). Its contribution is a declarative framework in which symbolic search, stable-model negation, and numeric verification can be combined without abandoning top-down relational execution.
6. Position within the Kanren ecosystem
stableKanren belongs to the broader Kanren family but should be distinguished from adjacent systems whose goals differ materially. Its nearest baseline is miniKanren, whose certified semantics formalizes definite-program denotations and fair interleaving search (Rozplokhas et al., 2020). stableKanren extends that core toward normal logic programs under stable model semantics, global contradiction reasoning, integrity constraints, and numeric verification.
Other nearby lines of work are explicitly adjacent rather than identical. The typed tagless-final embedding for functional conversion is described as sitting conceptually in the miniKanren/stableKanren ecosystem, but it does not name stableKanren explicitly in the text provided; its focus is typed embedding, normalization, mode analysis, determinism analysis, and functional conversion in Haskell (Engel et al., 1 Nov 2025). semiringKanren is even further removed: it is not about stableKanren, contains no explicit mention of stableKanren, and instead reinterprets relation expressions as semiring arrays with SAT-based execution for the Boolean semiring (Volkov et al., 26 Sep 2025).
These distinctions clarify the specific research identity of stableKanren. Its defining concern is not merely relational programming, typed embedding, or alternative execution backends. It is the integration of miniKanren-style relational search with stable model semantics, contradiction handling, integrity constraints, and selective numeric computation. The trajectory visible in the recent work is therefore internally coherent: first, stable-model reasoning is reconciled with ordinary miniKanren code through finer contradiction handling and run-partial; then integrity constraints are added as a top-down exception-like mechanism; then those same constraints are used to support numeric computation and heuristic pruning in combinatorial search (Guo et al., 2024, Guo et al., 2024, Guo et al., 5 Oct 2025).
A plausible implication is that stableKanren is evolving toward a more query-sensitive execution discipline, in which semantic obligations such as unavoidable-contradiction checking are applied only when the dependency structure of the query actually demands them. That direction is stated explicitly in the proposal for a dependency-graph analysis macro, tentatively has-stratified?, so that one interface could decide automatically between conservative and lightweight execution modes (Guo et al., 2024).