set-Kanren: First-Class Set Constraints
- set-Kanren is a relational extension of miniKanren that introduces first-class sets and association lists with specialized constraint solvers.
- It overcomes the limitations of cons-based encodings by enabling declarative, lazy specification and efficient enforcement of set equality and membership constraints.
- The system supports applications in program analysis and interpreters by propagating set and map constraints for more reliable, extensional reasoning.
set-Kanren, written as and read “set-Kanren,” is an extension of miniKanren with first-class relational sets and association lists, together with a dedicated constraint solver for reasoning about abstract collections rather than cons-based encodings. It includes first-class set objects, a functionally complete family of set-theoretic constraints, and new constraints for association lists with shadowing and scoped lookup. These additions allow programmers to describe collections declaratively and lazily, without relying on structural encodings and eager search over representation spaces; they also support set equality based on contents and enable finite failure (Sanna et al., 3 Oct 2025).
1. Relational background and semantic setting
miniKanren is a family of relational programming languages embedded in a host language. Programs are written as relations rather than functions, and the core language is organized around unification, conjunction, disjunction, fresh variable introduction, and relational invocation (Kosarev et al., 2018). A formal study of miniKanren gives a denotational semantics corresponding to the minimal Herbrand model for definite logic programs, and an operational semantics that models interleaving; the operational semantics is proved sound and complete with respect to the denotational semantics (Rozplokhas et al., 2020).
Within this family, “set-Kanren” has two closely related uses. In one use, it signals a Kanren variant where relations are thought of concretely as sets of tuples, or where the system supports set-like constraints and semantics (Kosarev et al., 2018). In the more specific system presented in , the emphasis is not merely on viewing answer streams extensionally, but on adding a dedicated constraint domain for sets and association lists themselves (Sanna et al., 3 Oct 2025).
This distinction is significant. Standard miniKanren already has a set-theoretic denotational reading at the level of solutions, but its data domain is still the ordinary first-order term universe. set-Kanren changes the object language by making sets and maps first-class relational objects.
2. Beyond cons-based encodings
Classic miniKanren is based on logic variables, unification over first-order terms, and data built out of constructors like cons, numbers, and symbols. Because only cons-based structure is primitive, sets are commonly encoded as lists without duplicates, often sorted, and maps are encoded as association lists. The logical notion of “set” or “map” is then only a convention layered on top of lists, enforced by user-written relations (Sanna et al., 3 Oct 2025).
The literature identifies four recurrent problems with this approach. First, it induces representation-dependent reasoning: a single abstract set has many list encodings. Second, it causes inefficient and redundant search, because the search can explore multiple encodings of the same abstract object. Third, it gives poor support for equality and failure: equality of sets by contents is difficult to enforce relationally on top of lists, and it is difficult to detect that no set satisfies a collection of conditions without exploring a large search space. Fourth, encoders leak operational details into specifications, entangling abstract conditions with decisions about insertion order or traversal strategy (Sanna et al., 3 Oct 2025).
set-Kanren addresses these problems by going beyond cons-based encodings and giving sets and association lists first-class constraint support. The intended style is to state constraints such as “ is in this set” or “this map binds key to value ” directly, without committing to a list representation. This shifts the burden of invariants from user code into the solver.
A common misconception is that set-Kanren is simply “lists without duplicates.” The system is explicitly designed to avoid that identification. Its purpose is to reason about abstract sets and maps extensionally, rather than to standardize one particular structural encoding.
3. Constraint domain for finite sets
As a constraint logic programming system, set-Kanren uses a constraint domain of finite sets of miniKanren terms, rather than only Herbrand terms with unification (Sanna et al., 3 Oct 2025). A set variable is a logic variable whose values are finite sets of miniKanren terms, and sets can be partially known: the solver may know some members, some non-members, and some relations such as equality, subset, or disjointness without knowing the entire contents explicitly.
The system exposes a family of primitive constraints whose logical meanings are given extensionally.
| Constraint | Logical meaning |
|---|---|
| Membership | |
| Set equality | |
| Subset 0 | 1 |
| Union 2 | 3 |
| Disjointness | 4 |
| Empty set | 5 |
The set-theoretic basis is described as functionally complete: using these constraints, other set operations can be expressed relationally, and the solver can reason about them without reducing everything to list encodings (Sanna et al., 3 Oct 2025). Extensional equality is central. Two sets are equal if and only if they contain the same elements, regardless of any representation. This is precisely the point at which set-Kanren diverges from structural list equality.
Operationally, the solver maintains partial information about known members, known non-members, and relationships such as equality, subset, union, and disjointness. When a new constraint is added, the solver checks for immediate inconsistency and propagates consequences. Typical propagation principles include deriving 6 from 7 and 8, deriving 9 from 0 and 1, and propagating membership across known equalities (Sanna et al., 3 Oct 2025).
The solver is monotone: once a constraint is added, it is not removed. The store only accumulates consequences or reports inconsistency. This monotonicity is what underlies early pruning and finite failure. In cons-based encodings, failure often requires exploring representation space; in set-Kanren, inconsistency can often be detected directly in the constraint store.
4. Association lists, shadowing, and interpreter-oriented use
set-Kanren extends the same extensional discipline to association lists. Association lists are modeled as relational objects that map keys to values, and shadowing is explicit and first-class (Sanna et al., 3 Oct 2025). Conceptually, a map is a finite sequence of bindings in which later bindings shadow earlier ones. The meaning of the map at key 2 is the value of the last pair with key 3, or undefined if no such pair exists.
Two constraints are central. Lookup is expressed relationally as
4
and extension is expressed as
5
Map equality is likewise extensional: 6 holds when the two maps denote the same finite mapping (Sanna et al., 3 Oct 2025).
These constraints internalize the semantics that is usually programmed manually over association-list structure. If a lookup is performed on an extended environment at the same key, the extended value must be returned; if the key differs, the lookup propagates back to the base environment. Multiple lookup constraints on the same key must agree or the system fails. In this way, the solver captures the “last binding wins” discipline directly.
The main beneficiaries are interpreters, type checkers, and other relational specifications that manipulate environments. The paper emphasizes free-variable analysis and environment-threading in interpreters. For free variables, constraints such as membership, subset, and union let the relation mirror the mathematical definition of free-variable sets. For interpreters, lookup and extend encode environment semantics without requiring recursive traversal over concrete lists (Sanna et al., 3 Oct 2025).
This has both declarative and operational consequences. Declaratively, specifications become closer to the mathematical objects they describe. Operationally, the solver prunes spurious branches caused by multiple encodings of the same environment or set.
5. Operational model, search, and typed embeddings
set-Kanren is implemented on top of a constraint-enabled miniKanren system. Evaluation state is a pair 7, where 8 is a substitution for logic variables and 9 is a constraint store. The store is decomposed by domain, with components such as 0 and 1, and each domain provides an add-and-propagate operation that either returns a stronger store or signals inconsistency (Sanna et al., 3 Oct 2025).
At the search level, set-Kanren inherits miniKanren’s broader operational context. miniKanren implementations use fair, interleaving search rather than depth-first search, and a certified semantics shows how interleaving operational behavior corresponds to the declarative relation (Rozplokhas et al., 2020). A separate complexity analysis shows that, under interleaving search, conventional ways of reasoning about complexity become inadequate because scheduling itself is a key component: asymptotic behavior can change solely because of the position of recursive calls inside conjunctions (Rozplokhas et al., 2022). Any set-Kanren implementation that retains miniKanren-style interleaving search inherits this scheduling sensitivity.
A typed perspective comes from the OCaml embedding of miniKanren. That system represents a goal as a function from state to a lazy stream of states, supports polymorphic unification over arbitrary OCaml values, and provides a generic injection/projection pattern for typed relational specifications (Kosarev et al., 2018). The paper explicitly remarks that these mechanisms give a solid foundation that a set-Kanren could build on. This suggests that typed set-oriented extensions can reuse the same goal-and-stream model while adding set and association-list constraints at the level of the constraint store.
The contrast is therefore not between “search-based” and “constraint-based” systems, but between different domains of constraints layered over the same relational substrate. set-Kanren preserves miniKanren’s relational style while replacing representation-level search over lists with domain-specific propagation over sets and maps.
6. Relation to stable-model and semiring Kanren variants
The Kanren ecosystem contains several nearby but distinct directions. One is stableKanren, which extends miniKanren to normal logic programs under stable model semantics by adding defineo and noto. In that setting, integrity constraints are used to constrain which predicates may be simultaneously true. The comparison drawn in the literature is explicit: set-Kanren and related work focus on reasoning about sets and usually remain in a monotone, Horn-like setting, whereas stableKanren with integrity constraints reasons about which predicates can be true together under non-monotonic stable-model semantics (Guo et al., 2024). The two systems therefore address different kinds of constraints: value-level set constraints in one case, predicate-level model constraints in the other.
Another nearby direction is semiringKanren. There, each well-typed goal denotes an 2-dimensional semiring array over a finite index set, with conjunction interpreted as semiring multiplication and disjunction as semiring addition (Volkov et al., 26 Sep 2025). For the Boolean semiring, each array entry acts like membership in a set of tuples, so Boolean semiringKanren is a set-based relational language and can be understood as a finite-domain, bottom-up set-Kanren. A later polymorphic extension makes the same point more sharply: with an idempotent semiring, repeated derivations collapse in a set-like way, and equality patterns plus large-enough instances provide a non-monomorphizing account of polymorphism for bottom-up relational languages (Volkov, 14 May 2026).
These comparisons delimit the design space. set-Kanren in the 3 sense is a first-class constraint system for finite sets and association lists inside the traditional relational style of miniKanren. stableKanren is a non-monotonic extension centered on predicate truth under stable models. semiringKanren is a bottom-up, algebraic reformulation in which Boolean arrays recover a finite set semantics. The overlap is real, but the semantic commitments are different.
7. Limitations and directions for extension
The current set-Kanren system targets finite sets only. The literature also notes that full first-order set theory is undecidable; the solver therefore supports a decidable and practical subset focused on extensional equality, union, membership, disjointness, and related operations (Sanna et al., 3 Oct 2025). This is a deliberate trade-off: the system gains strong propagation and finite failure, but it does not attempt unrestricted set-theoretic expressiveness.
Constraint propagation also has a cost. Although propagation reduces search space, the solver itself introduces overhead, and the literature notes that for some problems purely list-based encodings may be simpler or faster if extensional reasoning is not needed (Sanna et al., 3 Oct 2025). This is not a contradiction of the design; it is a statement about the boundary between richer constraint domains and their implementation cost.
The proposed extensions are broad. The methodology could extend to other abstract data types such as multisets, relations, and graphs. Richer constraints are suggested as well, including cardinality constraints, ordered maps, and infinite sets described by generators, together with integration with arithmetic. Further work is also directed toward more aggressive propagation algorithms, richer libraries of interpreters and analyses, and formal study of completeness and complexity properties (Sanna et al., 3 Oct 2025).
A related line of work in bottom-up, set-like languages identifies recursive types as a central open design question. semiringKanren and its polymorphic extension obtain clean finite-array semantics by restricting attention to finite algebraic types, and recursive types remain a nontrivial challenge in that setting (Volkov, 14 May 2026). This suggests a broader research tension inside the set-Kanren landscape: the more directly a system treats extensional sets and maps, the more valuable solver support becomes, but the harder it becomes to reconcile such support with unbounded recursive structure and general-purpose host-language data.