miniKanren, Live and Untagged: updated

Per-variable constraint stores, commit-by-commit refactoring, and benchmarks in (aux kanren micro)

(aux kanren micro), the miniKanren of the CHICKEN 6 library aux.scm, descends from the quine-generating system of Byrd, Holk and FriedmanWilliam E. Byrd, Eric Holk, and Daniel P. Friedman. miniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming, Scheme '12, pages 8–29, New York, NY, USA, 2012. Association for Computing Machinery. ISBN 9781450318952. doi: 10.1145/2661103.2661105. URL https://doi.org/10.1145/2661103.2661105. Its disequality, type and absento stores were plain lists: every unification re-verified the type and absento stores whole and scanned the disequalities for those it touched. They have become per-variable constraint records in the manner of faster-miniKanrenMichael Ballantyne and William E. Byrd. faster-miniKanren: A fast implementation of miniKanren with disequality and absento, 2015. URL https://github.com/michaelballantyne/faster-minikanren. Software, 2015--, so that a binding costs in proportion to the constraints on the variables it binds. We describe the result and tell the refactoring commit by commit, dead ends included. Each step was checked by differential testing on tens of thousands of random programs, whose answers stayed byte-identical except for two deliberate changes of clause order. Making answers independent of the order of the stores came first, and it is what let the change of representation be checked at all. Seven later commits remove limitations of that version: answers no longer print the disequalities that the types symbol° and number° imply or those on hidden variables, absento tags that hold variables are walked, set° and groupby° answer their groups in the order of their first state, and the occurs check and left-nested conjunctions are no longer quadratic in the cases measured. We then show the constraints at work, quines, twines and thrines, and the measurements.

1Introduction

aux.scm is a Scheme library for CHICKEN 6, and (aux kanren micro) is its miniKanren. It descends from the system of Byrd, Holk and Friedman’s 2012 pearlWilliam E. Byrd, Eric Holk, and Daniel P. Friedman. miniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming, Scheme '12, pages 8–29, New York, NY, USA, 2012. Association for Computing Machinery. ISBN 9781450318952. doi: 10.1145/2661103.2661105. URL https://doi.org/10.1145/2661103.2661105 and keeps its constraints ≠°, symbol°, number° and absent°, its untagged relational interpreters and its reifier. Its core is in the style of microKanrenJason Hemann and Daniel P. Friedman. microkanren: A Minimal Functional Core for Relational Programming, November 2013. URL https://shu.elsevierpure.com/en/publications/microkanren-a-minimal-functional-core-for-relational-programming/. Workshop on Scheme and Functional Programming ; Conference date: 13-11-2013 Through 13-11-2013: freshª, orª and andª build goals from states to streams, and the state carries a variable counter. The arithmetic follows The Reasoned SchemerDaniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann. The Reasoned Schemer. The MIT Press, 2nd edition, 2018. ISBN 0262535513. URL https://dl.acm.org/doi/10.5555/3267158.

Up to b23f7e2 the state was the pearl’s package (S D A T), and every =° that extended S re-verified all of A and T, and the prefixes of D that mention a variable it bound (after a scan of the whole of D): the pearl’s post-unify-≡ with verify-D, verify-A and verify-T (pp. 18/24–19/25), less the untouched disequalities, so a binding cost in proportion to the size of the stores. At 2d2fb4e the constraints live in per-variable records, as in faster-miniKanrenMichael Ballantyne and William E. Byrd. faster-miniKanren: A fast implementation of miniKanren with disequality and absento, 2015. URL https://github.com/michaelballantyne/faster-minikanren. Software, 2015--, and a binding costs in proportion to the constraints on the variables it binds: symbol° on 1600 variables bound one by one goes from 195 s at b23f7e2 to 13 ms (Table 8), and a 6400-variable mixed store from 81.6 s with the keyed stores of 91a5322 to 0.20 s. We describe the current design and its invariants, and tell the story of b23f7e2..12d318f commit by commit, dead ends included; on the way answers became a function of the contents of the stores only, a stronger form of the pearl’s Church–Rosser property (p. 21/27), and 37 new tests came to cover quines, twines, thrines and the pearl’s App. A. Seven later commits (Section 3.6) remove limitations that 12d318f left, in its answers, in absent°, in aggregation, and in two quadratic costs: walk° over a fresh list of 4000 cells goes from 11.8 s to 63 ms (Section 6.1).

Verification. Every step was checked by differential testing: about 19,000 random programs (plus 16,000 with hidden variables), their reified answers compared byte for byte between builds, and on the keyed-stores branch also raw-store traces and a shadow build (Section 3). Up to 12d318f only two commits change answers on purpose: e09288a (178 answers, only in the order of their absent? clauses) and c52c365 (500 answers, only in the order of clauses or of the conjuncts of an and). Of those programs, 47 answer differently at d5b6f34, each only by dropping clauses: 30 a disequality that the types imply (d09d5c5), 17 the clauses on a hidden variable (f569f8d). On 240,000 more random programs that mix the constraints, with tags that hold variables, no answer of d5b6f34 accepts a non-solution or misses a ground solution.

Conventions. Pages of the pearl are cited as PDF page/printed page, as in p. 18/24. An answer is an executable form (λ (α …) clause … term): applied to values for its variables, it checks the constraints and builds the term. For example, (fresh° (q) (absent° 'closure q) (symbol° q)) answers ((λ (α) (begin (deny (equal? α 'closure))) (assert (every (μ v (symbol? v)) (list α))) α)). Outputs are shown as pp prints them, with 'x for (quote x), unless marked as write form. In names, ° marks a goal, ª a goal constructor and § a stream. Code is quoted at d5b6f34, and micro.scm:nn is line nn of its src/aux.kanren.micro.scm.The tests are in src/test/; those on quines and interpreters in microkanren-untagged.scm. Section 2 describes the implementation, Section 3 its history, Section 4 the constraints at work, Section 5 quines and the extended interpreter, Section 6 performance, and Section 7 the relation to the pearl and to faster-miniKanren.

2The implementation now

This section describes src/aux.kanren.micro.scm at d5b6f34, after the seven commits of Section 3.6.Code is quoted as in the source, less two spaces of module indentation.

2.1Variables and the state

A variable is a record holding an index ii, as the pearl suggests (p. 20/26). Working variables have i≥0i \geq 0, their slot in S and C; reified ones have i=−1,−2,…i = -1, -2, \dots and print as α, β, …, α1. freshª only increments vars-count, as microKanren’s call/fresh doesJason Hemann and Daniel P. Friedman. microkanren: A Minimal Functional Core for Relational Programming, November 2013. URL https://shu.elsevierpure.com/en/publications/microkanren-a-minimal-functional-core-for-relational-programming/. Workshop on Scheme and Functional Programming ; Conference date: 13-11-2013 Through 13-11-2013. The state is a record μkanren-state with the eight fields of Table 1; the pearl’s lists D, A and T survive as views folded from P and C. The source states six invariants;micro.scm:91–98. the slack they allow, stale ids, stale t and stale variables in W, costs time, not answers.

