Skip to content

Commit fc57d04

Browse files
committed
srs: s2 component 3 Kanren core (vic: "Do 1, 2, 3 first."): s2/kanren.srs K.Term (Var, Con with a Sym functor, Int), substitution = Coll.IMap[K.Term] by variable (one O(log n) lookup per walk link; audit K1: lib/kanren walks an association list), walk, occurs, unify, bind, unifyAll (arity), reify. tests/s2/kanren.srs: the 8 cases of tests/kanren.srs with the same lines, plus a 100k-link variable chain bound to 42 (0.19 s, 120 MB) .out. mutants 5/5 killed (no occurs check -> crash, functor not compared, arity ignored, walk one link, self binding -> timeout). srs-test 173 ok 0 FAIL.
1 parent 525752c commit fc57d04

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

‎s2/kanren.srs‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// s2 component 3: Kanren core (vic 2026-11-06): terms, a substitution as a persistent map from variable to term
2+
// (Coll.IMap: one O(log n) lookup per link of a walk; the s1 lib walks an association list), unify with the occurs
3+
// check. a functor is a Sym: equal functors compare as integers.
4+
enum K.Term:
5+
case K.Var(n: I64)
6+
case K.Con(f: Sym.Sym, args: Coll.List[K.Term])
7+
case K.Int(v: I64)
8+
9+
// the term t stands for in s: a variable follows its bindings (a triangular substitution)
10+
def K.walk {r: Region} (s: Coll.IMap[K.Term], t: K.Term): K.Term = t match
11+
case K.Var(n) => Coll.lookup(s, n) match
12+
case Coll.None => t
13+
case Coll.Some(u) => K.walk(s, u)
14+
case K.Con(_, _) => t
15+
case K.Int(_) => t
16+
17+
// variable n occurs in t under s: binding n to t would build an infinite term
18+
def K.occurs {r: Region} (s: Coll.IMap[K.Term], n: I64, t: K.Term): Bool = K.walk(s, t) match
19+
case K.Var(m) => m == n
20+
case K.Con(_, args) => K.occursIn(s, n, args)
21+
case K.Int(_) => false
22+
23+
def K.occursIn {r: Region} (s: Coll.IMap[K.Term], n: I64, ts: Coll.List[K.Term]): Bool = ts match
24+
case Coll.Nil => false
25+
case Coll.Cons(h, t) => if K.occurs(s, n, h) then true else K.occursIn(s, n, t)
26+
27+
// s extended so a and b are equal, or None
28+
def K.unify {r: Region} (s: Coll.IMap[K.Term], a: K.Term, b: K.Term): Coll.Option[Coll.IMap[K.Term]] =
29+
val x = K.walk(s, a)
30+
val y = K.walk(s, b)
31+
x match
32+
case K.Var(n) => K.bind(s, n, y)
33+
case K.Con(f, xs) => y match
34+
case K.Var(m) => K.bind(s, m, x)
35+
case K.Con(g, ys) => if Sym.eq(f, g) then K.unifyAll(s, xs, ys) else Coll.None
36+
case K.Int(_) => Coll.None
37+
case K.Int(v) => y match
38+
case K.Var(m) => K.bind(s, m, x)
39+
case K.Int(w) => if v == w then Coll.Some(s) else Coll.None
40+
case K.Con(_, _) => Coll.None
41+
42+
def K.bind {r: Region} (s: Coll.IMap[K.Term], n: I64, t: K.Term): Coll.Option[Coll.IMap[K.Term]] = t match
43+
case K.Var(m) => if m == n then Coll.Some(s) else Coll.Some(Coll.insert(s, n, t))
44+
case K.Con(_, _) => if K.occurs(s, n, t) then Coll.None else Coll.Some(Coll.insert(s, n, t))
45+
case K.Int(_) => Coll.Some(Coll.insert(s, n, t))
46+
47+
// pairwise; lists of other lengths do not unify (arity)
48+
def K.unifyAll {r: Region} (s: Coll.IMap[K.Term], xs: Coll.List[K.Term], ys: Coll.List[K.Term]): Coll.Option[Coll.IMap[K.Term]] = xs match
49+
case Coll.Nil => ys match
50+
case Coll.Nil => Coll.Some(s)
51+
case Coll.Cons(_, _) => Coll.None
52+
case Coll.Cons(x, xt) => ys match
53+
case Coll.Nil => Coll.None
54+
case Coll.Cons(y, yt) => K.unify(s, x, y) match
55+
case Coll.None => Coll.None
56+
case Coll.Some(s2) => K.unifyAll(s2, xt, yt)
57+
58+
// t with every bound variable replaced, all the way down (an answer)
59+
def K.reify {r: Region} (s: Coll.IMap[K.Term], t: K.Term): K.Term = K.walk(s, t) match
60+
case K.Var(n) => K.Var(n)
61+
case K.Int(v) => K.Int(v)
62+
case K.Con(f, args) => K.Con(f, Coll.rev(K.reifyOnto(s, args, Coll.Nil)))
63+
64+
def K.reifyOnto {r: Region} (s: Coll.IMap[K.Term], ts: Coll.List[K.Term], acc: Coll.List[K.Term]): Coll.List[K.Term] = ts match
65+
case Coll.Nil => acc
66+
case Coll.Cons(h, t) => K.reifyOnto(s, t, Coll.Cons(K.reify(s, h), acc))

