-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprolog_engine.lua
More file actions
720 lines (670 loc) · 24.1 KB
/
Copy pathprolog_engine.lua
File metadata and controls
720 lines (670 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
-- prolog_engine.lua — native soa32 Prolog / type-inference substrate.
--
-- The execution engine that replaces the compiled-KL CPS Prolog machinery
-- (klambda/prolog.kl runtime + the t-star.kl driver's allocation profile).
-- Design validated by bench/wam_poc_v4.lua ("soa32"):
--
-- * Terms are PLAIN LUA NUMBERS, range-tagged:
-- atom id v < VAR_BASE (2^24)
-- var VAR_BASE <= v < CONS_BASE (2^25), idx = v - VAR_BASE
-- cons v >= CONS_BASE, pair p = v - CONS_BASE
-- Tag tests are `<` compares; payload extraction is subtraction. There are
-- deliberately NO bit operations and NO 64-bit cdata anywhere — int64
-- tag-packing was measured 2.2x slower (the v2 PoC trap).
-- * Storage is int32_t FFI arrays (cells / vbind / trail / capture buffer),
-- grown by doubling. vbind[idx] == -1 means unbound (terms are >= 0).
-- * Unification is ITERATIVE over explicit goal stacks with a trail;
-- failure unwinds bindings in a batch back to the caller's trail mark.
-- * Continuations are DEFUNCTIONALIZED: an integer handle indexing
-- contFn[h] (a statically-lifted Lua function) + contBase[h] (offset into
-- the int32 capture buffer). No freeze closures. Rare non-int32 captures
-- go to the cold contSpill[h] table.
-- * Choice points live in the LUA STACK FRAME of each predicate function:
-- plain-local marks (trail/var/cont tops) captured at entry, batch-unwound
-- at every alternative-try point. No heap choice-point objects.
-- * Cut is a 1:1 transcription of shen.cut/lock/unlock/unlocked?/fits?
-- (klambda/prolog.kl:80-92) over two scalars lock_open / lock_depth.
--
-- Success propagates as a non-false return value (like the legacy engine —
-- `return` passes the answer term up through the cont chain); failure is the
-- literal `false`.
--
-- This module is loaded by boot.lua unless SHEN_PROLOG_ENGINE=legacy. Until
-- the clause compiler (prolog_compile.lua) and the t-star port
-- (typecheck_native.lua) wire into it, install() is inert.
-- PUC Lua tier: without the FFI the soa32 substrate cannot exist. Return an
-- inert module — boot.lua's install() call becomes a no-op and the kernel
-- keeps the compiled-KL CPS Prolog engine (exactly SHEN_PROLOG_ENGINE=legacy).
local ok_ffi, ffi = pcall(require, "ffi")
if not ok_ffi then
return { install = function() end }
end
local R = require("runtime")
local loadstring = loadstring or load -- 5.2+ shim (module is FFI-gated anyway)
local M = {}
-- ---------------------------------------------------------------------------
-- tagging
-- ---------------------------------------------------------------------------
local VAR_BASE = 16777216 -- 2^24
local CONS_BASE = 33554432 -- 2^25
M.VAR_BASE, M.CONS_BASE = VAR_BASE, CONS_BASE
-- ---------------------------------------------------------------------------
-- arenas (int32 FFI arrays, grow-by-doubling)
-- ---------------------------------------------------------------------------
local function newarr(n) return ffi.new("int32_t[?]", n) end
local function grown(arr, cap, need)
local nc = cap
repeat nc = nc * 2 until nc >= need
local na = newarr(nc)
ffi.copy(na, arr, cap * 4)
return na, nc
end
local cells, ccap = newarr(65536), 65536 -- cons pairs: car at p, cdr at p+1
local vbind, vcap = newarr(16384), 16384 -- per-var binding; -1 = unbound
local trail, tcap = newarr(16384), 16384 -- bound var indices
local capbuf, kcap = newarr(16384), 16384 -- continuation captures
local gA, gB, gcap = newarr(4096), newarr(4096), 4096 -- unify goal stacks
local cell_top, var_top, trail_top, cap_top = 0, 0, 0, 0
-- continuation registry (handles are 1-based ints)
local contFn, contBase, contSpill = {}, {}, {}
local ch_top = 0
-- cut lock (transcribed from the legacy lock absvector {unlocked?, depth})
local lock_open, lock_depth = true, 0
-- inference counter (flushed to GLOBALS["shen.*infs*"] by the typecheck port)
local infs = 0
-- ---------------------------------------------------------------------------
-- atom interning
-- ---------------------------------------------------------------------------
local Symbol, Cons, Vmt, NIL = R.Symbol, R.Cons, R.Vmt, R.NIL
local shen_pvar = R.intern("shen.pvar")
local getmt = getmetatable
local atomval = { [0] = NIL, [1] = true, [2] = false }
local atom_top = 2
local symAtom, numAtom, strAtom = {}, {}, {}
local opqAtom = {} -- opaque objects, identity-interned
local opq_log, opq_top = {}, 0 -- interned-this-query log (epoch-cleared)
local function atom(x)
if x == NIL then return 0 end
local t = type(x)
local id
if t == "boolean" then
return x and 1 or 2
elseif t == "number" then
id = numAtom[x]
if id then return id end
atom_top = atom_top + 1; id = atom_top
if id >= VAR_BASE then error("prolog_engine: atom table overflow") end
numAtom[x] = id; atomval[id] = x
return id
elseif t == "string" then
id = strAtom[x]
if id then return id end
atom_top = atom_top + 1; id = atom_top
if id >= VAR_BASE then error("prolog_engine: atom table overflow") end
strAtom[x] = id; atomval[id] = x
return id
elseif getmt(x) == Symbol then
id = symAtom[x]
if id then return id end
atom_top = atom_top + 1; id = atom_top
if id >= VAR_BASE then error("prolog_engine: atom table overflow") end
symAtom[x] = id; atomval[id] = x
return id
else
-- opaque value (freshterm absvector, tuple, stream, closure, ...):
-- identity-interned, released at query end so a long session can't leak.
id = opqAtom[x]
if id then return id end
atom_top = atom_top + 1; id = atom_top
if id >= VAR_BASE then error("prolog_engine: atom table overflow") end
opqAtom[x] = id; atomval[id] = x
opq_top = opq_top + 1; opq_log[opq_top] = x
return id
end
end
M.atom = atom
function M.atomval(v) return atomval[v] end
-- ---------------------------------------------------------------------------
-- term construction / inspection
-- ---------------------------------------------------------------------------
local function newvar()
local idx = var_top
if idx >= vcap then vbind, vcap = grown(vbind, vcap, idx + 1) end
vbind[idx] = -1
var_top = idx + 1
return VAR_BASE + idx
end
M.newvar = newvar
-- LIFO-pop the most recent var: the translated (shen.gc B E) reclaim, paired
-- 1:1 with a preceding shen.newpv by the stpart emission structure
function M.popvar()
var_top = var_top - 1
end
local function mkcons(carv, cdrv)
local p = cell_top
if p + 2 > ccap then cells, ccap = grown(cells, ccap, p + 2) end
cells[p] = carv
cells[p + 1] = cdrv
cell_top = p + 2
return CONS_BASE + p
end
M.cons = mkcons
function M.car(v) return cells[v - CONS_BASE] end
function M.cdr(v) return cells[v - CONS_BASE + 1] end
function M.is_cons(v) return v >= CONS_BASE end
function M.is_var(v) return v >= VAR_BASE and v < CONS_BASE end
function M.is_atom(v) return v < VAR_BASE end
-- follow bound-var chains; stops at an unbound var or a non-var
local function lazyderef(v)
while v >= VAR_BASE and v < CONS_BASE do
local b = vbind[v - VAR_BASE]
if b >= 0 then v = b else break end
end
return v
end
M.lazyderef = lazyderef
-- ---------------------------------------------------------------------------
-- continuations
-- ---------------------------------------------------------------------------
-- Lifted continuation functions are called as fn(base, h): they read their
-- captures at fixed offsets capbuf[base + i] (and cold spills via
-- contSpill[h]). thaw accepts either a handle (number) or a plain Lua
-- function — call sites are monomorphic, so the type() branch is trace-cheap.
local function thawH(h)
if type(h) == "number" then
return contFn[h](contBase[h], h)
end
return h()
end
M.thawH = thawH
local function ckroom(n)
if cap_top + n > kcap then capbuf, kcap = grown(capbuf, kcap, cap_top + n) end
end
function M.newcont0(fn)
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = cap_top
return h
end
function M.newcont1(fn, a)
ckroom(1)
local b = cap_top; capbuf[b] = a; cap_top = b + 1
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont2(fn, a, a2)
ckroom(2)
local b = cap_top; capbuf[b] = a; capbuf[b+1] = a2; cap_top = b + 2
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont3(fn, a, a2, a3)
ckroom(3)
local b = cap_top; capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3
cap_top = b + 3
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont4(fn, a, a2, a3, a4)
ckroom(4)
local b = cap_top
capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3; capbuf[b+3] = a4
cap_top = b + 4
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont5(fn, a, a2, a3, a4, a5)
ckroom(5)
local b = cap_top
capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3; capbuf[b+3] = a4
capbuf[b+4] = a5
cap_top = b + 5
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont6(fn, a, a2, a3, a4, a5, a6)
ckroom(6)
local b = cap_top
capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3; capbuf[b+3] = a4
capbuf[b+4] = a5; capbuf[b+5] = a6
cap_top = b + 6
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont7(fn, a, a2, a3, a4, a5, a6, a7)
ckroom(7)
local b = cap_top
capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3; capbuf[b+3] = a4
capbuf[b+4] = a5; capbuf[b+5] = a6; capbuf[b+6] = a7
cap_top = b + 7
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
function M.newcont8(fn, a, a2, a3, a4, a5, a6, a7, a8)
ckroom(8)
local b = cap_top
capbuf[b] = a; capbuf[b+1] = a2; capbuf[b+2] = a3; capbuf[b+3] = a4
capbuf[b+4] = a5; capbuf[b+5] = a6; capbuf[b+6] = a7; capbuf[b+7] = a8
cap_top = b + 8
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
-- newcont9..newcont16 (generated): wide conjunctions in compiled clauses can
-- capture more than 8 live variables. Same monomorphic shape as 0-8.
for k = 9, 16 do
local args, stores = {}, {}
for i = 1, k do
args[i] = "x" .. i
stores[i] = "capbuf[b+" .. (i - 1) .. "] = x" .. i
end
local src = ([[
local ckroom, state = ...
return function(fn, %s)
ckroom(%d)
local capbuf, b = state.capbuf(), state.cap_top()
%s
return state.push(fn, b, %d)
end]]):format(table.concat(args, ", "), k, table.concat(stores, "; "), k)
-- state accessors keep the generated code valid across arena growth
local mk = assert(loadstring(src, "@newcont" .. k))
M["newcont" .. k] = mk(ckroom, {
capbuf = function() return capbuf end,
cap_top = function() return cap_top end,
push = function(fn, b, n)
cap_top = b + n
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end,
})
end
-- varargs fallback for very wide captures (>16): trace-unfriendly but cold —
-- only long conjunctions with many clause variables reach it, once per
-- clause-body entry
function M.newcontV(fn, ...)
local k = select("#", ...)
ckroom(k)
local b = cap_top
for i = 1, k do
capbuf[b + i - 1] = select(i, ...)
end
cap_top = b + k
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = b
return h
end
-- handle with a spill table for non-int32 captures (cold path)
function M.newcont_spill(fn, tbl)
local h = ch_top + 1; ch_top = h
contFn[h] = fn; contBase[h] = cap_top; contSpill[h] = tbl
return h
end
function M.spill(h) return contSpill[h] end
function M.capref(b, i) return capbuf[b + i] end
-- ---------------------------------------------------------------------------
-- marks / backtracking
-- ---------------------------------------------------------------------------
-- A choice point saves five tops: trail, var, cont-handle, capture-buffer,
-- cell. Cell reclaim at a failed alternative is safe: cells built after the
-- mark are only reachable through (a) bindings made after the mark — unwound
-- by the same undo — or (b) Lua locals of frames that are dead on the failure
-- path. Anything that must survive backtracking (findall solutions, returned
-- answers) is materialized OUT to Shen values first.
local function marks()
return trail_top, var_top, ch_top, cap_top, cell_top
end
M.marks = marks
local function undo(tm, vm, hm, bm, cm)
for i = trail_top - 1, tm, -1 do
vbind[trail[i]] = -1
end
trail_top = tm
var_top = vm
for h = ch_top, hm + 1, -1 do
contFn[h] = nil; contSpill[h] = nil
end
ch_top = hm
cap_top = bm
cell_top = cm
end
M.undo = undo
-- ---------------------------------------------------------------------------
-- unification
-- ---------------------------------------------------------------------------
-- occurs(idx, term): does var idx occur in (derefed) term? Iterative walk
-- using the gB stack scratch (safe: only called outside an active unify loop,
-- before any goal-stack use of the same depth region... NOT safe — use its
-- own small stack instead).
local ostk, ocap = newarr(1024), 1024
local function occurs(idx, v)
local n = 1
ostk[0] = v
while n > 0 do
n = n - 1
local x = lazyderef(ostk[n])
if x >= CONS_BASE then
local p = x - CONS_BASE
if n + 2 > ocap then ostk, ocap = grown(ostk, ocap, n + 2) end
ostk[n] = cells[p]; ostk[n + 1] = cells[p + 1]
n = n + 2
elseif x >= VAR_BASE then
if x - VAR_BASE == idx then return true end
end
end
return false
end
M.occurs = occurs
-- core unify loop. oc = occurs-check var bindings (lzy=! vs lzy=).
-- On structural success thaws cont; if the cont fails, bindings made HERE are
-- unwound (batch, to the entry trail mark) — semantically identical to the
-- legacy per-bind! unwind chain. Returns the cont's value, or false.
local function unify_core(rootA, rootB, cont, oc)
local n = 1
gA[0] = rootA; gB[0] = rootB
local tmark = trail_top
local tn = tmark
local ok = true
while n > 0 do
n = n - 1
local x = gA[n]
local y = gB[n]
-- inline lazyderef
while x >= VAR_BASE and x < CONS_BASE do
local b = vbind[x - VAR_BASE]
if b >= 0 then x = b else break end
end
while y >= VAR_BASE and y < CONS_BASE do
local b = vbind[y - VAR_BASE]
if b >= 0 then y = b else break end
end
if x == y then
-- identical atom / same var / same cons cell: success at this goal
elseif x >= VAR_BASE and x < CONS_BASE then
local idx = x - VAR_BASE
if oc and y >= CONS_BASE and occurs(idx, y) then ok = false; break end
vbind[idx] = y
if tn >= tcap then trail, tcap = grown(trail, tcap, tn + 1) end
trail[tn] = idx; tn = tn + 1
elseif y >= VAR_BASE and y < CONS_BASE then
local idx = y - VAR_BASE
if oc and x >= CONS_BASE and occurs(idx, x) then ok = false; break end
vbind[idx] = x
if tn >= tcap then trail, tcap = grown(trail, tcap, tn + 1) end
trail[tn] = idx; tn = tn + 1
elseif x >= CONS_BASE and y >= CONS_BASE then
local px, py = x - CONS_BASE, y - CONS_BASE
if n + 2 > gcap then
local oldcap = gcap
gA, gcap = grown(gA, oldcap, n + 2)
gB = (grown(gB, oldcap, n + 2))
end
gA[n] = cells[px + 1]; gB[n] = cells[py + 1]; n = n + 1
gA[n] = cells[px]; gB[n] = cells[py]; n = n + 1
else
ok = false
break
end
end
if ok then
trail_top = tn
local r = thawH(cont)
if r == false then
for i = tn - 1, tmark, -1 do vbind[trail[i]] = -1 end
trail_top = tmark
return false
end
return r
else
for i = tn - 1, tmark, -1 do vbind[trail[i]] = -1 end
-- trail_top was never advanced past tmark on the failure path
return false
end
end
function M.unify(a, b, cont) return unify_core(a, b, cont, false) end -- lzy=
function M.unify_oc(a, b, cont) return unify_core(a, b, cont, true) end -- lzy=!
-- shen.bind! equivalent: bind a (known-unbound) var, thaw, unwind on failure.
function M.bind1(varv, val, cont)
local idx = varv - VAR_BASE
vbind[idx] = val
if trail_top >= tcap then trail, tcap = grown(trail, tcap, trail_top + 1) end
trail[trail_top] = idx
trail_top = trail_top + 1
local r = thawH(cont)
if r == false then
vbind[idx] = -1
trail_top = trail_top - 1
return false
end
return r
end
-- ---------------------------------------------------------------------------
-- cut (transcribed from klambda/prolog.kl:80-92)
-- ---------------------------------------------------------------------------
function M.lock_is_open() return lock_open end
-- shen.cut: thaw; on failure with an open lock, lock at depth n.
function M.cut(n, cont)
local r = thawH(cont)
if r == false and lock_open then
lock_open = false
lock_depth = n
return false
end
return r
end
-- shen.unlock: re-open iff locked at exactly this depth; always "fails"
-- (it is the value of a fully-exhausted clause sequence).
function M.unlock(n)
if (not lock_open) and lock_depth == n then
lock_open = true
end
return false
end
-- ---------------------------------------------------------------------------
-- inference counter
-- ---------------------------------------------------------------------------
function M.incinfs() infs = infs + 1 end
function M.getinfs() return infs end
function M.setinfs(n) infs = n end
-- ---------------------------------------------------------------------------
-- Shen-value <-> arena-term boundary
-- ---------------------------------------------------------------------------
-- import: deep-convert a Shen value into an arena term. Legacy-format pvar
-- absvectors map to arena vars via `varmap` (keyed by pvar ticket), creating
-- fresh vars on first sight — callers that need to read bindings back out
-- keep the map. Proper-list spines are converted iteratively (no deep
-- recursion); only car nesting recurses.
local function import(x, varmap)
if getmt(x) == Cons then
-- walk the spine, then build cells back-to-front
local spine, sn = {}, 0
while getmt(x) == Cons do
sn = sn + 1; spine[sn] = x[1]
x = x[2]
end
local tail = import(x, varmap)
for i = sn, 1, -1 do
tail = mkcons(import(spine[i], varmap), tail)
end
return tail
elseif getmt(x) == Vmt and x[2] == shen_pvar then
local key = x[3]
local v = varmap and varmap[key]
if v then return v end
v = newvar()
if varmap then varmap[key] = v end
return v
else
return atom(x)
end
end
M.import = import
-- import_cached: identity-memoized import for values re-imported repeatedly
-- within one query (e.g. the shen.*datatypes* assoc list, passed to every
-- search-user-datatypes call). Sound because Shen list structure is immutable
-- and pvar imports go through the identity varmap (no allocation). The memo
-- is cleared at query_end (cells are reclaimed there).
local import_memo = {}
local identity_varmap = setmetatable({}, {
__index = function(_, idx) return VAR_BASE + idx end,
})
M.identity_varmap = identity_varmap
local function import_cached(x)
if type(x) == "table" then
local t = import_memo[x]
if t then return t end
t = M.import(x, identity_varmap)
import_memo[x] = t
return t
end
return M.import(x, identity_varmap)
end
M.import_cached = import_cached
-- maxinferences check, transcribed from shen.maxinfexceeded? (t-star.kl):
-- THROWS "maximum inferences exceeded" past the limit, else returns false
local GLOBALS, ERRFN
function M.maxinf_exceeded()
local mx = GLOBALS and GLOBALS["shen.*maxinferences*"]
if type(mx) == "number" and infs > mx then
ERRFN("maximum inferences exceeded")
end
return false
end
-- materialize: deep deref + export an arena term to a Shen value. Unbound
-- vars become legacy-format pvar absvectors {2, shen.pvar, idx} (cached per
-- idx — they are immutable, so sharing across calls is safe), preserving
-- legacy printing / equality / guard behavior.
local pvcache = {}
local function mat_pvar(idx)
local pv = pvcache[idx]
if not pv then
pv = setmetatable({ 2, shen_pvar, idx }, Vmt)
pvcache[idx] = pv
end
return pv
end
local function materialize(v)
v = lazyderef(v)
if v < VAR_BASE then
return atomval[v]
elseif v < CONS_BASE then
return mat_pvar(v - VAR_BASE)
else
-- iterative over the cdr spine; recursive on cars
local items, n = {}, 0
while true do
v = lazyderef(v)
if v >= CONS_BASE then
local p = v - CONS_BASE
n = n + 1; items[n] = materialize(cells[p])
v = cells[p + 1]
else
break
end
end
local tail
if v < VAR_BASE then tail = atomval[v] else tail = mat_pvar(v - VAR_BASE) end
for i = n, 1, -1 do
tail = R.cons(items[i], tail)
end
return tail
end
end
M.materialize = materialize
-- ---------------------------------------------------------------------------
-- goal builtins (legacy 6-arg goal ABI becomes (args..., n, cont))
-- ---------------------------------------------------------------------------
function M.g_when(test, n, cont) -- (when Test ...) prolog.kl:179
if test == true then return thawH(cont) end
return false
end
function M.g_var(v, n, cont) -- (var? X ...) prolog.kl:187
local d = lazyderef(v)
if d >= VAR_BASE and d < CONS_BASE then return thawH(cont) end
return false
end
function M.g_return(v) -- (return V ...) prolog.kl:177
return materialize(v)
end
-- is/is!/bind compile straight to M.unify / M.unify_oc / M.bind1.
-- fork / findall / call need the clause compiler's dispatch table and are
-- provided by prolog_compile.lua (Phase 2).
-- ---------------------------------------------------------------------------
-- query lifecycle
-- ---------------------------------------------------------------------------
-- Nested queries run above the current tops and fully unwind before the outer
-- resumes; query_begin/query_end save and restore every piece of engine state
-- a query can touch (including the cut lock and the opaque-intern epoch).
function M.query_begin()
local q = {
cell_top, var_top, trail_top, cap_top, ch_top,
lock_open, lock_depth, opq_top,
}
lock_open, lock_depth = true, 0
return q
end
function M.query_end(q)
-- unwind trail bindings made during the query
for i = trail_top - 1, q[3], -1 do
vbind[trail[i]] = -1
end
cell_top, var_top, trail_top, cap_top = q[1], q[2], q[3], q[4]
for h = ch_top, q[5] + 1, -1 do
contFn[h] = nil; contSpill[h] = nil
end
ch_top = q[5]
lock_open, lock_depth = q[6], q[7]
-- release opaque atoms interned during the query
for i = opq_top, q[8] + 1, -1 do
local obj = opq_log[i]
atomval[opqAtom[obj]] = nil
opqAtom[obj] = nil
opq_log[i] = nil
end
opq_top = q[8]
-- the import memo references cells that are no longer valid
if next(import_memo) ~= nil then
for k in pairs(import_memo) do import_memo[k] = nil end
end
end
-- full reset (tests / benchmarks only)
function M.reset_all()
cell_top, var_top, trail_top, cap_top, ch_top = 0, 0, 0, 0, 0
lock_open, lock_depth = true, 0
infs = 0
for h in pairs(contFn) do contFn[h] = nil end
for h in pairs(contSpill) do contSpill[h] = nil end
for k in pairs(import_memo) do import_memo[k] = nil end
end
-- expose tops read-only for tests/diagnostics
function M.tops()
return cell_top, var_top, trail_top, cap_top, ch_top
end
-- ---------------------------------------------------------------------------
-- predicate dispatch table (filled by prolog_compile.lua / typecheck_native)
-- ---------------------------------------------------------------------------
M.NativePred = {}
-- ---------------------------------------------------------------------------
-- install: wiring into the running kernel. Inert until prolog_compile.lua
-- (Phase 2) and typecheck_native.lua (Phase 3) register their overrides.
-- ---------------------------------------------------------------------------
function M.install(P)
M.P = P
GLOBALS = P.GLOBALS
ERRFN = P.ERR
for _, mod in ipairs({ "prolog_compile", "typecheck_native" }) do
local ok, m = pcall(require, mod)
if ok then
m.install(P, M)
elseif not tostring(m):find("module '" .. mod .. "' not found", 1, true) then
error(m)
end
end
end
return M