I1: “a variable with a record is unbound: =° hands the constraints of a variable it binds on” (Section 2.4). I2: “the left sides of the pairs of a prefix are unbound, and their right sides are non-variables or unbound”, since prefixes are unifier assocs. I3: “a variable with a type tag has no absento tags, which become disequalities”: a typed value is atomic, so absence is ≠, the pearl’s update-D/T (p. 19/25) done eagerly. I4: “no tag of a variable subsumes a prefix with the variable on the left side of a pair”: the pearl’s subsume, kept at all times. I5: “the answer of μkanren-tag-unify on a compound key of T changes only when a variable with t is bound”, so =° skips Tc unless it binds such a variable. I6: “the datum of an absento tag of a variable holds neither that variable nor a bound one, and each variable it holds lists that variable in W”, so a tag whose datum holds variables is posted again when any of them is bound (Section 2.5).

Table 1: The fields of μkanren-state; an sbral is a skew-binary random-access list. The pearl’s package is the list (S D A T) of association lists (p. 16/22).
FieldContents
vars-countthe number of variables made so far
Sthe triangular substitution, an sbral by variable
M(m . X), which spares occurs checks (Section 2.2)
Can sbral of per-variable records μkanren-c, indexed like S
Pan sbral of the disequality prefixes by id; #f once dropped
Tcthe absento entries (u . tag) whose key u is compound
tagsthe type tags asserted so far
raw#f, or lists (D A T) not yet indexed (Section 2.2)

2.2Substitution, walk and unification

S is an sbralChris Okasaki. Purely functional random-access lists. In Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture, FPCA '95, pages 86–95, New York, NY, USA, 1995. Association for Computing Machinery. ISBN 0897917197. doi: 10.1145/224164.224187. URL https://doi.org/10.1145/224164.224187: the pearl’s walk is an assq, and “various persistent structures, such as those in (Okasaki 1999), would improve the performance” (p. 20/26,Chris Okasaki. Purely Functional Data Structures. Cambridge University Press, 1998. URL https://www.cambridge.org/core/books/purely-functional-data-structures/0409255DA1B48FA731859AC72E34D494). The sbral grows at its front, so slots count from the back, and binding a variable pads S with the marker *μkanren-unbound*. The walk answers a variable beyond S, or a slot holding the marker, as unbound; walks over whole terms compute the length of S once.

(μkanren-unify u v s assocs) answers #f or (s* assocs*), with exactly the new bindings added to assocs, and returns s itself when nothing is bound, so μkanren-state-equal? is eq?. The assocs replace the pearl’s prefix-S (p. 18/24); they are faster-miniKanren’s addedMichael Ballantyne and William E. Byrd. faster-miniKanren: A fast implementation of miniKanren with disequality and absento, 2015. URL https://github.com/michaelballantyne/faster-minikanren. Software, 2015--. Of two variables the older is bound to the newer; strings, numbers (NaN-reflexively) and the slots of vectors and records are compared too. The internal unifier keeps the stores, sound only because =° hands the constraints on. The exported one, like make-μkanren-state and μkanren-state-with, leaves (D A T) lists in raw instead, and the next goal indexes them with μkanren-state-index, posting every entry afresh: the pearl’s whole-store verification, run once.

The two quadratic costs of Section 6.1 are gone where the benchmarks time them. Binding α to v walks v for the occurs check (μkanren-state-update, micro.scm:218–257) unless M, the pair (m . X) of Table 1, rules α out: “X lists at most four indices greater than m, and every unbound variable written in a value of S has index at most m or is in X”. μkanren-unify/held tells whether v is a part of a value of S; a walk from such a part meets only variables written in S, so if M does not admit α the check is skipped. Otherwise it walks as before and M admits the variables it meets, a full X folding into m; a state whose S comes from outside admits every variable.

((andª f g) s) is (append-map§ g (δ (f s))); in a chain nested to the left, as and° and fold build it, each bind wrapped again every promise of the binds inside it. The goal of andª now carries as procedure data the innermost goal f and the list gs of the others, last first, which a further andª extends instead of nesting,micro.scm:948–971; aux.stream.scm:116–121. An and° goal is thus an extended procedure, whose procedure-data is a μkanren-binds record (false at 12d318f): extend-procedure and set-procedure-data! change it in place, and its chain then nests again. and runs them as one (append-map§/fold gs (δ (f s))), that is (fold append-map§ $ gs) step by step, with one promise for the whole chain. Some chains stay quadratic (Section 6.1).

2.3Records C, prefixes P, compound keys Tc

As in faster-miniKanren, each variable has a record (μkanren-c A T D t W), in C at its index: A is the type tag, T the absento tags, D the ids of the prefixes that mention the variable, t whether it is reachable from a compound key in Tc, and W the variables with an absento tag whose datum holds it (I6). A prefix, the pairs ((α . u) …) a disequality forbids, is stored once in P under an id that never moves, and listed under every left side and every variable right side. Dropping it writes #f into P; dead ids are pruned lazily. faster-miniKanren copies the disequality onto the variables of its first pair only; listing it under every pair lets a new tag on α find every prefix with α on a left side (I4), and one shared copy vanishes everywhere at once.

2.4=° and μkanren-repost

(=° u v) unifies; if S changed, it indexes the raw stores, if any, and otherwise re-posts the records of the variables it bound,micro.scm:929 and micro.scm:884. with the heart of the design:

