|
| 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)) |
0 commit comments