‎tests/s2/kanren.out‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
f(a, b)
2+
c
3+
_0
4+
fail
5+
fail
6+
fail
7+
fail
8+
fail
9+
42

‎tests/s2/kanren.srs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// s2 component 3 (Kanren core): the 8 cases of tests/kanren.srs (same .out lines), then a 100k-link variable chain
2+
// (each walk step is one map lookup)
3+
def T.c0 {r: Region} (f: Str): K.Term = K.Con(Sym.of(f), Coll.Nil)
4+
def T.c1 {r: Region} (f: Str, a: K.Term): K.Term = K.Con(Sym.of(f), Coll.Cons(a, Coll.Nil))
5+
def T.c2 {r: Region} (f: Str, a: K.Term, b: K.Term): K.Term = K.Con(Sym.of(f), Coll.Cons(a, Coll.Cons(b, Coll.Nil)))
6+
7+
def T.show(t: K.Term): Unit = t match
8+
case K.Var(n) =>
9+
rt_print("_")
10+
rt_print_i64(n)
11+
case K.Int(v) => rt_print_i64(v)
12+
case K.Con(f, args) =>
13+
rt_print(Sym.str(f))
14+
args match
15+
case Coll.Nil => ()
16+
case Coll.Cons(h, t) =>
17+
rt_print("(")
18+
T.show(h)
19+
T.showRest(t)
20+
rt_print(")")
21+
22+
def T.showRest(ts: Coll.List[K.Term]): Unit = ts match
23+
case Coll.Nil => ()
24+
case Coll.Cons(h, t) =>
25+
rt_print(", ")
26+
T.show(h)
27+
T.showRest(t)
28+
29+
def T.res {r: Region} (o: Coll.Option[Coll.IMap[K.Term]], t: K.Term): Unit = o match
30+
case Coll.None => rt_print("fail\n")
31+
case Coll.Some(s) =>
32+
T.show(K.reify(s, t))
33+
rt_print("\n")
34+
35+
def T.then {r: Region} (o: Coll.Option[Coll.IMap[K.Term]], a: K.Term, b: K.Term): Coll.Option[Coll.IMap[K.Term]] = o match
36+
case Coll.None => Coll.None
37+
case Coll.Some(s) => K.unify(s, a, b)
38+
39+
// _0 = _1, _1 = _2, ..., _(n-1) = _n
40+
def T.chain {r: Region} (s: Coll.IMap[K.Term], i: I64, n: I64): Coll.IMap[K.Term] =
41+
if i == n then s
42+
else K.unify(s, K.Var(i), K.Var(i + 1)) match
43+
case Coll.None => s
44+
case Coll.Some(s2) => T.chain(s2, i + 1, n)
45+
46+
def main(): I64 =
47+
handle r = Arena()
48+
val e: Coll.IMap[K.Term] = Coll.Tip
49+
val x = K.Var(0)
50+
val y = K.Var(1)
51+
T.res(K.unify(e, T.c2("f", x, T.c0("b")), T.c2("f", T.c0("a"), y)), T.c2("f", x, y))
52+
T.res(T.then(K.unify(e, x, y), y, T.c0("c")), x)
53+
T.res(K.unify(e, x, x), x)
54+
T.res(K.unify(e, x, T.c1("list", x)), x)
55+
T.res(T.then(K.unify(e, x, y), y, T.c1("list", x)), x)
56+
T.res(K.unify(e, T.c1("f", T.c0("a")), T.c1("g", T.c0("a"))), x)
57+
T.res(K.unify(e, T.c1("f", T.c0("a")), T.c2("f", T.c0("a"), T.c0("b"))), x)
58+
T.res(K.unify(e, K.Int(1), K.Int(2)), x)
59+
val s = T.chain(e, 0, 100000)
60+
T.res(K.unify(s, K.Var(100000), K.Int(42)), K.Var(0))
61+
0

0 commit comments

Comments
 (0)