(define (μkanren-repost s assocs)
  (let R ((assocs assocs) (s s) (Tc? #f))
    (cond
      ((not s) #f)
      ((null? assocs) (if Tc? (μkanren-repost-Tc s) s))
      (else (let* ((β (lhs (car assocs))) (c (μkanren-c-ref s β)))
              (if (μkanren-c-empty? c)
                  (R (cdr assocs) s Tc?)
                  (let* ((s (μkanren-repost-prefixes β (μkanren-c-D c) s))
                         (s (if (and s (μkanren-c-A c)) (μkanren-post-type β (μkanren-c-A c) s) s))
                         (s (μkanren-fold/and (λ (tag s) (μkanren-absento+ β tag s)) s (μkanren-c-T c)))
                         (s (μkanren-fold/and (λ (α s) (μkanren-repost-datum α β s)) s (μkanren-c-W c))))
                    (R (cdr assocs) s (or Tc? (μkanren-c-t c))))))))))

Only the variables in assocs changed their walk, so constraints on others read as before, compound keys aside (I5). For each bound β with a record, μkanren-repost-prefixes unifies each of its prefixes in the new S: if that fails, the prefix is dropped as satisfied; if it binds nothing, the disequality is violated and =° fails; otherwise the new extension d* replaces it, again unifier assocs (I2). All are tried before any is replaced. β’s record is emptied (I1), the d* are added anew (re-checking I4), and the type and absento tags are posted again on β’s walk. For each α in β’s W, μkanren-repost-datum posts again on α those of its absento tags whose datum holds β, and only those (I6). Tc is posted again only if some bound β had t. Section 6 measures the effect. This is faster-miniKanren’s update-constraints, plus the all-or-nothing trial and the t/Tc mechanism; the pearl’s post-unify-≡ instead verifies the whole of D, A and T on every == that extends S (pp. 18/24–19/25).

2.5≠°, type tags and absent°

(≠° u v) unifies u and v: equal terms fail; otherwise the assocs, if any, are stored as a prefix and s* is discarded, as in the pearl (p. 18/24). A prefix with a pair (α . u) that a tag of α denies at the top of u is skipped (I4), like the pearl’s subsumed-pr?. A variable takes one type tag: the same again is a no-op, a different one fails; the new tag drops the prefixes it subsumes and turns the absento tags into one-pair prefixes ((α . datum)) (I3). The pearl’s update-D/T hard-codes 'sym and 'num (p. 19/25); this works for any tag made with define-μkanren-tag, whose comment states what a tag must be: atoms only, infinitely many of them, none that another tag holds. The suite tests a third one, string°, defined through the exported API.

The pearl’s absento tag must be a symbol (p. 19/25); absent° takes any datum and looks at the walk of the term: an unbound variable records the tag (or a disequality, if typed), an atom must satisfy the predicate, and a compound is compared with the datum by μkanren-tag-unify, which fails, recurs into the parts, or, when the compound could still become the datum, watches it whole in Tc and sets t on the variables reachable from it (Section 4).

A datum that holds variables is walked whenever the tag is posted (μkanren-absento/term+), as by faster-miniKanren’s generalised absento. Once its walk is ground, the tag takes the path above. Until then every node of the term must differ from it: a compound or an atom takes a disequality with the datum, as does a typed variable (I3), and an untyped variable keeps the tag in its T and is listed in the W of each variable of the datum. A variable that is the datum fails; one that the datum holds takes nothing, since no term holds a term that holds it.

2.6Reification

μkanren-projectmicro.scm:550–566. (1) reifies the answer into variables −1,−2,…-1, -2, \dots (the pearl’s reify-S, p. 21/27); (2) drops prefixes and type tags on variables hidden from the answer, walking into the terms that variables are bound to, since a right side may hold a variable bound after the prefix was posted; (3) splits into their parts the compound absento keys that hold a hidden variable, and drops a datum that holds one; (4) drops prefixes that another implies (μkanren-rem-subsumed), keyed on the rarest variable; (5) subsumes prefixes by type tags, as the pearl’s subsumed-pr? does and, when the answer holds both symbol° and number°, also those that equate a variable of one with a variable of the other, by a pair, as faster-miniKanren’s d-subsumed-by-T? finds them, or through a third variable, as (≠° (list x y) (list z z)), and drops duplicate absento entries; (6) sorts, ties broken on the printed term (Section 3.4) and then on the written one, since ->string displays 1 and "1" alike; and (7) forms the executable answer, where a datum with variables prints as a term that absent? evaluates. Unlike the pearl’s (term (≠ …) …) (p. 21/27), such an answer can be applied, and so checked (Section 5.1).At d5b6f34 the eleventh and twelfth reified variables are named λ and μ, which the answer form uses: an answer with twelve variables or more and a type tag cannot be applied, since its parameter μ shadows the μ of the type assertion (“unbound variable”). Commit 8046761 leaves both letters out of the names, which run α … κ, ν … ω, then α1, β1 and so on.

3From lists to records: the refactoring, commit by commit

Apart from 12d318f, which adds tests, the commits change the file src/aux.kanren.micro.scm; most also add unit tests to src/test/microkanren.scm, b2fa67d adds two sbral operations to src/aux.fds.sbral.scm, and the follow-ups of Section 3.6 also touch src/aux.stream.scm, src/aux.base.scm, src/aux.kanren.micro.show.scm and a comment of src/aux.anthropic.scm.The file has 713 lines at b23f7e2, 1019 at 4fa54f9, its peak on the keyed-stores branch (982 at the branch top 17e82f7), 942 at 2d2fb4e and 1150 at d5b6f34. The keyed stores were developed on a branch while e09288a went to master. Answers stay byte-identical unless a commit says otherwise.

3.1A dead end and two list-side fixes (5773e7a)

Table 2: 5773e7a, best of 5, from its commit message; the other workloads were within noise.

workloadbefore (ms)after (ms)
≠° 20 distinct full14770
perm7 ≠° search13671240
perm6179166
3000-prefix ≠°13041248

The first experiment made the stores sbrals, flat and recursive (each prefix of D too). Both kept the answers and neither was faster: a store sees only front pushes, scans that stop at the first hit and rebuilds by right fold, so logarithmic access buys nothing, while each element costs a tree walk and about four times the allocation; workloads with one big store were 2–11% slower. The lever was algorithmic: an index keyed by variable. The experiment’s only gains came from two changes independent of the representation, which 5773e7a keeps on lists (Table 2): μkanren-subsumed? builds the state of d once, not at every step along D, and μkanren-state-unify/assoc folds the unifier over the pairs instead of unifying two lists built for the purpose.

3.2T reversed once per key of A (e09288a)

Table 3: Order of the clauses on α for 'a and 'z, by the number of other typed variables.

build012
b23f7e2, 5773e7az,aa,zz,a
e09288a, 91a5322z,az,az,a
c52c365, 2d2fb4ea,za,za,z

μkanren-update-D/T+, like the pearl’s update-D/T+ (p. 19/25), rebuilt T by consing the entries it keeps, so T came out reversed; called once per key of A, it reversed T once per typed variable on every verifying =°. Answers sorted their absent? clauses by variable only, tags printing alike under ->string, so the ties showed the parity of |A|: a type tag on one variable reordered the clauses of another (Table 3, for (fresh° (q x) (=° q (list x)) (absent° 'a x) (absent° 'z x)) with zero, one or two typed variables added to q), against the Church–Rosser property the pearl asks of answers (p. 21/27). e09288a reverses the kept entries once, at the end; of about 19,000 random programs, 178 answers changed, only in the order of the absent? clauses of one variable.

3.3The keyed stores (b2fa67d..17e82f7, merged as 91a5322)

Table 4: Along the keyed branch, best of 3, in ms, each value from the message of the commit that measured it; 5773e7a was master. * symbol° posted on 400 variables, which are then bound. ** the three constraints on 100 variables, bound one by one.

commitsymbol° 400*DAT 100**≠°+symbol° 100
5773e7a87121071849
b2fa67d168953–
4ece3ad109197386
086acaa–118366
a5e0e53–95352
f0dc9a1––33

The keyed-stores branch skips the verification on the many =° that cannot affect the stores, and keys the remaining searches by variable, with answers byte-identical to master; b2fa67d to f0dc9a1 were also checked on the raw stores of 24,000 programs and by a shadow build that ran the unkeyed code next to the keyed one. b2fa67d adds marks M, an sbral of bitmasks indexed like S that over-approximates which variables D, A and T mention, and a dirty flag, set when some prefix of D may be subsumed by A or T. An =° on a clean state that binds no marked variable is quiet and answers the unified state unverified; the later commits key the remaining work on the marks (Table 4), down to μkanren-rem-subsumed, which f0dc9a1 keys by variable and 4fa54f9 on the variable occurring in the fewest prefixes (a fresh x kept ≠° from each of 600 older variables, reified: 3078 to 124 ms).

Two bugs of the branch were caught on the way. A goal that bound through the exported μkanren-state-unify kept its marks and clean flag, so the next =° could be quiet and skip a check that master runs, losing an absent°; bac18c9 lets the exported unifier answer a state without marks. And srfi-69 salts each table built on its own hash functions with a rand() draw, so the stores’ tables moved the salt of later ones and the order of set° and groupby° answers; 4fa54f9 hashes them without salt. This restores master’s order, not determinism: set° and groupby° still build a salted table of their own, so their order is reproducible only per fresh process, on b23f7e2 and 2d2fb4e alike.

A quiet =° also skips the reversals of T of Section 3.2, which reached the answers, so the branch had to reproduce them, with a reversal when |A| was odd, then fields A-odd and T-reversed, then reversals owed by parity; the shadow build caught a mutation dropping an owed reversal. Once e09288a fixed the order at its source, 17e82f7 deleted all of it (+40/−77).

3.4Answers independent of store order (c52c365)

Table 5: Clause order, old (up to 91a5322) as posted and reversed, and new. Queries on q: absent° of 'a, '(b), 'c; ≠° of two box records 1 and 2; ≠° of 'a and "a".

oldnew
absentoc (b) a / a (b) ca c (b)
records2 1 / 1 21 2
'a "a""a" 'a / 'a "a""a" 'a

μkanren-form sorted D and T on ->string prints, under which every record and every tag print alike, so ties kept the store order. Records by variable keep no posting order at all, so unless answers became a function of the store contents only, they could not be checked byte for byte against lists. c52c365 breaks the ties on the printed (repr x), which spells out the slots of a record and the datum of a tag, sorting the pairs of each prefix, the prefixes and the absento entries; D is also sorted before μkanren-rem-subsumed picks which of two equivalent prefixes to keep (Table 5). Reversing D, A and T before projection now changes no answer of about 19,000 random programs; against master, 500 answers changed, each only in the order of its clauses or of the conjuncts of an and.

3.5Per-variable records (2d2fb4e)

With canonical answers the representation was free. 2d2fb4e introduces the design of Section 2 and removes the marks, the dirty flag, the quiet test and the rebuilds of A, T and D with all their bookkeeping (the keyed μkanren-rem-subsumed at projection stays): the eight-line verifying branch of =° becomes one line that calls μkanren-repost (Section 2.4). The answers are byte-identical to c52c365 on the same random programs (plus 16,000 with hidden variables). Binding the variables one by one, the commit message reports, against the keyed stores of 91a5322, a 6400-variable mixed store going from 81.6 s to 0.20 s, a symbol° store from 16.6 s to 51 ms, and all-distinct on 400 variables from 12.8 s to 7.3 s; ≠° on 20 distinct variables is 2 ms slower, because of the sort of c52c365. 12d318f then adds the 37 tests of Section 5, and closes the refactoring.

3.6After the records (f569f8d..d5b6f34)

Table 6: The follow-ups, and how many random programs (about 19,000, and 16,000 with hidden variables) answer differently from the previous commit. * None of them aggregates.

commitchangeprograms
f569f8dhidden behind a binding17
aaa50d0ties on write0
9cecddftags with variables0
d09d5c5disjoint types29+1
f8f28b7a column twice0*
a0382e3groups by first state0*
d5b6f34two quadratic costs0

Seven commits followed 12d318f (Table 6). Four carry out the future work it left: 9cecddf, d09d5c5, a0382e3 and d5b6f34; f569f8d, aaa50d0 and f8f28b7 fix bugs found on the way. Section 4 shows three answers they change, H1 to H3.

f569f8d makes μkanren-anyvar? walk into bound terms, so that a variable bound after a prefix was posted no longer brings a hidden working variable into the answer (H3): 17 programs of the hidden-variable fuzzer change, in 30 answers, each only losing the clauses that named a working variable. aaa50d0 breaks the ties left by Section 3.4 on the datum as write prints it, since ->string displays 1 and "1" alike: (≠° q 1) (≠° q "1") printed its clauses in the order of the goals, and both orders now print the clause on "1" first. 9cecddf walks the absento tags that hold variables, as faster-miniKanren’s generalised absento does (H2, Section 2.5). On 18,000 programs with such tags the answers agree with those of the same programs with the tags grounded, where 12d318f gives 188 wrong answers; 232 differ in form only, a compound node taken apart before the datum is ground printing as the disequalities of its nodes. d09d5c5 drops at reification the disequalities that the two library tags imply (H1, Section 2.6): 36 answers of 29 random programs change, and one of the hidden-variable fuzzer, each losing one such deny, and none of the 37 printed at 12d318f remains.

No fuzzer above aggregates. Two equal? aggregated expressions shared one table entry, so a column aggregated twice, as a sum and a count for an average, saw each value twice: summing and counting the salaries of empsalary° by department gave (develop 50200 10), and f8f28b7, with one list of values per expression, answers (develop 25100 5). The groups came in the order of the buckets of a salted srfi-69 table (Section 3.3), which building zero to three tables before a query moved in 1116 of 2420 random programs; a0382e3 folds the stream from the left, with a new fold§, and answers the groups in the order of their first state.

d5b6f34 removes the two quadratic costs of Section 6.1 in the cases timed there (Section 2.2). The random programs, the hidden-variable fuzzers, 360,000 random occurs-check programs and the stream traces of 62,000 random goals, plus 40,000 that aggregate, are byte-identical.

4The constraints at work

This section runs the constraints of Section 2 on small queries, each with (°->list #f …), which returns every answer. Each block of queries is followed by their answer lists, in the same order, in write form ((quote a), not 'a), exactly as a run on a build of d5b6f34 prints them, broken into lines only where too long. The queries before the last paragraph give byte-identical answers on all six builds from b23f7e2 to 2d2fb4e and on d5b6f34, so the refactoring of Section 3 and the later commits preserved these behaviours.The labels are those of our example script, which holds 57 queries in groups A to E and G, and three in group H; only the order examples of group E differ between the six builds (Sections 3.2 and 3.4), and only group H between 2d2fb4e and d5b6f34.

Absento on a typed variable. A symbol contains the symbol closure only by being it, so on a symbol° variable the absento becomes a disequality (A1: the example of Section 1, goals swapped, in write form). A number contains no symbol, so under number° the absento disappears, as does a compound tag under symbol°. The converted disequality still fires when the variable is bound (A6).

(fresh° (q) (symbol° q) (absent° 'closure q))  ; A1
(fresh° (q) (symbol° q) (absent° 'closure q) (=° q 'closure))  ; A6
((λ (α) (begin (deny (equal? α (quote closure)))) (assert (every (μ v (symbol? v)) (list α))) α))
()

Subsumption. A type tag subsumes a disequality that it implies: symbol° implies ≠ 5, which is dropped (B1), while under number° it stays. A list disequality is one prefix, the disjunction x ≠ 1 ∨ y ≠ 2; it is subsumed as soon as one of its pairs is ruled out, here the second (B5). Tags deny only at the top of a right side, so (absent° 'a q) (≠° q '(b a)) keeps a redundant disequality: as with the pearl’s subsumed-pr?, this costs a printed clause, not an answer.

(fresh° (q) (symbol° q) (≠° q 5))  ; B1
(fresh° (q x y) (=° q (list x y)) (symbol° y) (≠° (list x y) (list 1 2)))  ; B5
((λ (α) (assert (every (μ v (symbol? v)) (list α))) α))
((λ (α β) (assert (every (μ v (symbol? v)) (list β))) (cons α (cons β (quote ())))))

Hand-off through =°. When =° binds a variable, μkanren-repost empties its record and posts the contents on its walk (Section 2.4). C9 chains three steps: the absento on the hidden x is split over (list y q), handed on to q, and converted under symbol°; nothing of it remains on the hidden y. Two clashing type tags that meet on the walk fail.

(fresh° (q) (fresh° (x y) (absent° 'a x) (=° x (list y q)) (symbol° q)))  ; C9
((λ (α) (begin (deny (equal? α (quote a)))) (assert (every (μ v (symbol? v)) (list α))) α))

Compound datums. A partly bound term that could still become the datum is watched whole, as a compound key in Tc, so the clause of D1 is on (cons 1 α), not on α, and completing the term to the datum fails (D2). The constraint means “no node of the term equals the datum”, as for a Scheme absent?: the tail (1 2) of (0 1 2) is a node, so D5 fails (with '(0 1 2 3) it succeeds, since (1 2 3) is not (1 2)). At reification a watched term that holds a hidden variable is broken into its parts (μkanren-project-T): the hidden variable can always keep the whole apart from the datum, but the parts still constrain the visible variables (D8). Atoms are compared as unification compares them, NaN-reflexively, so (absent° +nan.0 q) fails once q is bound to +nan.0.

(fresh° (q x) (=° q (cons 1 x)) (absent° '(1 2) q))  ; D1
(fresh° (q x) (=° q (cons 1 x)) (absent° '(1 2) q) (=° x '(2)))  ; D2
(fresh° (q) (=° q '(0 1 2)) (absent° '(1 2) q))  ; D5
(fresh° (q) (fresh° (z) (absent° '(1 2) (cons q z))))  ; D8
((λ (α) (assert (absent? (quote (1 2)) (cons 1 α))) (cons 1 α)))
()
()
((λ (α) (assert (absent? (quote (1 2)) α)) α))

List prefixes. A list disequality is stored as one prefix and printed as (deny (and …)) (G1); binding x to 1 would shrink it to y ≠ 2, and to 3 satisfy the disjunction and drop it. At reification a prefix that another implies is dropped by μkanren-rem-subsumed: in G5 the longer prefix goes, because y ≠ 2 alone implies it. G6 is the test test/≠°/subsumed/keyed: the prefix x ≠ y ∨ z ≠ 1 is subsumed by x ≠ y, which the keyed search of Section 3.3 must find through y, the later variable, and not only through x, the left side of its first pair, whatever the order of the goals.

(fresh° (q x y) (=° q (list x y)) (≠° (list x y) '(1 2)))  ; G1
(fresh° (q x y) (=° q (list x y)) (≠° (list x y) (list 1 2)) (≠° y 2))  ; G5
(fresh° (q x y z) (=° q (list x y z)) (≠° x y) (≠° (list x z) (list y 1)))  ; G6
((λ (α β) (begin (deny (and (equal? α 1) (equal? β 2)))) (cons α (cons β (quote ())))))
((λ (α β) (begin (deny (equal? β 2))) (cons α (cons β (quote ())))))
((λ (α β γ) (begin (deny (equal? α β))) (cons α (cons β (cons γ (quote ()))))))

Three answers that changed. The commits after 2d2fb4e (Section 3.6) change answers in three ways. No value is both a symbol and a number, so the disequality of H1 is implied by the types, and μkanren-subsume now drops it at reification, as faster-miniKanren does;micro.scm:589–613. Only the two tags of the library count as disjoint; the disequalities of a tag defined by the user stay. 12d318f printed (begin (deny (equal? α β))) before the two assertions. An absento tag that holds variables is now walked when it is posted, and posted again when one of its variables is bound.micro.scm:811–829. In H2 the tag t becomes 'a, so binding q to 'a fails; 12d318f compared q with the variable t itself and answered ((λ () (quote a))). In H3 the right side of the prefix holds e, bound to a term that holds the hidden f. μkanren-anyvar? now walks into that term,micro.scm:469–476. so the prefix is dropped as every prefix on a hidden variable is; 12d318f kept (begin (deny (equal? α (cons (cons 1 _2) (quote ()))))), with the working variable _2, and the answer could not be evaluated.

(fresh° (q x y) (=° q (list x y)) (symbol° x) (number° y) (≠° x y))  ; H1
(fresh° (q t) (absent° t q) (=° t 'a) (=° q 'a))  ; H2
(fresh° (q) (fresh° (e f) (≠° q (list e)) (=° e (cons 1 f))))  ; H3
((λ (α β) (assert (every (μ v (symbol? v)) (list α))) (assert (every (μ v (number? v)) (list β))) (cons α (cons β (quote ())))))
()
((λ (α) α))

5Quines, twines, thrines and the extended interpreter

The 37 tests that 12d318f adds replay the pearl’s queries of sections 3–6 and App. A, checking answers against the pearl and against Scheme’s eval. They are unchanged and pass at d5b6f34; here is what they print there, byte for byte as on 2d2fb4e.CPU ms around one query on d5b6f34, library compiled, relations under csi, one process per query; medians of 8 runs interleaved with 2d2fb4e, whose medians are within 5%, but 14 against 11 ms for the factorisations of 6. Section 6 compares the builds. aux.scm binds λ to lambda, so generated programs run in the REPL as they are, the point of untagged interpreters (p. 9/15).

On a fresh program, eval-exp° enumerates program–value pairs (p. 7/13).(take° 6 (fresh° (q e v) (eval-exp° e '() v) (=° `(,e → ,v) q))), 1–2 ms. Its first answer, (quote α) → α, asserts (absent? 'closure α): quoted data may not contain the tag closure, so closures and quoted lists stay apart without tagged applications (p. 8/14). Answers 3 and 6 are (λ (α) β) → (closure α β ()), with α a symbol, and (list (quote α) (quote β)) → (α β).

5.1The run-1 quine

(take° 1 (fresh° (q) (eval-exp° q '() q))), reified, answers in 81 ms.Instantiated, about the same (78–88 ms). Its four constraint clauses, verbatim, and the instantiated term in place of the term builder:

(λ (α)
   (begin (deny (equal? α 'closure)))
   (begin (deny (equal? α 'list)))
   (begin (deny (equal? α 'quote)))
   (assert (every (μ v (symbol? v)) (list α)))
; term:
((λ (α) (list α (list 'quote α))) '(λ (α) (list α (list 'quote α)))))

They are the pearl’s (=/= ((_.0 closure)) ((_.0 list)) ((_.0 quote))) and (sym _.0) (p. 8/14). The answer, Q1, is a procedure: ((eval Q1) 'x) is equal? to the pearl’s quinec (p. 2/8), and applying it to 'closure, 'list or 'quote raises, since a deny fails. Fixing the parameter to x with (=° `((λ (x) ,b) ,r) q) returns quinec itself (38 ms).

Table 7 runs the body of Q1, without its clauses, with α each of the three forbidden symbols and λ. With quote and list the program breaks everywhere. With closure it is a genuine Scheme quine that only the untagged interpreter rejects: the absent° 'closure on quoted data, turned into α ≠ closure because α is a symbol, is the price of having no tags. Shadowing λ is harmless, as the body never uses it as a keyword, so no α ≠ λ is posted.

Table 7: The body of Q1 with α fixed, run by eval-exp°, by the functional eval-exp and by Scheme’s eval. (): no answer; †: match/non-overlapping finds no match; ‡: call of non-procedure.

αeval-exp°eval-expeval
quote()error†error‡
list()error†error‡
closure()itselfitself
λitselfitselfitself

5.2More quines

The second quine (314 ms, reified; clauses verbatim, term in write form):

(λ (α β γ)
   (begin (deny (equal? α 'closure)))
   (begin (deny (equal? α 'list)))
   (begin (deny (equal? α 'quote)))
   (begin (deny (equal? α 'λ)))
   (begin (deny (equal? β 'closure)))
   (begin (deny (equal? β α)))
   (assert (every (μ v (symbol? v)) (list α β)))
   (assert (absent? 'closure γ))
; term:
((λ (α) (list ((λ (β) α) (quote γ)) (list (quote quote) α)))
 (quote (λ (α) (list ((λ (β) α) (quote γ)) (list (quote quote) α))))))

The inner λ lies in the scope of α, so α may not shadow it; β may not be α, or the redex would return γ; and γ is quoted junk, constrained only by absent?.

The first tenRun 10 of quine°, instantiated, 1140 ms. are distinct, and each is a fixed point both of Scheme’s eval and of eval-exp°. #1 is the run-1 quine; #4, ((λ (α) (list (list 'λ '(α) α) (list 'quote α))) '(list (list 'λ '(α) α) (list 'quote α))), rebuilds its (λ (α) …) wrapper with list, so it quotes only its body; #9, ((λ (α) ((λ (β) (list β (list 'quote β))) α)) '…), hands the work from α to β. #2, #3, #5, #7 and #8 splice a dead redex ((λ (β) …) 'γ) into quinec.

5.3Twines and thrines

twine° is (fresh° (p q) (≠° p q) (eval-exp° p '() q) (eval-exp° q '() p) (=° `(,p ,q) x)) (p. 8/14). Its run 1 is the pearl’s pair (p q):973 ms; reified, 951 ms, with the four clauses of the run-1 quine. The second twine takes 4552 ms and has the constraints of the second quine.

(('((λ (α) (list 'quote (list α (list 'quote α))))
    '(λ (α) (list 'quote (list α (list 'quote α)))))
  ((λ (α) (list 'quote (list α (list 'quote α))))
   '(λ (α) (list 'quote (list α (list 'quote α)))))))

p is (quote q), and q is quinec with one more (list 'quote …) around its result. The (≠° p q) matters: with (=° p q) the query returns the run-1 quine paired with itself (92 ms), since every quine is a twine.

With three pairwise ≠° and the cycle p→q→r→p, run 1 gives the pearl’s thrine (p. 8/14) in 5.4 to 6.1 s, with the same four clauses. With R = (λ (α) (list 'quote (list 'quote (list α (list 'quote α))))), it is r = (R 'R), q = 'r and p = ''r. In general an n-cycle is a quinec that wraps its result in n−1n-1 extra (list 'quote …), with its quotations 1 to n−1n-1 times. The test checks that no member is a quine or a twine: the cycle has length exactly 3.

5.4(I love you) and shadowing

The 30 programs that evaluate to (I love you) (81 ms) all eval to it in Scheme, the sixth, ((λ (α) '(I love you)) (λ (β) γ)), only because the λ with the free γ is never applied. A parameter may shadow quote, list or λ, since each keyword is guarded by not-in-env° (p. 6/12): backwards, the body q of ((λ (quote) ,q) (λ (x) x)) that gives (closure x x ()) can be quote, or (quote quote), which applies the identity to itself, among the first six answers (run 6, 25–29 ms).

5.5The extended interpreter of App. A

App. A (pp. 10/16–11/17) adds booleans, if, pairs and binary arithmetic; the arithmetic comes from (aux kanren arith), afterDaniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann. The Reasoned Schemer. The MIT Press, 2nd edition, 2018. ISBN 0262535513. URL https://dl.acm.org/doi/10.5555/3267158, not from App. BOleg Kiselyov, William E. Byrd, Daniel P. Friedman, and Chung-Chieh Shan. Pure, declarative, and constructive arithmetic relations. In Proceedings of the 9th International Conference on Functional and Logic Programming, FLOPS'08, pages 64–80, Berlin, Heidelberg, 2008. Springer-Verlag. ISBN 3540789685. URL https://dl.acm.org/doi/10.5555/1788446.1788456. The factorial of 5 through the Poorman’s Y has exactly one value, (int-val (0 0 0 1 1 1 1)), that is 120 (91 ms), and (* (int-exp ,a) (int-exp ,b)) run backwards to 6 gives the four factorisations (((1) (0 1 1)) ((0 1 1) (1)) ((0 1) (1 1)) ((1 1) (0 1))) (11 ms).

The first two quines are the literals #t and #f. The first five are the pearl’s five (3.0 to 3.4 s), all fixed points of Scheme’s eval: #3 is quinec, with an extra α ≠ int-val from the new absent° 'int-val, and #5 is the quine #4 of Section 5.2. #4 needs the new car primitive: it is quinec with 'quote computed as (car '(quote . β)). Reified, it adds α ≠ car to the clauses of #3, and the cdr β is junk, bound only by absent? of closure and int-val, as in the pearl (p. 11/17).

Of the pearl’s twelve programs that evaluate to 6 (p. 11/17), our run 15 (217 ms) gives the first eleven in the same order; the twelfth, ((λ () (sub1 (int-exp (1 1 1))))), arrives fifteenth, after (car (list (int-exp (0 1 1)) 'α)) and two others. The pearl’s run of 500 is not tested.

6Performance

workloadanswersb23f7e2 (ms)5773e7a (ms)91a5322 (ms)2d2fb4e (ms)d5b6f34 ¶ (ms)
step2: store-only scaling, n=400n = 400
A symbol° 400, bind one by one1270126826253
T absent° 400, bind one by one1210921177743
D ≠° w 400, bind one by one146424766
DAT 400, bind one by one1530850852971211
DAT 400 idle, 400 others bound127659287971177
step2: store-only scaling, n=1600n = 1600
A symbol° 1600, bind one by one1194750 †192676 †10491312
T absent° 1600, bind one by one1147781 †153483 †12261212
D ≠° w 1600, bind one by one16676406702626
DAT 1600, bind one by one1> 200 s ‡> 200 s ‡49184748
DAT 1600 idle, 1600 others bound1> 200 s ‡> 200 s ‡763031
bench.scm
S backwards-2000 search2000655652690639346
D perm6 ≠° full720185163170158158
D perm7 ≠° search504014601276128912471218
DA ≠°+symbol° 100 full186781636249
bench-big.scm
D all-distinct 100 vars, bind one by one118717118313378
DAT mixed 100 vars, bind one by one111041045102398
Table 8: CPU ms, best of 3, of the five builds: base, list-side fixes, keyed stores, records, and the later series of Section 3.6. † A single run. ‡ A single run, killed at the 200 s cutoff. ¶ Best of 6, in runs interleaved with a re-run of 2d2fb4e.

Table 8 times the four builds of Section 3 and d5b6f34 with CHICKEN 6 (rev 55615603) on an x86_64 Xeon Gold 6238R, benchmarks compiled with csc -O2 -d1, one process per row. Runs of different builds overlapped, so differences under about 10% are noise: S backwards-2000, which posts no constraint, spans 639–690 ms across the four builds. The answer counts agree across builds. The step2 rows post symbol° (A), (absent° 'z v) (T), (≠° w v) (D) or all three (DAT) on nn fresh variables, then bind them one by one; the idle rows bind nn other variables instead.

From n=400n=400 to n=1600n=1600 (×4 is linear, ×16 quadratic, ×64 cubic), A and T grow about ×70 on the list stores, cubic: each =° re-verifies the whole store, and that verification is itself quadratic on lists. The keyed stores make them quadratic (×16–17), since binding a marked variable still re-verifies everything, and the records linear (×2.6–3): symbol° 1600 goes from 195 s to 13 ms. D grows ×14–15, quadratic, at the same times, until 2d2fb4e (×4.3) re-posts only the disequalities in the bound variable’s record. DAT idle, whose constraints sit on variables that the bindings never touch, is the case the quiet =° was built for: 27.7 s become 11 ms, and the records keep that gain without the marks.

Up to 2d2fb4e the rows that change least are S backwards-2000, which posts no constraints, perm6 and perm7, whose D stores hold 15 and 21 disequalities, and D all-distinct 100, whose 4950 disequalities are inherent: the last three gain 15–29%, against factors of 100 and more elsewhere. perm6 and perm7 gain theirs at 5773e7a (−12%, as in Table 2), whose fold of μkanren-state-unify/assoc also speeds up the re-verification of touched prefixes; all-distinct gains at 2d2fb4e (187 to 133 ms), which re-posts only the bound variable’s prefixes.

The last column was timed later, interleaved with a re-run of 2d2fb4e that stayed within 2 ms or 2% of its column. The step2 rows move by 2 ms at most; S backwards-2000, DA, D all-distinct and DAT mixed drop to 0.21–0.59 of their times, as they meet the costs of Section 6.1.

Table 9: Interpreter queries of Section 5, ms, two runs per build (first/second), the eight queries in turn in one csi process (Section 5 runs each in a process of its own).

queryb23f7e22d2fb4ed5b6f34
quine 193/9382/8185/82
quine 101203/11781141/10651189/1073
twine 1919/919826/820822/822
twine 25287/47134760/46004766/4551
thrine 15956/53734434/57444784/5867
ext. quines 53245/31172298/28862532/2948
ext. “6” 15218/218182/194193/189
I love you 3077/7470/6972/74

On the relational interpreters (Table 9) b23f7e2 and 2d2fb4e print the same answers in the same order, as a hash of their write strings shows. The gain is only about 0–20%, and noisy (an interleaved re-run found almost none on quine 1 and thrine 1), because these queries keep few constraints per variable. d5b6f34 prints the same answers, with the same hashes, at the same times within that noise.

6.1Removing two quadratic costs outside the stores

Table 10: The occurs check, ms: walk° over a ground list of nn cells, a list of nn fresh cells built by fresh-list°, and that list walked. Best of 12 runs in four processes for d5b6f34, of 9 in three for 2d2fb4e, the processes of the two builds interleaved.

nnwalk groundbuildbuild, walk
2d2fb4e
1000844675
200032282959
400012371611835
d5b6f34
10008416
200017830
4000331763

Two costs that involve no store shaped the benchmarks of 2d2fb4e. Binding a variable to a compound value walked the whole value for the occurs check, about 0.15 µs per cons cell, so a walk° that takes a list apart with cons°, binding d to the rest at every step, was quadratic (Table 10). And ((andª f g) s) is (append-map§ g (δ (f s))), where append-map§ re-wraps a promise as (δ (append-map§ f (force $))): a chain of nn bindings nested to the left, as fold builds it, re-wrapped each inner promise once per enclosing bind, quadratic, where one nested to the right is linear: 4.4 s against 9 ms at n=4000n = 4000 (Table 11). Hence the step2 rows hold their variables in a Scheme list and nest to the right. d5b6f34 removes both costs where these benchmarks meet them.

Table 11: nn bindings in a left- or right-nested and° chain, ms; eq° wraps =° in a relation; timed as in Table 10.

nn=° left=° righteq° lefteq° right
2d2fb4e
100022123282
20001002415045
400044389664310
d5b6f34
10002232
20005556
400010101111

The bound M of Section 2.2 skips the occurs check when the variable being bound cannot occur in the value, and its window X keeps this linear whichever of the car and the cdr fresh° declares first: a walk over 4000 fresh cells takes 63 ms instead of 11.8 s. A chain of andª nested to the left runs as one append-map§/fold (Section 2.2), and left and right chains now cost the same (Table 11). Every andª pays for it, right-nested ones too: against the parent a0382e3, 0.16–0.18 µs in a tight loop and up to about 0.4 µs in place, so the step2 rows at n=6400n = 6400, chains of 12,800–25,600 goals, get 3–10% slower. Aggregation bodies of three goals, built for every state, make set° and groupby° 18% and 29% slower interpreted, and under 10% faster compiled.

Chains that andª cannot see, behind a relation or a fresh° around the left argument, stay quadratic: a left recursion through a relation takes 322, 1479 and 6479 ms at n=1000n = 1000, 20002000 and 40004000, as on 2d2fb4e. So do the answers behind a promise still pending when an earlier answer crosses the chain, which a disjunction of three goals or more early in a long chain is enough to cause: with an or° of three as the sixth of 4000 goals the chain takes 4.1 s, and 10 ms with an or° of two (12.9 s and 9.0 s on 2d2fb4e).

7Relation to the 2012 pearl and conclusions

Table 12 sets (aux kanren micro) at d5b6f34 against the miniKanren of the pearl’s App. DWilliam E. Byrd, Eric Holk, and Daniel P. Friedman. miniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming, Scheme '12, pages 8–29, New York, NY, USA, 2012. Association for Computing Machinery. ISBN 9781450318952. doi: 10.1145/2661103.2661105. URL https://doi.org/10.1145/2661103.2661105. The changed rows are representation, or generalisations that leave the pearl’s programs alone. Four list-level habits of App. D were fixed along the way (Section 3): the reversal of T in update-D/T+, unify*’s pair of lists built by map lhs and map rhs (p. 22/28), the ties of the Church–Rosser sort, and the whole-store verification of post-unify-≡. The seven commits after 12d318f (Section 3.6) change three rows.

Positioning. Beyond the microKanren coreJason Hemann and Daniel P. Friedman. microkanren: A Minimal Functional Core for Relational Programming, November 2013. URL https://shu.elsevierpure.com/en/publications/microkanren-a-minimal-functional-core-for-relational-programming/. Workshop on Scheme and Functional Programming ; Conference date: 13-11-2013 Through 13-11-2013 and the pearl’s constraints, (aux kanren micro) at d5b6f34 shares four ideas with faster-miniKanrenMichael Ballantyne and William E. Byrd. faster-miniKanren: A fast implementation of miniKanren with disequality and absento, 2015. URL https://github.com/michaelballantyne/faster-minikanren. Software, 2015--: a constraint record per variable; re-posting the record of a variable when it is bound, as faster-miniKanren’s update-constraints does; absento tags that may hold variables, walked whenever they are posted; and dropping at reification a disequality between variables of disjoint types, as d-subsumed-by-T? does. It differs in five ways. A disequality is stored once in P and listed under every variable it mentions, not copied onto the variables of its first pair (Section 2.3). An absento on a typed variable becomes ≠ at once (I3); faster-miniKanren keeps the absento term and drops it at reification. A compound that could still become a ground tag is watched whole in Tc, where faster-miniKanren posts =/= at every node, as (aux kanren micro) does, untyped variables aside, only for a tag with variables (I6). faster-miniKanren’s ext-s-check walks the value at every binding; (aux kanren micro) skips the walk when M shows that the variable cannot occur. And (aux kanren micro) has no scope-based destructive binding.faster-miniKanren also swaps the pearl’s store names (its record is (T D A), T for types) and keeps the pearl’s printed answer format, with =/=, sym/num and absento clauses. The arithmetic of aux.scm translates The Reasoned Schemer, second edition, while the pearl’s App. B follows Kiselyov et al.Daniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann. The Reasoned Schemer. The MIT Press, 2nd edition, 2018. ISBN 0262535513. URL https://dl.acm.org/doi/10.5555/3267158; and Oleg Kiselyov, William E. Byrd, Daniel P. Friedman, and Chung-Chieh Shan. Pure, declarative, and constructive arithmetic relations. In Proceedings of the 9th International Conference on Functional and Logic Programming, FLOPS'08, pages 64–80, Berlin, Heidelberg, 2008. Springer-Verlag. ISBN 3540789685. URL https://dl.acm.org/doi/10.5555/1788446.1788456

Future work. The four items that 12d318f left are done (Section 3.6). So is a bug they left: the body of set°, groupby° and enumerate° ran on the state before the from goal, so a fresh° in the body took again the indices of that goal’s fresh variables: with k bound by (fresh° (y) (=° k (list y))), the body (fresh° (z) (=° z 5) (=° q (list k z))) grounded to ((5) 5), not ((α) 5), up to 8046761; 5f406fd runs the body past the variables of the from goal. Two points are left. (i) The chains of Section 6.1 that andª cannot see, and the answers behind an early disjunction of three goals or more, are still quadratic. (ii) Disjointness of type tags is trusted, not checked: I3 and the reification of hidden variables rely on it, and only the two library tags (micro.scm:1020) drop the disequalities it implies, so string° keeps them, where faster-miniKanren’s three types are disjoint by construction. A checked declaration would let user tags drop them too.

Conclusion. (aux kanren micro) answers the pearl’s queries as the pearl does, with the same interpreters and the same quine, twines and thrines. Underneath, three lists, scanned or re-verified on every unification, became per-variable records, and a binding now costs in proportion to the constraints on the variables it binds. The order of the work mattered: an accidental order that reaches the answers must be fixed at its source first, or every optimisation has to reproduce it; answers that depend on the store contents alone then let the move to records be verified byte for byte.

References

  1. William E. Byrd, Eric Holk, and Daniel P. Friedman. miniKanren, live and untagged: quine generation via relational interpreters (programming pearl). In Proceedings of the 2012 Annual Workshop on Scheme and Functional Programming, Scheme '12, pages 8–29, New York, NY, USA, 2012. Association for Computing Machinery. ISBN 9781450318952. doi: 10.1145/2661103.2661105. URL https://doi.org/10.1145/2661103.2661105.
  2. Michael Ballantyne and William E. Byrd. faster-miniKanren: A fast implementation of miniKanren with disequality and absento, 2015. URL https://github.com/michaelballantyne/faster-minikanren. Software, 2015--.
  3. Jason Hemann and Daniel P. Friedman. microkanren: A Minimal Functional Core for Relational Programming, November 2013. URL https://shu.elsevierpure.com/en/publications/microkanren-a-minimal-functional-core-for-relational-programming/. Workshop on Scheme and Functional Programming ; Conference date: 13-11-2013 Through 13-11-2013.
  4. Daniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann. The Reasoned Schemer. The MIT Press, 2nd edition, 2018. ISBN 0262535513. URL https://dl.acm.org/doi/10.5555/3267158.
  5. Chris Okasaki. Purely functional random-access lists. In Proceedings of the Seventh International Conference on Functional Programming Languages and Computer Architecture, FPCA '95, pages 86–95, New York, NY, USA, 1995. Association for Computing Machinery. ISBN 0897917197. doi: 10.1145/224164.224187. URL https://doi.org/10.1145/224164.224187.
  6. Chris Okasaki. Purely Functional Data Structures. Cambridge University Press, 1998. URL https://www.cambridge.org/core/books/purely-functional-data-structures/0409255DA1B48FA731859AC72E34D494.
  7. Oleg Kiselyov, William E. Byrd, Daniel P. Friedman, and Chung-Chieh Shan. Pure, declarative, and constructive arithmetic relations. In Proceedings of the 9th International Conference on Functional and Logic Programming, FLOPS'08, pages 64–80, Berlin, Heidelberg, 2008. Springer-Verlag. ISBN 3540789685. URL https://dl.acm.org/doi/10.5555/1788446.1788456.