From 984dc7688c3809f8c2e8a659885919c683f40fa2 Mon Sep 17 00:00:00 2001 From: pyrex41 Date: Tue, 20 Oct 2026 23:21:41 -0500 Subject: [PATCH] fix(typecheck): follow the kernel's drivers, and fall back loudly when it diverges On ShenOSKernel 43.1 with the typecheck deref hoist, the translated shen.by-hypothesis called the new shen.by-hypothesis-h, which was not in the hard-coded DRIVER_FNS. NP's lazy lookup returned nil, and the call raised a plain Lua error mid-search. typecheck_dispatch rethrew everything but NATIVE_MISS, trap-error turned the Lua error into an ordinary Shen exception, and callers that trap typecheck errors (capcov's judgements, the fail-closed shen.typecheck Lua API) read it as "does not typecheck". Every typecheck failed and the process exited 0. - The driver set is now the closure of DRIVER_FNS under calls by name. The translator records each predicate a translated function calls, and any t-star.kl defun a driver calls is translated too. A call to anything that is neither a t-star.kl defun nor a hand port is reported before the first query. - Kernel conformance: the defuns the port reimplements rather than translates are fingerprinted (gensym-insensitive) against the versions it implements. These are the t-star hand ports and the prolog.kl primitives and builtin goals. The known set is 43.1 and 43.1+deref-hoist; scripts/kernel-fingerprints.lua regenerates it. - On any such problem, typechecks use the kernel's own KL typechecker, and stderr names the cause once. SHEN_TYPECHECK_NATIVE=require raises instead. - Call sites resolve through (NP[name] or MISS(name)). A predicate missing at run time raises a distinct sentinel, and the typecheck reruns the query on the KL engine, as it already did for untranslatable datatypes. g_call uses the same sentinel. test/kernel_conformance_spec.lua runs a typecheck battery on the vendored kernel, the deref-hoisted kernel (fixture, sha256 a0cedbb0...), and three deliberately mismatched kernels. Each run is compared with the kernel's KL typechecker. On the parent commit, 14 of its 23 checks fail: the hoisted kernel answers false to every typecheck. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 29 ++ prolog_compile.lua | 38 ++- prolog_engine.lua | 15 + scripts/kernel-fingerprints.lua | 47 +++ scripts/run-tests.lua | 1 + .../kernel-43.1-deref-hoist/README.md | 17 + .../kernel-43.1-deref-hoist/t-star.kl | 112 ++++++ test/kernel_conformance_spec.lua | 292 ++++++++++++++++ typecheck_native.lua | 319 +++++++++++++++++- 9 files changed, 842 insertions(+), 28 deletions(-) create mode 100644 scripts/kernel-fingerprints.lua create mode 100644 test/fixtures/kernel-43.1-deref-hoist/README.md create mode 100644 test/fixtures/kernel-43.1-deref-hoist/t-star.kl create mode 100644 test/kernel_conformance_spec.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 983d838..17ddb31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,35 @@ Notable changes to shen-lua. Release notes for earlier versions are under ### Fixed +- The native typechecker no longer returns wrong verdicts on a kernel whose + type-checker defuns differ from the ones it was written against. On + ShenOSKernel 43.1 with the typecheck deref hoist (shen-sources + `perf/typecheck-deref-hoist`), the translated `shen.by-hypothesis` called + the new `shen.by-hypothesis-h`, which had no native definition. The call + raised a plain Lua error. That error reached callers as an ordinary Shen + exception, which `trap-error` (and the Lua `shen.typecheck` API, which + fails closed) read as "does not typecheck". Every typecheck failed and + the process exited 0. Now: + - The translated drivers are the closure of the t-star roots under + "calls by name": any `t-star.kl` defun a driver calls is translated too, + so the hoisted kernel's `shen.by-hypothesis-h` runs natively. + - Before the first typecheck, every kernel defun the port reimplements + instead of translating is fingerprinted against the versions it + implements. That covers the hand-ported `shen.typecheck`, + `lookupsig`/`sigf`, `search-user-datatypes`(`-h`), `show` and + `maxinfexceeded?`, plus the `prolog.kl` primitives and builtin goals. + The fingerprint is gensym-insensitive; regenerate it with + `scripts/kernel-fingerprints.lua`. The set also checks that every driver + call resolves. + - A mismatch makes typechecks use the kernel's own KL typechecker, and + stderr names the defun once. A predicate that is missing at run time + raises a distinct sentinel, and the query is rerun on the KL engine. + - `SHEN_TYPECHECK_NATIVE=require` raises an error instead of falling back. + - `test/kernel_conformance_spec.lua` runs the native typechecker on the + vendored 43.1 and the deref-hoisted 43.1 (fixture under + `test/fixtures/`) against the kernel's KL typechecker. It also runs it + on deliberately mismatched kernels. + - Deep non-tail recursion no longer overflows the Lua stack, in user code or in the kernel's own tree walkers. `(depth 12000)` for a plain `(+ 1 (depth (- N 1)))` recursion failed with `stack overflow` (it reached diff --git a/prolog_compile.lua b/prolog_compile.lua index 7b52bf1..1865416 100644 --- a/prolog_compile.lua +++ b/prolog_compile.lua @@ -167,6 +167,16 @@ local BUILTIN_GOALS = { ["call"] = true, } +-- A call to another predicate by name. Records the callee (the driver +-- closure in typecheck_native.lua follows these) and resolves through MISS so +-- a name with no native predicate raises E.native_miss rather than calling +-- nil. +local function np_ref(ctx, nm) + ctx.callees[nm] = true + local q = string.format("%q", nm) + return "(NP[" .. q .. "] or MISS(" .. q .. "))" +end + local function refuse(ctx, why) ctx.failed = why error(ctx.fail, 0) @@ -742,8 +752,8 @@ compile_value = function(ctx, e, env, out, d) for _, l in ipairs(gb) do out[#out + 1] = l end local gname = sname(g[1]) if BUILTIN_GOALS[gname] then refuse(ctx, "builtin inside fork") end - out[#out + 1] = indent(d) .. guard .. r .. " = NP[" .. - string.format("%q", gname) .. "](" .. + out[#out + 1] = indent(d) .. guard .. r .. " = " .. + np_ref(ctx, gname) .. "(" .. table.concat(parts, ", ") .. (gn > 0 and ", " or "") .. nk .. ", " .. kv .. ")" .. (first and "" or " end") first = false @@ -774,7 +784,7 @@ compile_value = function(ctx, e, env, out, d) local k = compile_cont(ctx, a[an], env, out, d) parts[#parts + 1] = n parts[#parts + 1] = k - return "NP[" .. string.format("%q", nm) .. "](" .. table.concat(parts, ", ") .. ")" + return np_ref(ctx, nm) .. "(" .. table.concat(parts, ", ") .. ")" end refuse(ctx, "unsupported form") @@ -1002,6 +1012,7 @@ local CONS, CAR, CDR, LZD = E.cons, E.car, E.cdr, E.lazyderef local NEWVAR, POPVAR = E.newvar, E.popvar local UNIFY, UNIFYOC, BIND1 = E.unify, E.unify_oc, E.bind1 local EQTERM = E.eqterm +local MISS = E.native_miss local CUT, UNLOCK, LOCKOPEN = E.cut, E.unlock, E.lock_is_open local INCINFS, THAW, THAWN, CAPREF = E.incinfs, E.thawH, E.thawN, E.capref local GVAR, GCALL, GFINDALL = E.g_var, E.g_call, E.g_findall @@ -1030,7 +1041,7 @@ local function translate_once(name, allparams, body, mode) allparams[np - 1], allparams[np] local ctx = setmetatable({ - buf = {}, nlift = 0, nlocal = 0, fail = {}, liftnames = {}, + buf = {}, nlift = 0, nlocal = 0, fail = {}, liftnames = {}, callees = {}, fnloc = nparams + 2, split = mode and true or false, wide = mode == "wide", }, CtxMT) @@ -1081,7 +1092,7 @@ local function translate_once(name, allparams, body, mode) return nil, "luagen: " .. tostring(err) .. "\n" .. source, nil, err end local fn = chunk(E, NP, F, E.materialize, E.import_cached) - return fn, nil, source + return fn, nil, source, nil, ctx.callees end -- Let-chain splitting (see compile_value) costs a call per split point, so @@ -1097,15 +1108,16 @@ local function width_error(lerr) or lerr:find("too many lifted functions", 1, true) ~= nil end +-- Returns fn, err, src, callees (the set of predicate names fn calls). local function translate_core(name, allparams, body) - local fn, err, src, lerr = translate_once(name, allparams, body, false) + local fn, err, src, lerr, callees = translate_once(name, allparams, body, false) if lerr and tostring(lerr):find("local variables", 1, true) then - fn, err, src, lerr = translate_once(name, allparams, body, "split") + fn, err, src, lerr, callees = translate_once(name, allparams, body, "split") end if width_error(lerr) then - fn, err, src = translate_once(name, allparams, body, "wide") + fn, err, src, lerr, callees = translate_once(name, allparams, body, "wide") end - return fn, err, src + return fn, err, src, callees end M.translate_core = translate_core @@ -1131,8 +1143,8 @@ function M.translate_defun(defunForm) local name = sname(parts[2]) local allparams = lst2tbl(parts[3]) local body = parts[4] - local fn, err, src = translate_core(name, allparams, body) - return fn, err, src, name + local fn, err, src, callees = translate_core(name, allparams, body) + return fn, err, src, name, callees end -- --------------------------------------------------------------------------- @@ -1268,9 +1280,7 @@ local function install_dispatch_builtins() error("native call: goal head is not a symbol") end local fn = NP[hv.name] - if not fn then - error("native call: no native predicate " .. hv.name) - end + if not fn then E.native_miss(hv.name) end local args, an = {}, 0 local rest = E.lazyderef(E.cdr(d)) while rest >= E.CONS_BASE do diff --git a/prolog_engine.lua b/prolog_engine.lua index cd68427..16275c3 100644 --- a/prolog_engine.lua +++ b/prolog_engine.lua @@ -870,6 +870,21 @@ end -- --------------------------------------------------------------------------- M.NativePred = {} +-- A translated predicate calls another by name through NativePred. A name +-- with no native predicate used to be `nil(...)`: a plain Lua error that +-- trap-error turns into an ordinary Shen exception, so a typecheck that hit +-- it simply looked like a failed typecheck (wrong answer, exit 0). Call +-- sites now resolve through MISS, which raises this distinct sentinel; the +-- typecheck entry recognises it and reruns the whole query on the kernel's +-- KL engine (typecheck_native.lua). +local MissMT = { __tostring = function(m) + return "native prolog: no native predicate " .. tostring(m.name) +end } +function M.native_miss(name) + error(setmetatable({ name = name }, MissMT), 0) +end +function M.is_native_miss(x) return getmetatable(x) == MissMT end + -- --------------------------------------------------------------------------- -- install: wiring into the running kernel. Inert until prolog_compile.lua -- (Phase 2) and typecheck_native.lua (Phase 3) register their overrides. diff --git a/scripts/kernel-fingerprints.lua b/scripts/kernel-fingerprints.lua new file mode 100644 index 0000000..7dabdfd --- /dev/null +++ b/scripts/kernel-fingerprints.lua @@ -0,0 +1,47 @@ +-- scripts/kernel-fingerprints.lua — regenerate typecheck_native.lua's KNOWN +-- table: the fingerprints of the kernel defuns the native typechecker +-- reimplements (typecheck_native.lua REIMPLEMENTED). +-- +-- luajit scripts/kernel-fingerprints.lua LABEL KLDIR [LABEL KLDIR ...] +-- +-- Prints the table body for every kernel given. Add a kernel ONLY after +-- checking that the native code implements each of its listed defuns: a +-- fingerprint in KNOWN is a claim that the port runs that defun's semantics. +local root = (arg[0]:gsub("scripts/[^/]*$", "")) +package.path = (root ~= "" and root or "./") .. "?.lua;" .. package.path +local TN = require("typecheck_native") + +if #arg < 2 or #arg % 2 ~= 0 then + io.stderr:write("usage: kernel-fingerprints.lua LABEL KLDIR [LABEL KLDIR ...]\n") + os.exit(2) +end + +local function slurp(path) + local f = assert(io.open(path, "r")) + local s = f:read("*a"); f:close() + return s +end + +local seen = {} -- name -> { fp -> {labels} }, fp order +for i = 1, #arg, 2 do + local label, dir = arg[i], arg[i + 1] + local defsets = {} + for _, e in ipairs(TN.REIMPLEMENTED) do + local file = e[1] + defsets[file] = defsets[file] or TN.defuns_of(slurp(dir .. "/" .. file .. ".kl")) + local fp = TN.fingerprint_in(defsets[file], e[2]) + local byname = seen[e[2]] + if not byname then byname = { order = {} }; seen[e[2]] = byname end + if not byname[fp] then byname[fp] = {}; byname.order[#byname.order + 1] = fp end + table.insert(byname[fp], label) + end +end + +for _, e in ipairs(TN.REIMPLEMENTED) do + local byname = seen[e[2]] + local parts = {} + for _, fp in ipairs(byname.order) do + parts[#parts + 1] = string.format('["%s"] = "%s"', fp, table.concat(byname[fp], ", ")) + end + io.write(string.format(' [%q] = { %s },\n', e[2], table.concat(parts, ", "))) +end diff --git a/scripts/run-tests.lua b/scripts/run-tests.lua index 9f1de01..a62f75f 100644 --- a/scripts/run-tests.lua +++ b/scripts/run-tests.lua @@ -42,6 +42,7 @@ local specs = { "test/interop_spec.lua", "test/io_spec.lua", "test/jit_spec.lua", + "test/kernel_conformance_spec.lua", "test/library_spec.lua", "test/numeric_literal_spec.lua", "test/numeric_render_spec.lua", diff --git a/test/fixtures/kernel-43.1-deref-hoist/README.md b/test/fixtures/kernel-43.1-deref-hoist/README.md new file mode 100644 index 0000000..344894f --- /dev/null +++ b/test/fixtures/kernel-43.1-deref-hoist/README.md @@ -0,0 +1,17 @@ +# ShenOSKernel 43.1 with the typecheck deref hoist + +`t-star.kl` is ShenOSKernel 43.1's `klambda/t-star.kl` with one kernel source +change applied: `perf(t-star): deref the checked term once, outside the +datatype and hypothesis loops` (shen-sources branch +`perf/typecheck-deref-hoist`, commit +`55964453e8a687ba2c9d107dbaebb23ef72996a9`, not yet upstream). + +- SHA-256: `a0cedbb0334eb7f66fe5e2296056ea66d1e2f42c9efe176f6d1be2f0b28bf2ec` +- `shen.search-user-datatypes` and `shen.by-hypothesis` become plain defuns + that dereference their term arguments once and call the new Prolog + predicates `shen.search-user-datatypes-h` and `shen.by-hypothesis-h`. +- Every other defun is unchanged except for gensym renumbering. + +`test/kernel_conformance_spec.lua` assembles a kernel directory from the +vendored `klambda/` with this file in place of `t-star.kl` and checks that the +native typechecker runs it (and agrees with the kernel's KL typechecker). diff --git a/test/fixtures/kernel-43.1-deref-hoist/t-star.kl b/test/fixtures/kernel-43.1-deref-hoist/t-star.kl new file mode 100644 index 0000000..2e719e2 --- /dev/null +++ b/test/fixtures/kernel-43.1-deref-hoist/t-star.kl @@ -0,0 +1,112 @@ +(defun shen.typecheck (V4901 V4902) (let W4903 (shen.extract-vars V4902) (let W4904 (shen.rectify-type V4902) (let W4905 (shen.curry V4901) (((((lambda Z4906 (lambda Z4907 (lambda Z4908 (lambda Z4909 (let W4910 (shen.newpv Z4906) (shen.gc Z4906 (do (shen.incinfs) (shen.insert-prolog-variables (receive (shen.deref W4903 Z4906)) (receive (shen.deref W4904 Z4906)) W4910 Z4906 Z4907 Z4908 (freeze (shen.toplevel-forms (receive (shen.deref W4905 Z4906)) W4910 Z4906 Z4907 Z4908 (freeze (return W4910 Z4906 Z4907 Z4908 Z4909)))))))))))) (shen.prolog-vector)) (@v true (@v 0 (vector 0)))) 0) (freeze true)))))) + +(defun shen.insert-prolog-variables (V4911 V4912 V4913 V4914 V4915 V4916 V4917) (let W4918 (if (shen.unlocked? V4915) (let W4919 (shen.lazyderef V4911 V4914) (if (= W4919 ()) (do (shen.incinfs) (is! V4912 V4913 V4914 V4915 V4916 V4917)) false)) false) (if (= W4918 false) (if (shen.unlocked? V4915) (let W4920 (shen.lazyderef V4911 V4914) (if (cons? W4920) (let W4921 (hd W4920) (let W4922 (tl W4920) (let W4923 (shen.newpv V4914) (shen.gc V4914 (do (shen.incinfs) (shen.insert-prolog-variables W4922 (subst (shen.deref W4923 V4914) W4921 V4912) V4913 V4914 V4915 V4916 V4917)))))) false)) false) W4918))) + +(defun shen.toplevel-forms (V4924 V4925 V4926 V4927 V4928 V4929) (let W4930 (+ V4928 1) (let W4931 (if (shen.unlocked? V4927) (let W4932 (shen.lazyderef V4924 V4926) (if (cons? W4932) (let W4933 (shen.lazyderef (hd W4932) V4926) (if (= W4933 define) (let W4934 (shen.lazyderef (tl W4932) V4926) (if (cons? W4934) (let W4935 (hd W4934) (let W4936 (tl W4934) (do (shen.incinfs) (when (shen.type-theory-enabled?) V4926 V4927 W4930 (freeze (shen.cut V4926 V4927 W4930 (freeze (shen.signal-def (value shen.*spy*) W4935 V4926 V4927 W4930 (freeze (shen.t* (cons define (cons W4935 W4936)) V4925 V4926 V4927 W4930 V4929)))))))))) false)) false)) false)) false) (if (= W4931 false) (let W4937 (if (shen.unlocked? V4927) (do (shen.incinfs) (shen.system-S (cons V4924 (cons (intern ":") (cons V4925 ()))) () V4926 V4927 W4930 V4929)) false) (if (= W4937 false) (shen.unlock V4927 W4930) W4937)) W4931)))) + +(defun shen.signal-def (V4938 V4939 V4940 V4941 V4942 V4943) (let W4944 (if (shen.unlocked? V4941) (let W4945 (shen.lazyderef V4938 V4940) (if (= W4945 false) (do (shen.incinfs) (thaw V4943)) false)) false) (if (= W4944 false) (if (shen.unlocked? V4941) (let W4946 (shen.lazyderef V4938 V4940) (if (= W4946 true) (let W4947 (shen.newpv V4940) (shen.gc V4940 (do (shen.incinfs) (is W4947 (pr (cn " +typechecking (fn " (shen.app (shen.deref V4939 V4940) ") +" shen.a)) (stoutput)) V4940 V4941 V4942 V4943)))) false)) false) W4944))) + +(defun shen.rectify-type (V4948) (shen.demodulate (shen.curry-type V4948))) + +(defun shen.demodulate (V4949) (trap-error (let W4950 (shen.walk (lambda Z4951 (shen.demod Z4951)) V4949) (if (= W4950 V4949) V4949 (shen.demodulate W4950))) (lambda Z4952 V4949))) + +(defun shen.curry-type (V4953) (cond ((and (cons? V4953) (and (cons? (tl V4953)) (and (= --> (hd (tl V4953))) (and (cons? (tl (tl V4953))) (and (cons? (tl (tl (tl V4953)))) (= --> (hd (tl (tl (tl V4953)))))))))) (shen.curry-type (cons (hd V4953) (cons --> (cons (tl (tl V4953)) ()))))) ((and (cons? V4953) (and (cons? (hd V4953)) (and (= list (hd (hd V4953))) (and (cons? (tl (hd V4953))) (and (= () (tl (tl (hd V4953)))) (and (cons? (tl V4953)) (and (= ==> (hd (tl V4953))) (and (cons? (tl (tl V4953))) (= () (tl (tl (tl V4953)))))))))))) (shen.curry-type (cons (hd V4953) (cons --> (cons (cons str (cons (hd V4953) (tl (tl V4953)))) ()))))) ((and (cons? V4953) (and (cons? (tl V4953)) (and (= * (hd (tl V4953))) (and (cons? (tl (tl V4953))) (and (cons? (tl (tl (tl V4953)))) (= * (hd (tl (tl (tl V4953)))))))))) (shen.curry-type (cons (hd V4953) (cons * (cons (tl (tl V4953)) ()))))) ((cons? V4953) (map (lambda Z4954 (shen.curry-type Z4954)) V4953)) (true V4953))) + +(defun shen.curry (V4955) (cond ((and (cons? V4955) (and (= define (hd V4955)) (cons? (tl V4955)))) V4955) ((and (cons? V4955) (and (= type (hd V4955)) (and (cons? (tl V4955)) (and (cons? (tl (tl V4955))) (= () (tl (tl (tl V4955)))))))) (cons type (cons (shen.curry (hd (tl V4955))) (tl (tl V4955))))) ((and (cons? V4955) (shen.special? (hd V4955))) (cons (hd V4955) (map (lambda Z4956 (shen.curry Z4956)) (tl V4955)))) ((and (cons? V4955) (shen.extraspecial? (hd V4955))) V4955) ((and (cons? V4955) (and (cons? (tl V4955)) (cons? (tl (tl V4955))))) (shen.curry (cons (cons (hd V4955) (cons (hd (tl V4955)) ())) (tl (tl V4955))))) ((and (cons? V4955) (and (cons? (tl V4955)) (= () (tl (tl V4955))))) (cons (shen.curry (hd V4955)) (cons (shen.curry (hd (tl V4955))) ()))) (true V4955))) + +(defun shen.special? (V4957) (element? V4957 (value shen.*special*))) + +(defun shen.extraspecial? (V4958) (element? V4958 (value shen.*extraspecial*))) + +(defun shen.system-S (V4959 V4960 V4961 V4962 V4963 V4964) (let W4965 (+ V4963 1) (let W4966 (if (shen.unlocked? V4962) (do (shen.incinfs) (when (shen.maxinfexceeded?) V4961 V4962 W4965 V4964)) false) (if (= W4966 false) (let W4967 (if (shen.unlocked? V4962) (let W4968 (shen.lazyderef V4959 V4961) (if (cons? W4968) (let W4969 (hd W4968) (let W4970 (shen.lazyderef (tl W4968) V4961) (if (cons? W4970) (let W4971 (hd W4970) (let W4972 (shen.lazyderef (tl W4970) V4961) (if (cons? W4972) (let W4973 (hd W4972) (let W4974 (shen.lazyderef (tl W4972) V4961) (if (= W4974 ()) (do (shen.incinfs) (when (= (shen.deref W4971 V4961) (intern ":")) V4961 V4962 W4965 (freeze (when (shen.type-theory-enabled?) V4961 V4962 W4965 (freeze (shen.cut V4961 V4962 W4965 (freeze (shen.system-S-h W4969 W4973 V4960 V4961 V4962 W4965 V4964)))))))) false))) false))) false))) false)) false) (if (= W4967 false) (let W4975 (if (shen.unlocked? V4962) (do (shen.incinfs) (when (value shen.*spy*) V4961 V4962 W4965 (freeze (shen.show V4959 V4960 V4961 V4962 W4965 V4964)))) false) (if (= W4975 false) (let W4976 (if (shen.unlocked? V4962) (do (shen.incinfs) (shen.search-user-datatypes V4959 V4960 (value shen.*datatypes*) V4961 V4962 W4965 V4964)) false) (if (= W4976 false) (shen.unlock V4962 W4965) W4976)) W4975)) W4967)) W4966)))) + +(defun shen.show (V4983 V4984 V4985 V4986 V4987 V4988) (do (shen.line) (do (shen.show-p (shen.deref V4983 V4985)) (do (nl 2) (do (shen.show-assumptions (shen.deref V4984 V4985) 1) (do (shen.pause-for-user) false)))))) + +(defun shen.line () (let W4989 (inferences) (pr (cn "____________________________________________________________ " (shen.app W4989 (cn " inference" (shen.app (if (= 1 W4989) "" "s") " +?- " shen.a)) shen.a)) (stoutput)))) + +(defun shen.show-p (V4990) (cond ((and (cons? V4990) (and (cons? (tl V4990)) (and (cons? (tl (tl V4990))) (and (= () (tl (tl (tl V4990)))) (= (hd (tl V4990)) (intern ":")))))) (do (shen.prterm (hd V4990)) (do (pr " : " (stoutput)) (pr (shen.app (hd (tl (tl V4990))) "" shen.r) (stoutput))))) (true (shen.prterm V4990)))) + +(defun shen.prterm (V4991) (cond ((and (cons? V4991) (and (= cons (hd V4991)) (and (cons? (tl V4991)) (and (cons? (tl (tl V4991))) (= () (tl (tl (tl V4991)))))))) (do (pr "[" (stoutput)) (do (shen.prterm (hd (tl V4991))) (do (shen.prtl (hd (tl (tl V4991)))) (pr "]" (stoutput)))))) ((cons? V4991) (do (pr "(" (stoutput)) (do (shen.prterm (hd V4991)) (do (map (lambda Z4992 (do (pr " " (stoutput)) (shen.prterm Z4992))) (tl V4991)) (pr ")" (stoutput)))))) (true (print V4991)))) + +(defun shen.prtl (V4993) (cond ((= () V4993) "") ((and (cons? V4993) (and (= cons (hd V4993)) (and (cons? (tl V4993)) (and (cons? (tl (tl V4993))) (= () (tl (tl (tl V4993)))))))) (do (pr " " (stoutput)) (do (shen.prterm (hd (tl V4993))) (shen.prtl (hd (tl (tl V4993))))))) (true (do (pr " | " (stoutput)) (shen.prterm V4993))))) + +(defun shen.show-assumptions (V5000 V5001) (cond ((= () V5000) (pr " +> " (stoutput))) ((cons? V5000) (do (pr (shen.app V5001 ". " shen.a) (stoutput)) (do (shen.show-p (hd V5000)) (do (nl 1) (shen.show-assumptions (tl V5000) (+ V5001 1)))))) (true (simple-error "implementation error in shen.show-assumptions")))) + +(defun shen.pause-for-user () (let W5002 (read-byte (stinput)) (if (= W5002 94) (simple-error "input aborted +") (nl 1)))) + +(defun shen.type-theory-enabled? () (value shen.*shen-type-theory-enabled?*)) + +(defun shen.maxinfexceeded? () (if (> (inferences) (value shen.*maxinferences*)) (simple-error "maximum inferences exceeded") false)) + +(defun shen.system-S-h (V5003 V5004 V5005 V5006 V5007 V5008 V5009) (let W5010 (+ V5008 1) (let W5011 (if (shen.unlocked? V5007) (do (shen.incinfs) (when (value shen.*spy*) V5006 V5007 W5010 (freeze (shen.show (cons V5003 (cons (intern ":") (cons V5004 ()))) V5005 V5006 V5007 W5010 V5009)))) false) (if (= W5011 false) (let W5012 (if (shen.unlocked? V5007) (do (shen.incinfs) (when (not (cons? (shen.lazyderef V5003 V5006))) V5006 V5007 W5010 (freeze (shen.primitive V5003 V5004 V5006 V5007 W5010 V5009)))) false) (if (= W5012 false) (let W5013 (if (shen.unlocked? V5007) (do (shen.incinfs) (shen.by-hypothesis V5003 V5004 V5005 V5006 V5007 W5010 V5009)) false) (if (= W5013 false) (let W5014 (if (shen.unlocked? V5007) (let W5015 (shen.lazyderef V5003 V5006) (if (cons? W5015) (let W5016 (hd W5015) (let W5017 (shen.lazyderef (tl W5015) V5006) (if (= W5017 ()) (do (shen.incinfs) (shen.lookupsig W5016 (cons --> (cons V5004 ())) V5006 V5007 W5010 V5009)) false))) false)) false) (if (= W5014 false) (let W5018 (if (shen.unlocked? V5007) (let W5019 (shen.lazyderef V5003 V5006) (if (cons? W5019) (let W5020 (shen.lazyderef (hd W5019) V5006) (if (= W5020 fn) (let W5021 (shen.lazyderef (tl W5019) V5006) (if (cons? W5021) (let W5022 (hd W5021) (let W5023 (shen.lazyderef (tl W5021) V5006) (if (= W5023 ()) (do (shen.incinfs) (when (= (arity (shen.deref W5022 V5006)) 0) V5006 V5007 W5010 (freeze (shen.cut V5006 V5007 W5010 (freeze (shen.system-S-h (cons W5022 ()) V5004 V5005 V5006 V5007 W5010 V5009)))))) false))) false)) false)) false)) false) (if (= W5018 false) (let W5024 (if (shen.unlocked? V5007) (let W5025 (shen.lazyderef V5003 V5006) (if (cons? W5025) (let W5026 (shen.lazyderef (hd W5025) V5006) (if (= W5026 fn) (let W5027 (shen.lazyderef (tl W5025) V5006) (if (cons? W5027) (let W5028 (hd W5027) (let W5029 (shen.lazyderef (tl W5027) V5006) (if (= W5029 ()) (do (shen.incinfs) (shen.lookupsig W5028 V5004 V5006 V5007 W5010 V5009)) false))) false)) false)) false)) false) (if (= W5024 false) (let W5030 (if (shen.unlocked? V5007) (let W5031 (shen.lazyderef V5003 V5006) (if (cons? W5031) (let W5032 (hd W5031) (let W5033 (shen.lazyderef (tl W5031) V5006) (if (cons? W5033) (let W5034 (hd W5033) (let W5035 (shen.lazyderef (tl W5033) V5006) (if (= W5035 ()) (let W5036 (shen.newpv V5006) (shen.gc V5006 (do (shen.incinfs) (when (not (cons? (shen.lazyderef W5032 V5006))) V5006 V5007 W5010 (freeze (shen.lookupsig W5032 (cons W5036 (cons --> (cons V5004 ()))) V5006 V5007 W5010 (freeze (shen.system-S-h W5034 W5036 V5005 V5006 V5007 W5010 V5009)))))))) false))) false))) false)) false) (if (= W5030 false) (let W5037 (if (shen.unlocked? V5007) (let W5038 (shen.lazyderef V5003 V5006) (if (cons? W5038) (let W5039 (hd W5038) (let W5040 (shen.lazyderef (tl W5038) V5006) (if (cons? W5040) (let W5041 (hd W5040) (let W5042 (shen.lazyderef (tl W5040) V5006) (if (= W5042 ()) (let W5043 (shen.newpv V5006) (shen.gc V5006 (do (shen.incinfs) (shen.system-S-h W5039 (cons W5043 (cons --> (cons V5004 ()))) V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5041 W5043 V5005 V5006 V5007 W5010 V5009)))))) false))) false))) false)) false) (if (= W5037 false) (let W5044 (if (shen.unlocked? V5007) (let W5045 (shen.lazyderef V5003 V5006) (if (cons? W5045) (let W5046 (shen.lazyderef (hd W5045) V5006) (if (= W5046 cons) (let W5047 (shen.lazyderef (tl W5045) V5006) (if (cons? W5047) (let W5048 (hd W5047) (let W5049 (shen.lazyderef (tl W5047) V5006) (if (cons? W5049) (let W5050 (hd W5049) (let W5051 (shen.lazyderef (tl W5049) V5006) (if (= W5051 ()) (let W5052 (shen.lazyderef V5004 V5006) (let W5053 (lambda Z5054 (do (shen.incinfs) (shen.system-S-h W5048 Z5054 V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5050 (cons list (cons Z5054 ())) V5005 V5006 V5007 W5010 V5009))))) (if (cons? W5052) (let W5055 (shen.lazyderef (hd W5052) V5006) (let W5056 (freeze (let W5057 (shen.lazyderef (tl W5052) V5006) (let W5058 (lambda Z5059 (W5053 Z5059)) (if (cons? W5057) (let W5060 (hd W5057) (let W5061 (shen.lazyderef (tl W5057) V5006) (let W5062 (freeze (W5058 W5060)) (if (= W5061 ()) (thaw W5062) (if (shen.pvar? W5061) (shen.bind! W5061 () V5006 W5062) false))))) (if (shen.pvar? W5057) (let W5063 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5057 (cons W5063 ()) V5006 (freeze (W5058 W5063))))) false))))) (if (= W5055 list) (thaw W5056) (if (shen.pvar? W5055) (shen.bind! W5055 list V5006 W5056) false)))) (if (shen.pvar? W5052) (let W5064 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5052 (cons list (cons W5064 ())) V5006 (freeze (W5053 W5064))))) false)))) false))) false))) false)) false)) false)) false) (if (= W5044 false) (let W5065 (if (shen.unlocked? V5007) (let W5066 (shen.lazyderef V5003 V5006) (if (cons? W5066) (let W5067 (shen.lazyderef (hd W5066) V5006) (if (= W5067 @p) (let W5068 (shen.lazyderef (tl W5066) V5006) (if (cons? W5068) (let W5069 (hd W5068) (let W5070 (shen.lazyderef (tl W5068) V5006) (if (cons? W5070) (let W5071 (hd W5070) (let W5072 (shen.lazyderef (tl W5070) V5006) (if (= W5072 ()) (let W5073 (shen.lazyderef V5004 V5006) (let W5074 (lambda Z5075 (lambda Z5076 (do (shen.incinfs) (shen.system-S-h W5069 Z5075 V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5071 Z5076 V5005 V5006 V5007 W5010 V5009)))))) (if (cons? W5073) (let W5077 (hd W5073) (let W5078 (shen.lazyderef (tl W5073) V5006) (let W5079 (lambda Z5080 ((W5074 W5077) Z5080)) (if (cons? W5078) (let W5081 (shen.lazyderef (hd W5078) V5006) (let W5082 (freeze (let W5083 (shen.lazyderef (tl W5078) V5006) (let W5084 (lambda Z5085 (W5079 Z5085)) (if (cons? W5083) (let W5086 (hd W5083) (let W5087 (shen.lazyderef (tl W5083) V5006) (let W5088 (freeze (W5084 W5086)) (if (= W5087 ()) (thaw W5088) (if (shen.pvar? W5087) (shen.bind! W5087 () V5006 W5088) false))))) (if (shen.pvar? W5083) (let W5089 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5083 (cons W5089 ()) V5006 (freeze (W5084 W5089))))) false))))) (if (= W5081 *) (thaw W5082) (if (shen.pvar? W5081) (shen.bind! W5081 * V5006 W5082) false)))) (if (shen.pvar? W5078) (let W5090 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5078 (cons * (cons W5090 ())) V5006 (freeze (W5079 W5090))))) false))))) (if (shen.pvar? W5073) (let W5091 (shen.newpv V5006) (shen.gc V5006 (let W5092 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5073 (cons W5091 (cons * (cons W5092 ()))) V5006 (freeze ((W5074 W5091) W5092))))))) false)))) false))) false))) false)) false)) false)) false) (if (= W5065 false) (let W5093 (if (shen.unlocked? V5007) (let W5094 (shen.lazyderef V5003 V5006) (if (cons? W5094) (let W5095 (shen.lazyderef (hd W5094) V5006) (if (= W5095 @v) (let W5096 (shen.lazyderef (tl W5094) V5006) (if (cons? W5096) (let W5097 (hd W5096) (let W5098 (shen.lazyderef (tl W5096) V5006) (if (cons? W5098) (let W5099 (hd W5098) (let W5100 (shen.lazyderef (tl W5098) V5006) (if (= W5100 ()) (let W5101 (shen.lazyderef V5004 V5006) (let W5102 (lambda Z5103 (do (shen.incinfs) (shen.system-S-h W5097 Z5103 V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5099 (cons vector (cons Z5103 ())) V5005 V5006 V5007 W5010 V5009))))) (if (cons? W5101) (let W5104 (shen.lazyderef (hd W5101) V5006) (let W5105 (freeze (let W5106 (shen.lazyderef (tl W5101) V5006) (let W5107 (lambda Z5108 (W5102 Z5108)) (if (cons? W5106) (let W5109 (hd W5106) (let W5110 (shen.lazyderef (tl W5106) V5006) (let W5111 (freeze (W5107 W5109)) (if (= W5110 ()) (thaw W5111) (if (shen.pvar? W5110) (shen.bind! W5110 () V5006 W5111) false))))) (if (shen.pvar? W5106) (let W5112 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5106 (cons W5112 ()) V5006 (freeze (W5107 W5112))))) false))))) (if (= W5104 vector) (thaw W5105) (if (shen.pvar? W5104) (shen.bind! W5104 vector V5006 W5105) false)))) (if (shen.pvar? W5101) (let W5113 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5101 (cons vector (cons W5113 ())) V5006 (freeze (W5102 W5113))))) false)))) false))) false))) false)) false)) false)) false) (if (= W5093 false) (let W5114 (if (shen.unlocked? V5007) (let W5115 (shen.lazyderef V5003 V5006) (if (cons? W5115) (let W5116 (shen.lazyderef (hd W5115) V5006) (if (= W5116 @s) (let W5117 (shen.lazyderef (tl W5115) V5006) (if (cons? W5117) (let W5118 (hd W5117) (let W5119 (shen.lazyderef (tl W5117) V5006) (if (cons? W5119) (let W5120 (hd W5119) (let W5121 (shen.lazyderef (tl W5119) V5006) (if (= W5121 ()) (let W5122 (shen.lazyderef V5004 V5006) (let W5123 (freeze (do (shen.incinfs) (shen.system-S-h W5118 string V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5120 string V5005 V5006 V5007 W5010 V5009))))) (if (= W5122 string) (thaw W5123) (if (shen.pvar? W5122) (shen.bind! W5122 string V5006 W5123) false)))) false))) false))) false)) false)) false)) false) (if (= W5114 false) (let W5124 (if (shen.unlocked? V5007) (let W5125 (shen.lazyderef V5003 V5006) (if (cons? W5125) (let W5126 (shen.lazyderef (hd W5125) V5006) (if (= W5126 lambda) (let W5127 (shen.lazyderef (tl W5125) V5006) (if (cons? W5127) (let W5128 (hd W5127) (let W5129 (shen.lazyderef (tl W5127) V5006) (if (cons? W5129) (let W5130 (hd W5129) (let W5131 (shen.lazyderef (tl W5129) V5006) (if (= W5131 ()) (let W5132 (shen.lazyderef V5004 V5006) (let W5133 (lambda Z5134 (lambda Z5135 (let W5136 (shen.newpv V5006) (shen.gc V5006 (let W5137 (shen.newpv V5006) (shen.gc V5006 (do (shen.incinfs) (bind W5137 (shen.freshterm (shen.lazyderef W5128 V5006)) V5006 V5007 W5010 (freeze (bind W5136 (shen.beta (shen.lazyderef W5128 V5006) (shen.deref W5137 V5006) (shen.deref W5130 V5006)) V5006 V5007 W5010 (freeze (shen.system-S-h W5136 Z5135 (cons (cons W5137 (cons (intern ":") (cons Z5134 ()))) V5005) V5006 V5007 W5010 V5009)))))))))))) (if (cons? W5132) (let W5138 (hd W5132) (let W5139 (shen.lazyderef (tl W5132) V5006) (let W5140 (lambda Z5141 ((W5133 W5138) Z5141)) (if (cons? W5139) (let W5142 (shen.lazyderef (hd W5139) V5006) (let W5143 (freeze (let W5144 (shen.lazyderef (tl W5139) V5006) (let W5145 (lambda Z5146 (W5140 Z5146)) (if (cons? W5144) (let W5147 (hd W5144) (let W5148 (shen.lazyderef (tl W5144) V5006) (let W5149 (freeze (W5145 W5147)) (if (= W5148 ()) (thaw W5149) (if (shen.pvar? W5148) (shen.bind! W5148 () V5006 W5149) false))))) (if (shen.pvar? W5144) (let W5150 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5144 (cons W5150 ()) V5006 (freeze (W5145 W5150))))) false))))) (if (= W5142 -->) (thaw W5143) (if (shen.pvar? W5142) (shen.bind! W5142 --> V5006 W5143) false)))) (if (shen.pvar? W5139) (let W5151 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5139 (cons --> (cons W5151 ())) V5006 (freeze (W5140 W5151))))) false))))) (if (shen.pvar? W5132) (let W5152 (shen.newpv V5006) (shen.gc V5006 (let W5153 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5132 (cons W5152 (cons --> (cons W5153 ()))) V5006 (freeze ((W5133 W5152) W5153))))))) false)))) false))) false))) false)) false)) false)) false) (if (= W5124 false) (let W5154 (if (shen.unlocked? V5007) (let W5155 (shen.lazyderef V5003 V5006) (if (cons? W5155) (let W5156 (shen.lazyderef (hd W5155) V5006) (if (= W5156 let) (let W5157 (shen.lazyderef (tl W5155) V5006) (if (cons? W5157) (let W5158 (hd W5157) (let W5159 (shen.lazyderef (tl W5157) V5006) (if (cons? W5159) (let W5160 (hd W5159) (let W5161 (shen.lazyderef (tl W5159) V5006) (if (cons? W5161) (let W5162 (hd W5161) (let W5163 (shen.lazyderef (tl W5161) V5006) (if (= W5163 ()) (let W5164 (shen.newpv V5006) (shen.gc V5006 (let W5165 (shen.newpv V5006) (shen.gc V5006 (let W5166 (shen.newpv V5006) (shen.gc V5006 (do (shen.incinfs) (shen.system-S-h W5160 W5166 V5005 V5006 V5007 W5010 (freeze (bind W5165 (shen.freshterm (shen.lazyderef W5158 V5006)) V5006 V5007 W5010 (freeze (bind W5164 (shen.beta (shen.lazyderef W5158 V5006) (shen.lazyderef W5165 V5006) (shen.lazyderef W5162 V5006)) V5006 V5007 W5010 (freeze (shen.system-S-h W5164 V5004 (cons (cons W5165 (cons (intern ":") (cons W5166 ()))) V5005) V5006 V5007 W5010 V5009)))))))))))))) false))) false))) false))) false)) false)) false)) false) (if (= W5154 false) (let W5167 (if (shen.unlocked? V5007) (let W5168 (shen.lazyderef V5003 V5006) (if (cons? W5168) (let W5169 (shen.lazyderef (hd W5168) V5006) (if (= W5169 open) (let W5170 (shen.lazyderef (tl W5168) V5006) (if (cons? W5170) (let W5171 (hd W5170) (let W5172 (shen.lazyderef (tl W5170) V5006) (if (cons? W5172) (let W5173 (hd W5172) (let W5174 (shen.lazyderef (tl W5172) V5006) (if (= W5174 ()) (let W5175 (shen.lazyderef V5004 V5006) (let W5176 (lambda Z5177 (do (shen.incinfs) (is! W5173 Z5177 V5006 V5007 W5010 (freeze (when (element? (shen.lazyderef Z5177 V5006) (cons in (cons out ()))) V5006 V5007 W5010 (freeze (shen.system-S-h W5171 string V5005 V5006 V5007 W5010 V5009))))))) (if (cons? W5175) (let W5178 (shen.lazyderef (hd W5175) V5006) (let W5179 (freeze (let W5180 (shen.lazyderef (tl W5175) V5006) (let W5181 (lambda Z5182 (W5176 Z5182)) (if (cons? W5180) (let W5183 (hd W5180) (let W5184 (shen.lazyderef (tl W5180) V5006) (let W5185 (freeze (W5181 W5183)) (if (= W5184 ()) (thaw W5185) (if (shen.pvar? W5184) (shen.bind! W5184 () V5006 W5185) false))))) (if (shen.pvar? W5180) (let W5186 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5180 (cons W5186 ()) V5006 (freeze (W5181 W5186))))) false))))) (if (= W5178 stream) (thaw W5179) (if (shen.pvar? W5178) (shen.bind! W5178 stream V5006 W5179) false)))) (if (shen.pvar? W5175) (let W5187 (shen.newpv V5006) (shen.gc V5006 (shen.bind! W5175 (cons stream (cons W5187 ())) V5006 (freeze (W5176 W5187))))) false)))) false))) false))) false)) false)) false)) false) (if (= W5167 false) (let W5188 (if (shen.unlocked? V5007) (let W5189 (shen.lazyderef V5003 V5006) (if (cons? W5189) (let W5190 (shen.lazyderef (hd W5189) V5006) (if (= W5190 type) (let W5191 (shen.lazyderef (tl W5189) V5006) (if (cons? W5191) (let W5192 (hd W5191) (let W5193 (shen.lazyderef (tl W5191) V5006) (if (cons? W5193) (let W5194 (hd W5193) (let W5195 (shen.lazyderef (tl W5193) V5006) (if (= W5195 ()) (do (shen.incinfs) (shen.cut V5006 V5007 W5010 (freeze (is! (shen.rectify-type (shen.deref W5194 V5006)) V5004 V5006 V5007 W5010 (freeze (shen.system-S-h W5192 V5004 V5005 V5006 V5007 W5010 V5009)))))) false))) false))) false)) false)) false)) false) (if (= W5188 false) (let W5196 (if (shen.unlocked? V5007) (let W5197 (shen.lazyderef V5003 V5006) (if (cons? W5197) (let W5198 (shen.lazyderef (hd W5197) V5006) (if (= W5198 shen.input-h+) (let W5199 (shen.lazyderef (tl W5197) V5006) (if (cons? W5199) (let W5200 (hd W5199) (let W5201 (shen.lazyderef (tl W5199) V5006) (if (cons? W5201) (let W5202 (hd W5201) (let W5203 (shen.lazyderef (tl W5201) V5006) (if (= W5203 ()) (do (shen.incinfs) (is V5004 (shen.rectify-type (shen.rdecons (shen.deref W5200 V5006))) V5006 V5007 W5010 (freeze (shen.system-S-h W5202 (cons stream (cons in ())) V5005 V5006 V5007 W5010 V5009)))) false))) false))) false)) false)) false)) false) (if (= W5196 false) (let W5204 (if (shen.unlocked? V5007) (let W5205 (shen.lazyderef V5003 V5006) (if (cons? W5205) (let W5206 (shen.lazyderef (hd W5205) V5006) (if (= W5206 set) (let W5207 (shen.lazyderef (tl W5205) V5006) (if (cons? W5207) (let W5208 (hd W5207) (let W5209 (shen.lazyderef (tl W5207) V5006) (if (cons? W5209) (let W5210 (hd W5209) (let W5211 (shen.lazyderef (tl W5209) V5006) (if (= W5211 ()) (do (shen.incinfs) (shen.system-S-h W5208 symbol V5005 V5006 V5007 W5010 (freeze (shen.system-S-h (cons value (cons W5208 ())) V5004 V5005 V5006 V5007 W5010 (freeze (shen.system-S-h W5210 V5004 V5005 V5006 V5007 W5010 V5009)))))) false))) false))) false)) false)) false)) false) (if (= W5204 false) (let W5212 (if (shen.unlocked? V5007) (let W5213 (shen.newpv V5006) (shen.gc V5006 (do (shen.incinfs) (shen.l-rules V5005 W5213 false V5006 V5007 W5010 (freeze (shen.cut V5006 V5007 W5010 (freeze (shen.system-S-h V5003 V5004 W5213 V5006 V5007 W5010 V5009)))))))) false) (if (= W5212 false) (let W5214 (if (shen.unlocked? V5007) (do (shen.incinfs) (shen.search-user-datatypes (cons V5003 (cons (intern ":") (cons V5004 ()))) V5005 (value shen.*datatypes*) V5006 V5007 W5010 V5009)) false) (if (= W5214 false) (shen.unlock V5007 W5010) W5214)) W5212)) W5204)) W5196)) W5188)) W5167)) W5154)) W5124)) W5114)) W5093)) W5065)) W5044)) W5037)) W5030)) W5024)) W5018)) W5014)) W5013)) W5012)) W5011)))) + +(defun shen.rdecons (V5215) (cond ((and (cons? V5215) (and (= cons (hd V5215)) (and (cons? (tl V5215)) (and (cons? (tl (tl V5215))) (= () (tl (tl (tl V5215)))))))) (cons (shen.rdecons (hd (tl V5215))) (shen.rdecons (hd (tl (tl V5215)))))) ((cons? V5215) (cons (shen.rdecons (hd V5215)) (shen.rdecons (tl V5215)))) (true V5215))) + +(defun shen.primitive (V5216 V5217 V5218 V5219 V5220 V5221) (let W5222 (if (shen.unlocked? V5219) (let W5223 (shen.lazyderef V5217 V5218) (let W5224 (freeze (do (shen.incinfs) (when (number? (shen.lazyderef V5216 V5218)) V5218 V5219 V5220 V5221))) (if (= W5223 number) (thaw W5224) (if (shen.pvar? W5223) (shen.bind! W5223 number V5218 W5224) false)))) false) (if (= W5222 false) (let W5225 (if (shen.unlocked? V5219) (let W5226 (shen.lazyderef V5217 V5218) (let W5227 (freeze (do (shen.incinfs) (when (boolean? (shen.lazyderef V5216 V5218)) V5218 V5219 V5220 V5221))) (if (= W5226 boolean) (thaw W5227) (if (shen.pvar? W5226) (shen.bind! W5226 boolean V5218 W5227) false)))) false) (if (= W5225 false) (let W5228 (if (shen.unlocked? V5219) (let W5229 (shen.lazyderef V5217 V5218) (let W5230 (freeze (do (shen.incinfs) (when (string? (shen.lazyderef V5216 V5218)) V5218 V5219 V5220 V5221))) (if (= W5229 string) (thaw W5230) (if (shen.pvar? W5229) (shen.bind! W5229 string V5218 W5230) false)))) false) (if (= W5228 false) (let W5231 (if (shen.unlocked? V5219) (let W5232 (shen.lazyderef V5217 V5218) (let W5233 (freeze (do (shen.incinfs) (when (symbol? (shen.lazyderef V5216 V5218)) V5218 V5219 V5220 V5221))) (if (= W5232 symbol) (thaw W5233) (if (shen.pvar? W5232) (shen.bind! W5232 symbol V5218 W5233) false)))) false) (if (= W5231 false) (if (shen.unlocked? V5219) (let W5234 (shen.lazyderef V5216 V5218) (if (= W5234 ()) (let W5235 (shen.lazyderef V5217 V5218) (let W5236 (lambda Z5237 (do (shen.incinfs) (thaw V5221))) (if (cons? W5235) (let W5238 (shen.lazyderef (hd W5235) V5218) (let W5239 (freeze (let W5240 (shen.lazyderef (tl W5235) V5218) (let W5241 (lambda Z5242 (W5236 Z5242)) (if (cons? W5240) (let W5243 (hd W5240) (let W5244 (shen.lazyderef (tl W5240) V5218) (let W5245 (freeze (W5241 W5243)) (if (= W5244 ()) (thaw W5245) (if (shen.pvar? W5244) (shen.bind! W5244 () V5218 W5245) false))))) (if (shen.pvar? W5240) (let W5246 (shen.newpv V5218) (shen.gc V5218 (shen.bind! W5240 (cons W5246 ()) V5218 (freeze (W5241 W5246))))) false))))) (if (= W5238 list) (thaw W5239) (if (shen.pvar? W5238) (shen.bind! W5238 list V5218 W5239) false)))) (if (shen.pvar? W5235) (let W5247 (shen.newpv V5218) (shen.gc V5218 (shen.bind! W5235 (cons list (cons W5247 ())) V5218 (freeze (W5236 W5247))))) false)))) false)) false) W5231)) W5228)) W5225)) W5222))) + +(defun shen.by-hypothesis (V5261 V5262 V5263 V5264 V5265 V5266 V5267) (shen.by-hypothesis-h (shen.deref V5261 V5264) V5262 V5263 V5264 V5265 V5266 V5267)) + +(defun shen.by-hypothesis-h (V5268 V5269 V5270 V5271 V5272 V5273 V5274) (let W5275 (if (shen.unlocked? V5272) (let W5276 (shen.lazyderef V5270 V5271) (if (cons? W5276) (let W5277 (shen.lazyderef (hd W5276) V5271) (if (cons? W5277) (let W5278 (hd W5277) (let W5279 (shen.lazyderef (tl W5277) V5271) (if (cons? W5279) (let W5280 (hd W5279) (let W5281 (shen.lazyderef (tl W5279) V5271) (if (cons? W5281) (let W5282 (hd W5281) (let W5283 (shen.lazyderef (tl W5281) V5271) (if (= W5283 ()) (do (shen.incinfs) (when (= (shen.deref W5280 V5271) (intern ":")) V5271 V5272 V5273 (freeze (when (= V5268 (shen.deref W5278 V5271)) V5271 V5272 V5273 (freeze (is! V5269 W5282 V5271 V5272 V5273 V5274)))))) false))) false))) false))) false)) false)) false) (if (= W5275 false) (if (shen.unlocked? V5272) (let W5284 (shen.lazyderef V5270 V5271) (if (cons? W5284) (let W5285 (tl W5284) (do (shen.incinfs) (shen.by-hypothesis-h V5268 V5269 W5285 V5271 V5272 V5273 V5274))) false)) false) W5275))) + +(defun shen.lookupsig (V5266 V5267 V5268 V5269 V5270 V5271) (if (shen.unlocked? V5269) (do (shen.incinfs) (shen.sigf (assoc V5266 (value shen.*sigf*)) V5267 V5268 V5269 V5270 V5271)) false)) + +(defun shen.sigf (V5286 V5287 V5288 V5289 V5290 V5291) (cond ((cons? V5286) ((((((tl V5286) V5287) V5288) V5289) V5290) V5291)) (true false))) + +(defun shen.freshterm (V5292) (let W5293 (absvector 3) (let W5294 (address-> W5293 0 shen.print-freshterm) (let W5295 (address-> W5294 1 V5292) (let W5296 (address-> W5295 2 (set shen.*gensym* (+ 1 (value shen.*gensym*)))) W5296))))) + +(defun shen.print-freshterm (V5297) (cn "&&" (shen.app (<-address V5297 1) "" shen.a))) + +(defun shen.search-user-datatypes (V5318 V5319 V5320 V5321 V5322 V5323 V5324) (shen.search-user-datatypes-h (shen.deref V5318 V5321) (shen.deref V5319 V5321) V5320 V5321 V5322 V5323 V5324)) + +(defun shen.search-user-datatypes-h (V5325 V5326 V5327 V5328 V5329 V5330 V5331) (let W5332 (if (shen.unlocked? V5329) (let W5333 (shen.lazyderef V5327 V5328) (if (cons? W5333) (let W5334 (shen.lazyderef (hd W5333) V5328) (if (cons? W5334) (let W5335 (tl W5334) (do (shen.incinfs) (call (((shen.deref W5335 V5328) V5325) V5326) V5328 V5329 V5330 V5331))) false)) false)) false) (if (= W5332 false) (if (shen.unlocked? V5329) (let W5336 (shen.lazyderef V5327 V5328) (if (cons? W5336) (let W5337 (tl W5336) (do (shen.incinfs) (shen.search-user-datatypes-h V5325 V5326 W5337 V5328 V5329 V5330 V5331))) false)) false) W5332))) + +(defun shen.l-rules (V5311 V5312 V5313 V5314 V5315 V5316 V5317) (let W5318 (+ V5316 1) (let W5319 (if (shen.unlocked? V5315) (let W5320 (shen.lazyderef V5311 V5314) (if (= W5320 ()) (let W5321 (shen.lazyderef V5313 V5314) (if (= W5321 true) (do (shen.incinfs) (shen.cut V5314 V5315 W5318 (freeze (bind V5312 () V5314 V5315 W5318 V5317)))) false)) false)) false) (if (= W5319 false) (let W5322 (if (shen.unlocked? V5315) (let W5323 (shen.lazyderef V5311 V5314) (if (cons? W5323) (let W5324 (shen.lazyderef (hd W5323) V5314) (if (cons? W5324) (let W5325 (shen.lazyderef (hd W5324) V5314) (if (cons? W5325) (let W5326 (shen.lazyderef (hd W5325) V5314) (if (= W5326 cons) (let W5327 (shen.lazyderef (tl W5325) V5314) (if (cons? W5327) (let W5328 (hd W5327) (let W5329 (shen.lazyderef (tl W5327) V5314) (if (cons? W5329) (let W5330 (hd W5329) (let W5331 (shen.lazyderef (tl W5329) V5314) (if (= W5331 ()) (let W5332 (shen.lazyderef (tl W5324) V5314) (if (cons? W5332) (let W5333 (hd W5332) (let W5334 (shen.lazyderef (tl W5332) V5314) (if (cons? W5334) (let W5335 (shen.lazyderef (hd W5334) V5314) (if (cons? W5335) (let W5336 (shen.lazyderef (hd W5335) V5314) (if (= W5336 list) (let W5337 (shen.lazyderef (tl W5335) V5314) (if (cons? W5337) (let W5338 (hd W5337) (let W5339 (shen.lazyderef (tl W5337) V5314) (if (= W5339 ()) (let W5340 (shen.lazyderef (tl W5334) V5314) (if (= W5340 ()) (let W5341 (tl W5323) (do (shen.incinfs) (when (= (shen.deref W5333 V5314) (intern ":")) V5314 V5315 W5318 (freeze (shen.cut V5314 V5315 W5318 (freeze (shen.l-rules (cons (cons W5328 (cons W5333 (cons W5338 ()))) (cons (cons W5330 (cons W5333 (cons (cons list (cons W5338 ())) ()))) W5341)) V5312 true V5314 V5315 W5318 V5317))))))) false)) false))) false)) false)) false)) false))) false)) false))) false))) false)) false)) false)) false)) false)) false) (if (= W5322 false) (let W5342 (if (shen.unlocked? V5315) (let W5343 (shen.lazyderef V5311 V5314) (if (cons? W5343) (let W5344 (shen.lazyderef (hd W5343) V5314) (if (cons? W5344) (let W5345 (shen.lazyderef (hd W5344) V5314) (if (cons? W5345) (let W5346 (shen.lazyderef (hd W5345) V5314) (if (= W5346 @p) (let W5347 (shen.lazyderef (tl W5345) V5314) (if (cons? W5347) (let W5348 (hd W5347) (let W5349 (shen.lazyderef (tl W5347) V5314) (if (cons? W5349) (let W5350 (hd W5349) (let W5351 (shen.lazyderef (tl W5349) V5314) (if (= W5351 ()) (let W5352 (shen.lazyderef (tl W5344) V5314) (if (cons? W5352) (let W5353 (hd W5352) (let W5354 (shen.lazyderef (tl W5352) V5314) (if (cons? W5354) (let W5355 (shen.lazyderef (hd W5354) V5314) (if (cons? W5355) (let W5356 (hd W5355) (let W5357 (shen.lazyderef (tl W5355) V5314) (if (cons? W5357) (let W5358 (shen.lazyderef (hd W5357) V5314) (if (= W5358 *) (let W5359 (shen.lazyderef (tl W5357) V5314) (if (cons? W5359) (let W5360 (hd W5359) (let W5361 (shen.lazyderef (tl W5359) V5314) (if (= W5361 ()) (let W5362 (shen.lazyderef (tl W5354) V5314) (if (= W5362 ()) (let W5363 (tl W5343) (do (shen.incinfs) (when (= (shen.deref W5353 V5314) (intern ":")) V5314 V5315 W5318 (freeze (shen.cut V5314 V5315 W5318 (freeze (shen.l-rules (cons (cons W5348 (cons W5353 (cons W5356 ()))) (cons (cons W5350 (cons W5353 (cons W5360 ()))) W5363)) V5312 true V5314 V5315 W5318 V5317))))))) false)) false))) false)) false)) false))) false)) false))) false)) false))) false))) false)) false)) false)) false)) false)) false) (if (= W5342 false) (let W5364 (if (shen.unlocked? V5315) (let W5365 (shen.lazyderef V5311 V5314) (if (cons? W5365) (let W5366 (shen.lazyderef (hd W5365) V5314) (if (cons? W5366) (let W5367 (shen.lazyderef (hd W5366) V5314) (if (cons? W5367) (let W5368 (shen.lazyderef (hd W5367) V5314) (if (= W5368 @s) (let W5369 (shen.lazyderef (tl W5367) V5314) (if (cons? W5369) (let W5370 (hd W5369) (let W5371 (shen.lazyderef (tl W5369) V5314) (if (cons? W5371) (let W5372 (hd W5371) (let W5373 (shen.lazyderef (tl W5371) V5314) (if (= W5373 ()) (let W5374 (shen.lazyderef (tl W5366) V5314) (if (cons? W5374) (let W5375 (hd W5374) (let W5376 (shen.lazyderef (tl W5374) V5314) (if (cons? W5376) (let W5377 (shen.lazyderef (hd W5376) V5314) (if (= W5377 string) (let W5378 (shen.lazyderef (tl W5376) V5314) (if (= W5378 ()) (let W5379 (tl W5365) (do (shen.incinfs) (when (= (shen.deref W5375 V5314) (intern ":")) V5314 V5315 W5318 (freeze (shen.cut V5314 V5315 W5318 (freeze (shen.l-rules (cons (cons W5370 (cons W5375 (cons string ()))) (cons (cons W5372 (cons W5375 (cons string ()))) W5379)) V5312 true V5314 V5315 W5318 V5317))))))) false)) false)) false))) false)) false))) false))) false)) false)) false)) false)) false)) false) (if (= W5364 false) (let W5380 (if (shen.unlocked? V5315) (let W5381 (shen.lazyderef V5311 V5314) (if (cons? W5381) (let W5382 (shen.lazyderef (hd W5381) V5314) (if (cons? W5382) (let W5383 (shen.lazyderef (hd W5382) V5314) (if (cons? W5383) (let W5384 (shen.lazyderef (hd W5383) V5314) (if (= W5384 @v) (let W5385 (shen.lazyderef (tl W5383) V5314) (if (cons? W5385) (let W5386 (hd W5385) (let W5387 (shen.lazyderef (tl W5385) V5314) (if (cons? W5387) (let W5388 (hd W5387) (let W5389 (shen.lazyderef (tl W5387) V5314) (if (= W5389 ()) (let W5390 (shen.lazyderef (tl W5382) V5314) (if (cons? W5390) (let W5391 (hd W5390) (let W5392 (shen.lazyderef (tl W5390) V5314) (if (cons? W5392) (let W5393 (shen.lazyderef (hd W5392) V5314) (if (cons? W5393) (let W5394 (shen.lazyderef (hd W5393) V5314) (if (= W5394 vector) (let W5395 (shen.lazyderef (tl W5393) V5314) (if (cons? W5395) (let W5396 (hd W5395) (let W5397 (shen.lazyderef (tl W5395) V5314) (if (= W5397 ()) (let W5398 (shen.lazyderef (tl W5392) V5314) (if (= W5398 ()) (let W5399 (tl W5381) (do (shen.incinfs) (when (= (shen.deref W5391 V5314) (intern ":")) V5314 V5315 W5318 (freeze (shen.cut V5314 V5315 W5318 (freeze (shen.l-rules (cons (cons W5386 (cons W5391 (cons W5396 ()))) (cons (cons W5388 (cons W5391 (cons (cons vector (cons W5396 ())) ()))) W5399)) V5312 true V5314 V5315 W5318 V5317))))))) false)) false))) false)) false)) false)) false))) false)) false))) false))) false)) false)) false)) false)) false)) false) (if (= W5380 false) (let W5400 (if (shen.unlocked? V5315) (let W5401 (shen.lazyderef V5311 V5314) (if (cons? W5401) (let W5402 (hd W5401) (let W5403 (tl W5401) (let W5404 (shen.lazyderef V5312 V5314) (let W5405 (lambda Z5406 (lambda Z5407 (do (shen.incinfs) (bind Z5406 W5402 V5314 V5315 W5318 (freeze (shen.l-rules W5403 Z5407 V5313 V5314 V5315 W5318 V5317)))))) (if (cons? W5404) (let W5408 (hd W5404) (let W5409 (tl W5404) ((W5405 W5408) W5409))) (if (shen.pvar? W5404) (let W5410 (shen.newpv V5314) (shen.gc V5314 (let W5411 (shen.newpv V5314) (shen.gc V5314 (shen.bind! W5404 (cons W5410 W5411) V5314 (freeze ((W5405 W5410) W5411))))))) false)))))) false)) false) (if (= W5400 false) (shen.unlock V5315 W5318) W5400)) W5380)) W5364)) W5342)) W5322)) W5319)))) + +(defun shen.t* (V5412 V5413 V5414 V5415 V5416 V5417) (let W5418 (+ V5416 1) (let W5419 (if (shen.unlocked? V5415) (let W5420 (shen.lazyderef V5412 V5414) (if (cons? W5420) (let W5421 (shen.lazyderef (hd W5420) V5414) (if (= W5421 define) (let W5422 (shen.lazyderef (tl W5420) V5414) (if (cons? W5422) (let W5423 (hd W5422) (let W5424 (tl W5422) (let W5425 (shen.newpv V5414) (shen.gc V5414 (let W5426 (shen.newpv V5414) (shen.gc V5414 (let W5427 (shen.newpv V5414) (shen.gc V5414 (let W5428 (shen.newpv V5414) (shen.gc V5414 (do (shen.incinfs) (shen.cut V5414 V5415 W5418 (freeze (bind W5425 (shen.sigxrules (cons W5423 W5424)) V5414 V5415 W5418 (freeze (bind W5428 (fst (shen.lazyderef W5425 V5414)) V5414 V5415 W5418 (freeze (bind W5426 (snd (shen.lazyderef W5425 V5414)) V5414 V5415 W5418 (freeze (bind W5427 (shen.freshen-sig (shen.deref W5428 V5414)) V5414 V5415 W5418 (freeze (shen.t*-rules W5423 W5426 W5427 1 V5414 V5415 W5418 (freeze (is W5428 V5413 V5414 V5415 W5418 V5417)))))))))))))))))))))))) false)) false)) false)) false) (if (= W5419 false) (shen.unlock V5415 W5418) W5419)))) + +(defun shen.sigxrules (V5429) (let W5430 (compile (lambda Z5431 (shen. Z5431)) V5429) (@p (fst W5430) (map (lambda Z5432 (shen.unmark-rule-custom-patterns Z5432)) (snd W5430))))) + +(defun shen.unmark-rule-custom-patterns (V5433) (cond ((tuple? V5433) (@p (map (lambda Z5434 (shen.unmark-custom-pattern Z5434)) (fst V5433)) (snd V5433))) (true (shen.f-error shen.unmark-rule-custom-patterns)))) + +(defun shen.unmark-custom-pattern (V5435) (cond ((and (cons? V5435) (and (= @p (hd V5435)) (and (cons? (tl V5435)) (and (= shen.custom-pattern (hd (tl V5435))) (and (cons? (tl (tl V5435))) (= () (tl (tl (tl V5435))))))))) (shen.unmark-custom-pattern (shen.curry (hd (tl (tl V5435)))))) ((cons? V5435) (cons (shen.unmark-custom-pattern (hd V5435)) (shen.unmark-custom-pattern (tl V5435)))) (true V5435))) + +(defun shen. (V5436) (let W5437 (if (cons? V5436) (let W5438 (tail V5436) (if (shen.hds=? W5438 {) (let W5439 (tail W5438) (let W5440 (shen. W5439) (if (shen.parse-failure? W5440) (shen.parse-failure) (let W5441 (shen.<-out W5440) (let W5442 (shen.in-> W5440) (if (shen.hds=? W5442 }) (let W5443 (tail W5442) (let W5444 (shen. W5443) (if (shen.parse-failure? W5444) (shen.parse-failure) (let W5445 (shen.<-out W5444) (let W5446 (shen.in-> W5444) (shen.comb W5446 (let W5447 (shen.rectify-type W5441) (@p W5447 W5445)))))))) (shen.parse-failure))))))) (shen.parse-failure))) (shen.parse-failure)) (if (shen.parse-failure? W5437) (shen.parse-failure) W5437))) + +(defun shen.freshen-sig (V5448) (let W5449 (shen.extract-vars V5448) (let W5450 (map (lambda Z5451 (cons Z5451 (shen.freshterm (concat & Z5451)))) W5449) (shen.freshen-type W5450 V5448)))) + +(defun shen.freshen-type (V5452 V5453) (cond ((= () V5452) V5453) ((and (cons? V5452) (cons? (hd V5452))) (shen.freshen-type (tl V5452) (subst (tl (hd V5452)) (hd (hd V5452)) V5453))) (true (shen.f-error shen.freshen-type)))) + +(defun shen. (V5454) (let W5455 (let W5456 (shen. V5454) (if (shen.parse-failure? W5456) (shen.parse-failure) (let W5457 (shen.<-out W5456) (let W5458 (shen.in-> W5456) (let W5459 (shen. W5458) (if (shen.parse-failure? W5459) (shen.parse-failure) (let W5460 (shen.<-out W5459) (let W5461 (shen.in-> W5459) (shen.comb W5461 (cons W5457 W5460)))))))))) (if (shen.parse-failure? W5455) (let W5462 (let W5463 (shen. V5454) (if (shen.parse-failure? W5463) (shen.parse-failure) (let W5464 (shen.<-out W5463) (let W5465 (shen.in-> W5463) (shen.comb W5465 (cons W5464 ())))))) (if (shen.parse-failure? W5462) (shen.parse-failure) W5462)) W5455))) + +(defun shen. (V5466) (let W5467 (let W5468 (shen. V5466) (if (shen.parse-failure? W5468) (shen.parse-failure) (let W5469 (shen.<-out W5468) (let W5470 (shen.in-> W5468) (if (shen.hds=? W5470 ->) (let W5471 (tail W5470) (if (cons? W5471) (let W5472 (head W5471) (let W5473 (tail W5471) (if (shen.hds=? W5473 where) (let W5474 (tail W5473) (if (cons? W5474) (let W5475 (head W5474) (let W5476 (tail W5474) (shen.comb W5476 (@p W5469 (cons where (cons W5475 (cons W5472 ()))))))) (shen.parse-failure))) (shen.parse-failure)))) (shen.parse-failure))) (shen.parse-failure)))))) (if (shen.parse-failure? W5467) (let W5477 (let W5478 (shen. V5466) (if (shen.parse-failure? W5478) (shen.parse-failure) (let W5479 (shen.<-out W5478) (let W5480 (shen.in-> W5478) (if (shen.hds=? W5480 <-) (let W5481 (tail W5480) (if (cons? W5481) (let W5482 (head W5481) (let W5483 (tail W5481) (if (shen.hds=? W5483 where) (let W5484 (tail W5483) (if (cons? W5484) (let W5485 (head W5484) (let W5486 (tail W5484) (shen.comb W5486 (@p W5479 (shen.correct (cons where (cons W5485 (cons W5482 ())))))))) (shen.parse-failure))) (shen.parse-failure)))) (shen.parse-failure))) (shen.parse-failure)))))) (if (shen.parse-failure? W5477) (let W5487 (let W5488 (shen. V5466) (if (shen.parse-failure? W5488) (shen.parse-failure) (let W5489 (shen.<-out W5488) (let W5490 (shen.in-> W5488) (if (shen.hds=? W5490 <-) (let W5491 (tail W5490) (if (cons? W5491) (let W5492 (head W5491) (let W5493 (tail W5491) (shen.comb W5493 (@p W5489 (shen.correct W5492))))) (shen.parse-failure))) (shen.parse-failure)))))) (if (shen.parse-failure? W5487) (let W5494 (let W5495 (shen. V5466) (if (shen.parse-failure? W5495) (shen.parse-failure) (let W5496 (shen.<-out W5495) (let W5497 (shen.in-> W5495) (if (shen.hds=? W5497 ->) (let W5498 (tail W5497) (if (cons? W5498) (let W5499 (head W5498) (let W5500 (tail W5498) (shen.comb W5500 (@p W5496 W5499)))) (shen.parse-failure))) (shen.parse-failure)))))) (if (shen.parse-failure? W5494) (shen.parse-failure) W5494)) W5487)) W5477)) W5467))) + +(defun shen.correct (V5501) (cond ((and (cons? V5501) (and (= where (hd V5501)) (and (cons? (tl V5501)) (and (cons? (tl (tl V5501))) (and (cons? (hd (tl (tl V5501)))) (and (= fail-if (hd (hd (tl (tl V5501))))) (and (cons? (tl (hd (tl (tl V5501))))) (and (cons? (tl (tl (hd (tl (tl V5501)))))) (and (= () (tl (tl (tl (hd (tl (tl V5501))))))) (= () (tl (tl (tl V5501))))))))))))) (cons where (cons (cons and (cons (hd (tl V5501)) (cons (cons not (cons (tl (hd (tl (tl V5501)))) ())) ()))) (tl (tl (hd (tl (tl V5501)))))))) ((and (cons? V5501) (and (= where (hd V5501)) (and (cons? (tl V5501)) (and (cons? (tl (tl V5501))) (= () (tl (tl (tl V5501)))))))) (cons where (cons (cons and (cons (hd (tl V5501)) (cons (cons not (cons (cons == (cons (hd (tl (tl V5501))) (cons (cons fail ()) ()))) ())) ()))) (tl (tl V5501))))) ((and (cons? V5501) (and (= fail-if (hd V5501)) (and (cons? (tl V5501)) (and (cons? (tl (tl V5501))) (= () (tl (tl (tl V5501)))))))) (cons where (cons (cons not (cons (tl V5501) ())) (tl (tl V5501))))) (true (cons where (cons (cons not (cons (cons == (cons V5501 (cons (cons fail ()) ()))) ())) (cons V5501 ())))))) + +(defun shen.t*-rules (V5502 V5503 V5504 V5505 V5506 V5507 V5508 V5509) (let W5510 (+ V5508 1) (let W5511 (if (shen.unlocked? V5507) (let W5512 (shen.lazyderef V5503 V5506) (if (= W5512 ()) (do (shen.incinfs) (thaw V5509)) false)) false) (if (= W5511 false) (let W5513 (if (shen.unlocked? V5507) (let W5514 (shen.lazyderef V5503 V5506) (if (cons? W5514) (let W5515 (hd W5514) (let W5516 (tl W5514) (let W5517 (shen.newpv V5506) (shen.gc V5506 (do (shen.incinfs) (bind W5517 (shen.freshen-rule (shen.deref W5515 V5506)) V5506 V5507 W5510 (freeze (shen.t*-rule V5502 V5505 (fst (shen.lazyderef W5517 V5506)) (snd (shen.lazyderef W5517 V5506)) V5504 V5506 V5507 W5510 (freeze (shen.cut V5506 V5507 W5510 (freeze (shen.t*-rules V5502 W5516 V5504 (+ V5505 1) V5506 V5507 W5510 V5509)))))))))))) false)) false) (if (= W5513 false) (shen.unlock V5507 W5510) W5513)) W5511)))) + +(defun shen.freshen-rule (V5518) (cond ((tuple? V5518) (let W5519 (shen.extract-vars (fst V5518)) (let W5520 (map (lambda Z5521 (cons Z5521 (shen.freshterm Z5521))) W5519) (@p (shen.freshen W5520 (fst V5518)) (shen.freshen W5520 (snd V5518)))))) (true (shen.f-error shen.freshen-rule)))) + +(defun shen.freshen (V5522 V5523) (cond ((= () V5522) V5523) ((and (cons? V5522) (cons? (hd V5522))) (shen.freshen (tl V5522) (shen.beta (hd (hd V5522)) (tl (hd V5522)) V5523))) (true (shen.f-error shen.freshen)))) + +(defun shen.t*-rule (V5524 V5525 V5526 V5527 V5528 V5529 V5530 V5531 V5532) (let W5533 (if (shen.unlocked? V5530) (do (shen.incinfs) (shen.t*-rule-h V5526 V5527 V5528 V5529 V5530 V5531 V5532)) false) (if (= W5533 false) (if (shen.unlocked? V5530) (let W5534 (shen.newpv V5529) (shen.gc V5529 (do (shen.incinfs) (bind W5534 (simple-error (cn "type error in rule " (shen.app V5525 (cn " of " (shen.app V5524 " +" shen.a)) shen.a))) V5529 V5530 V5531 V5532)))) false) W5533))) + +(defun shen.t*-rule-h (V5535 V5536 V5537 V5538 V5539 V5540 V5541) (let W5542 (+ V5540 1) (let W5543 (if (shen.unlocked? V5539) (let W5544 (shen.lazyderef V5535 V5538) (if (= W5544 ()) (let W5545 (shen.lazyderef V5537 V5538) (if (cons? W5545) (let W5546 (shen.lazyderef (hd W5545) V5538) (if (= W5546 -->) (let W5547 (shen.lazyderef (tl W5545) V5538) (if (cons? W5547) (let W5548 (hd W5547) (let W5549 (shen.lazyderef (tl W5547) V5538) (if (= W5549 ()) (do (shen.incinfs) (shen.cut V5538 V5539 W5542 (freeze (shen.t*-correct V5536 W5548 () V5538 V5539 W5542 V5541)))) false))) false)) false)) false)) false)) false) (if (= W5543 false) (let W5550 (if (shen.unlocked? V5539) (let W5551 (shen.newpv V5538) (shen.gc V5538 (let W5552 (shen.newpv V5538) (shen.gc V5538 (let W5553 (shen.newpv V5538) (shen.gc V5538 (do (shen.incinfs) (shen.p-hyps (shen.freshterms V5535) W5551 V5538 V5539 W5542 (freeze (shen.t*-integrity V5535 V5537 W5551 W5552 V5538 V5539 W5542 (freeze (shen.cut V5538 V5539 W5542 (freeze (shen.myassume V5535 V5537 W5553 V5538 V5539 W5542 (freeze (shen.t*-correct V5536 W5552 W5553 V5538 V5539 W5542 V5541)))))))))))))))) false) (if (= W5550 false) (shen.unlock V5539 W5542) W5550)) W5543)))) + +(defun shen.myassume (V5554 V5555 V5556 V5557 V5558 V5559 V5560) (let W5561 (if (shen.unlocked? V5558) (let W5562 (shen.lazyderef V5554 V5557) (if (= W5562 ()) (let W5563 (shen.lazyderef V5556 V5557) (let W5564 (freeze (do (shen.incinfs) (thaw V5560))) (if (= W5563 ()) (thaw W5564) (if (shen.pvar? W5563) (shen.bind! W5563 () V5557 W5564) false)))) false)) false) (if (= W5561 false) (if (shen.unlocked? V5558) (let W5565 (shen.lazyderef V5554 V5557) (if (cons? W5565) (let W5566 (hd W5565) (let W5567 (tl W5565) (let W5568 (shen.lazyderef V5555 V5557) (if (cons? W5568) (let W5569 (hd W5568) (let W5570 (shen.lazyderef (tl W5568) V5557) (if (cons? W5570) (let W5571 (shen.lazyderef (hd W5570) V5557) (if (= W5571 -->) (let W5572 (shen.lazyderef (tl W5570) V5557) (if (cons? W5572) (let W5573 (hd W5572) (let W5574 (shen.lazyderef (tl W5572) V5557) (if (= W5574 ()) (let W5575 (shen.lazyderef V5556 V5557) (let W5576 (lambda Z5577 (lambda Z5578 (lambda Z5579 (lambda Z5580 (do (shen.incinfs) (is! W5569 Z5579 V5557 V5558 V5559 (freeze (is! W5566 Z5577 V5557 V5558 V5559 (freeze (bind Z5578 (intern ":") V5557 V5558 V5559 (freeze (shen.myassume W5567 W5573 Z5580 V5557 V5558 V5559 V5560)))))))))))) (if (cons? W5575) (let W5581 (shen.lazyderef (hd W5575) V5557) (let W5582 (lambda Z5583 (lambda Z5584 (lambda Z5585 (let W5586 (tl W5575) ((((W5576 Z5583) Z5584) Z5585) W5586))))) (if (cons? W5581) (let W5587 (hd W5581) (let W5588 (shen.lazyderef (tl W5581) V5557) (let W5589 (lambda Z5590 (lambda Z5591 (((W5582 W5587) Z5590) Z5591))) (if (cons? W5588) (let W5592 (hd W5588) (let W5593 (shen.lazyderef (tl W5588) V5557) (let W5594 (lambda Z5595 ((W5589 W5592) Z5595)) (if (cons? W5593) (let W5596 (hd W5593) (let W5597 (shen.lazyderef (tl W5593) V5557) (let W5598 (freeze (W5594 W5596)) (if (= W5597 ()) (thaw W5598) (if (shen.pvar? W5597) (shen.bind! W5597 () V5557 W5598) false))))) (if (shen.pvar? W5593) (let W5599 (shen.newpv V5557) (shen.gc V5557 (shen.bind! W5593 (cons W5599 ()) V5557 (freeze (W5594 W5599))))) false))))) (if (shen.pvar? W5588) (let W5600 (shen.newpv V5557) (shen.gc V5557 (let W5601 (shen.newpv V5557) (shen.gc V5557 (shen.bind! W5588 (cons W5600 (cons W5601 ())) V5557 (freeze ((W5589 W5600) W5601))))))) false))))) (if (shen.pvar? W5581) (let W5602 (shen.newpv V5557) (shen.gc V5557 (let W5603 (shen.newpv V5557) (shen.gc V5557 (let W5604 (shen.newpv V5557) (shen.gc V5557 (shen.bind! W5581 (cons W5602 (cons W5603 (cons W5604 ()))) V5557 (freeze (((W5582 W5602) W5603) W5604))))))))) false)))) (if (shen.pvar? W5575) (let W5605 (shen.newpv V5557) (shen.gc V5557 (let W5606 (shen.newpv V5557) (shen.gc V5557 (let W5607 (shen.newpv V5557) (shen.gc V5557 (let W5608 (shen.newpv V5557) (shen.gc V5557 (shen.bind! W5575 (cons (cons W5605 (cons W5606 (cons W5607 ()))) W5608) V5557 (freeze ((((W5576 W5605) W5606) W5607) W5608))))))))))) false)))) false))) false)) false)) false))) false)))) false)) false) W5561))) + +(defun shen.freshterms (V5611) (cond ((= () V5611) ()) ((and (cons? V5611) (cons? (hd V5611))) (shen.freshterms (append (hd V5611) (tl V5611)))) ((and (cons? V5611) (shen.freshterm? (hd V5611))) (adjoin (hd V5611) (shen.freshterms (tl V5611)))) ((cons? V5611) (shen.freshterms (tl V5611))) (true (shen.f-error shen.freshterms)))) + +(defun shen.p-hyps (V5612 V5613 V5614 V5615 V5616 V5617) (let W5618 (if (shen.unlocked? V5615) (let W5619 (shen.lazyderef V5612 V5614) (if (= W5619 ()) (let W5620 (shen.lazyderef V5613 V5614) (let W5621 (freeze (do (shen.incinfs) (thaw V5617))) (if (= W5620 ()) (thaw W5621) (if (shen.pvar? W5620) (shen.bind! W5620 () V5614 W5621) false)))) false)) false) (if (= W5618 false) (if (shen.unlocked? V5615) (let W5622 (shen.lazyderef V5612 V5614) (if (cons? W5622) (let W5623 (hd W5622) (let W5624 (tl W5622) (let W5625 (shen.lazyderef V5613 V5614) (let W5626 (lambda Z5627 (lambda Z5628 (lambda Z5629 (lambda Z5630 (do (shen.incinfs) (bind Z5627 W5623 V5614 V5615 V5616 (freeze (bind Z5628 (intern ":") V5614 V5615 V5616 (freeze (shen.p-hyps W5624 Z5630 V5614 V5615 V5616 V5617)))))))))) (if (cons? W5625) (let W5631 (shen.lazyderef (hd W5625) V5614) (let W5632 (lambda Z5633 (lambda Z5634 (lambda Z5635 (let W5636 (tl W5625) ((((W5626 Z5633) Z5634) Z5635) W5636))))) (if (cons? W5631) (let W5637 (hd W5631) (let W5638 (shen.lazyderef (tl W5631) V5614) (let W5639 (lambda Z5640 (lambda Z5641 (((W5632 W5637) Z5640) Z5641))) (if (cons? W5638) (let W5642 (hd W5638) (let W5643 (shen.lazyderef (tl W5638) V5614) (let W5644 (lambda Z5645 ((W5639 W5642) Z5645)) (if (cons? W5643) (let W5646 (hd W5643) (let W5647 (shen.lazyderef (tl W5643) V5614) (let W5648 (freeze (W5644 W5646)) (if (= W5647 ()) (thaw W5648) (if (shen.pvar? W5647) (shen.bind! W5647 () V5614 W5648) false))))) (if (shen.pvar? W5643) (let W5649 (shen.newpv V5614) (shen.gc V5614 (shen.bind! W5643 (cons W5649 ()) V5614 (freeze (W5644 W5649))))) false))))) (if (shen.pvar? W5638) (let W5650 (shen.newpv V5614) (shen.gc V5614 (let W5651 (shen.newpv V5614) (shen.gc V5614 (shen.bind! W5638 (cons W5650 (cons W5651 ())) V5614 (freeze ((W5639 W5650) W5651))))))) false))))) (if (shen.pvar? W5631) (let W5652 (shen.newpv V5614) (shen.gc V5614 (let W5653 (shen.newpv V5614) (shen.gc V5614 (let W5654 (shen.newpv V5614) (shen.gc V5614 (shen.bind! W5631 (cons W5652 (cons W5653 (cons W5654 ()))) V5614 (freeze (((W5632 W5652) W5653) W5654))))))))) false)))) (if (shen.pvar? W5625) (let W5655 (shen.newpv V5614) (shen.gc V5614 (let W5656 (shen.newpv V5614) (shen.gc V5614 (let W5657 (shen.newpv V5614) (shen.gc V5614 (let W5658 (shen.newpv V5614) (shen.gc V5614 (shen.bind! W5625 (cons (cons W5655 (cons W5656 (cons W5657 ()))) W5658) V5614 (freeze ((((W5626 W5655) W5656) W5657) W5658))))))))))) false)))))) false)) false) W5618))) + +(defun shen.t*-correct (V5659 V5660 V5661 V5662 V5663 V5664 V5665) (let W5666 (+ V5664 1) (let W5667 (if (shen.unlocked? V5663) (let W5668 (shen.lazyderef V5659 V5662) (if (cons? W5668) (let W5669 (shen.lazyderef (hd W5668) V5662) (if (= W5669 where) (let W5670 (shen.lazyderef (tl W5668) V5662) (if (cons? W5670) (let W5671 (hd W5670) (let W5672 (shen.lazyderef (tl W5670) V5662) (if (cons? W5672) (let W5673 (hd W5672) (let W5674 (shen.lazyderef (tl W5672) V5662) (if (= W5674 ()) (let W5675 (shen.newpv V5662) (shen.gc V5662 (do (shen.incinfs) (shen.cut V5662 V5663 W5666 (freeze (bind W5675 (shen.curry W5671) V5662 V5663 W5666 (freeze (shen.system-S-h W5675 boolean V5661 V5662 V5663 W5666 (freeze (shen.cut V5662 V5663 W5666 (freeze (shen.t*-correct W5673 V5660 (cons (cons W5675 (cons (intern ":") (cons verified ()))) V5661) V5662 V5663 W5666 V5665)))))))))))) false))) false))) false)) false)) false)) false) (if (= W5667 false) (let W5676 (if (shen.unlocked? V5663) (do (shen.incinfs) (shen.system-S-h (shen.curry V5659) V5660 V5661 V5662 V5663 W5666 V5665)) false) (if (= W5676 false) (shen.unlock V5663 W5666) W5676)) W5667)))) + +(defun shen.t*-integrity (V5677 V5678 V5679 V5680 V5681 V5682 V5683 V5684) (let W5685 (if (shen.unlocked? V5682) (let W5686 (shen.lazyderef V5677 V5681) (if (= W5686 ()) (do (shen.incinfs) (is! V5678 V5680 V5681 V5682 V5683 V5684)) false)) false) (if (= W5685 false) (if (shen.unlocked? V5682) (let W5687 (shen.lazyderef V5677 V5681) (if (cons? W5687) (let W5688 (hd W5687) (let W5689 (tl W5687) (let W5690 (shen.lazyderef V5678 V5681) (if (cons? W5690) (let W5691 (hd W5690) (let W5692 (shen.lazyderef (tl W5690) V5681) (if (cons? W5692) (let W5693 (shen.lazyderef (hd W5692) V5681) (if (= W5693 -->) (let W5694 (shen.lazyderef (tl W5692) V5681) (if (cons? W5694) (let W5695 (hd W5694) (let W5696 (shen.lazyderef (tl W5694) V5681) (if (= W5696 ()) (do (shen.incinfs) (shen.system-S-h W5688 W5691 V5679 V5681 V5682 V5683 (freeze (shen.t*-integrity W5689 W5695 V5679 V5680 V5681 V5682 V5683 V5684)))) false))) false)) false)) false))) false)))) false)) false) W5685))) + +(defun shen.freshterm? (V5697) (and (absvector? V5697) (and (not (string? V5697)) (= (<-address V5697 0) shen.print-freshterm)))) + diff --git a/test/kernel_conformance_spec.lua b/test/kernel_conformance_spec.lua new file mode 100644 index 0000000..50ac4aa --- /dev/null +++ b/test/kernel_conformance_spec.lua @@ -0,0 +1,292 @@ +-- test/kernel_conformance_spec.lua — the native typechecker against more +-- than one kernel. +-- +-- luajit test/kernel_conformance_spec.lua +-- +-- The native typechecker (typecheck_native.lua) translates the kernel's +-- t-star.kl drivers and hand-ports a few kernel defuns. A kernel whose +-- type-checker defuns differ from what the port expects must make it either +-- follow the kernel or fall back to the kernel's own KL typechecker, loudly. +-- It must never keep running and return a different verdict: that is what a +-- ShenOSKernel 43.1 with the deref hoist used to do (its new +-- shen.by-hypothesis-h was called as nil(...), the Lua error surfaced as an +-- ordinary Shen exception, and a caller's trap-error read it as "does not +-- typecheck"). +-- +-- Each case boots a child process on a kernel directory (SHEN_KL_DIR, with the +-- kernel and fasl caches off so nothing shared is touched), runs a battery of +-- typechecks, and compares the verdicts with the same battery run on the +-- kernel's KL typechecker (SHEN_TYPECHECK_NATIVE=off) and with fixed +-- expectations. +-- +-- Kernels: +-- vendored klambda/ (ShenOSKernel 43.1) +-- hoist 43.1 with the deref hoist (test/fixtures/kernel-43.1-deref-hoist) +-- mutated 43.1 whose shen.search-user-datatypes never consults a +-- datatype: the hand port no longer implements it +-- moved hoist, with shen.by-hypothesis-h moved out of t-star.kl: a +-- driver now calls a predicate the port cannot translate + +local here = arg[0]:gsub("test/[^/]*$", "") +if here == "" then here = "./" end + +-- --------------------------------------------------------------------------- +-- child mode: boot, run the battery, print one line per verdict +-- --------------------------------------------------------------------------- +if arg[1] == "--child" then + package.path = here .. "?.lua;" .. package.path + local shen = require("shen") + shen.boot{ quiet = true } + local R = require("runtime") + local TN = require("typecheck_native") + local opts = {} + for i = 2, #arg do opts[arg[i]] = true end + + -- "raw" calls the kernel entry directly: shen.typecheck (the Lua API) + -- fails closed, turning any error into false + local function raw_typecheck(expr, ty) + local f = shen.prims.F["read-from-string"](expr .. " : " .. ty) + return shen.prims.F["shen.typecheck"](f[1], f[2][2][1]) + end + local function tc(expr, ty) + local ok, r = pcall(opts.raw and raw_typecheck or shen.typecheck, expr, ty) + if not ok then + local msg = (getmetatable(r) == R.Excn) and tostring(r.msg) or tostring(r) + return "ERROR " .. msg:gsub("\n", " ") + end + return r == false and "false" or R.to_str(r) + end + + shen.eval([[ +(define spec.holds? + Premise -> (trap-error (= true (eval Premise)) (/. E false))) +(datatype spec-discharge-verified + if (spec.holds? Premise) + ______________________ + Premise : verified;) +(datatype spec-box + X : string; + (not (= X "")) : verified; + ================ + [X] : spec-box;) +]]) + local battery = { + { "(/. X (+ X 1))", "(number --> number)" }, + { "(/. X Y (@p X Y))", "(number --> (string --> (number * string)))" }, + { "(/. X (+ X \"a\"))", "(number --> number)" }, + { "[1 2 3]", "(list number)" }, + { "(let X 1 (+ X X))", "number" }, + { "(/. F X (F (F X)))", "((number --> number) --> (number --> number))" }, + { "(/. X Y (if (= X Y) X Y))", "(symbol --> (symbol --> symbol))" }, + { "[\"hi\"]", "spec-box" }, + { "[\"\"]", "spec-box" }, + { "[37]", "spec-box" }, + } + if opts["drop-np"] then + -- warm the drivers, then lose one: the next query must miss loudly + tc("1", "number") + rawset(require("prolog_engine").NativePred, "shen.by-hypothesis", nil) + end + for i, c in ipairs(battery) do + io.write("V", i, "\t", c[1], " : ", c[2], "\t", tc(c[1], c[2]), "\n") + end + if TN.drivers then + io.write("PROBLEMS\t", TN.native_problems and #TN.native_problems or 0, "\n") + io.write("ADDED\t", table.concat(TN.drivers_added, ","), "\n") + -- the by-hypothesis comparison must stay on the arena (E.eqterm) + local PC = require("prolog_compile") + local defs = TN.defuns_of(io.open((os.getenv("SHEN_KL_DIR") or (here .. "klambda")) + .. "/t-star.kl"):read("*a")) + for _, name in ipairs({ "shen.by-hypothesis", "shen.by-hypothesis-h" }) do + if defs[name] then + local _, _, src = PC.translate_defun(defs[name]) + io.write("EQTERM\t", name, "\t", (src and src:find("EQTERM(", 1, true)) and "yes" or "no", "\n") + end + end + end + os.exit(0) +end + +-- --------------------------------------------------------------------------- +-- parent +-- --------------------------------------------------------------------------- +local npass, nfail = 0, 0 +local function check(cond, name) + if cond then npass = npass + 1 + else + nfail = nfail + 1 + io.write("FAIL: ", name, "\n") + end +end + +local function sh_quote(s) return "'" .. tostring(s):gsub("'", "'\\''") .. "'" end +local LUA = os.getenv("LUA") +if not LUA or LUA == "" then LUA = "luajit" end + +local function slurp(path) + local f = assert(io.open(path, "r")) + local s = f:read("*a"); f:close() + return s +end +local function spit(path, s) + local f = assert(io.open(path, "w")) + f:write(s); f:close() +end + +local tmp = os.tmpname() +os.remove(tmp) +os.execute("mkdir -p " .. sh_quote(tmp)) + +local KERNEL_FILES = { + "core", "declarations", "dict", "extension-expand-dynamic", + "extension-features", "extension-launcher", "extension-namespaces", + "extension-programmable-pattern-matching", "extension-type-annotations", + "init", "load", "macros", "prolog", "reader", "sequent", "stlib", "sys", + "t-star", "toplevel", "track", "types", "writer", "yacc", +} +-- a kernel dir: the vendored files, with `edits[name] = function(src) -> src` +local function kernel(name, edits) + local dir = tmp .. "/" .. name + os.execute("mkdir -p " .. sh_quote(dir)) + for _, f in ipairs(KERNEL_FILES) do + local src = slurp(here .. "klambda/" .. f .. ".kl") + if edits[f] then src = edits[f](src) end + spit(dir .. "/" .. f .. ".kl", src) + end + return dir +end + +local HOIST = slurp(here .. "test/fixtures/kernel-43.1-deref-hoist/t-star.kl") + +-- replace / remove the single-line (defun NAME ...) of a .kl source +local function defun_line(src, name) + local pat = "\n%(defun " .. name:gsub("%p", "%%%0") .. " %([^\n]*" + local s, e = src:find(pat) + assert(s, "no defun " .. name) + return s, e +end +local function replace_defun(src, name, with) + local s, e = defun_line(src, name) + return src:sub(1, s) .. with .. src:sub(e + 1) +end + +local KDIR = { + vendored = nil, + hoist = kernel("hoist", { ["t-star"] = function() return HOIST end }), + mutated = kernel("mutated", { ["t-star"] = function(src) + return replace_defun(src, "shen.search-user-datatypes", + "(defun shen.search-user-datatypes (V1 V2 V3 V4 V5 V6 V7) false)") + end }), +} +do + local s, e = defun_line(HOIST, "shen.by-hypothesis-h") + local helper = HOIST:sub(s + 1, e) + KDIR.moved = kernel("moved", { + ["t-star"] = function() return HOIST:sub(1, s) .. HOIST:sub(e + 1) end, + ["sys"] = function(src) return src .. "\n" .. helper .. "\n" end, + }) +end + +local function child(kdir, env, opts) + local cmd = "env " .. (kdir and "" or "-u SHEN_KL_DIR ") + .. "SHEN_KERNEL_CACHE=off SHEN_FASL=off " + .. (kdir and ("SHEN_KL_DIR=" .. sh_quote(kdir) .. " ") or "") + .. (env or "") .. " " .. LUA .. " " .. sh_quote(here .. "test/kernel_conformance_spec.lua") + .. " --child " .. (opts or "") .. " 2>" .. sh_quote(tmp .. "/stderr") + local h = io.popen(cmd) + local out = h:read("*a") or "" + h:close() + local res = { verdicts = {}, eqterm = {}, out = out, stderr = slurp(tmp .. "/stderr") } + for line in out:gmatch("[^\n]+") do + local f = {} + for x in line:gmatch("[^\t]+") do f[#f + 1] = x end + if f[1]:match("^V%d+$") then res.verdicts[#res.verdicts + 1] = f[2] .. " => " .. (f[3] or "") + elseif f[1] == "PROBLEMS" then res.problems = tonumber(f[2]) + elseif f[1] == "ADDED" then res.added = f[2] or "" + elseif f[1] == "EQTERM" then res.eqterm[f[2]] = f[3] end + end + return res +end + +local WANT = { + "(/. X (+ X 1)) : (number --> number) => (number --> number)", + "(/. X Y (@p X Y)) : (number --> (string --> (number * string))) => (number --> (string --> (number * string)))", + "(/. X (+ X \"a\")) : (number --> number) => false", + "[1 2 3] : (list number) => (list number)", + "(let X 1 (+ X X)) : number => number", + "(/. F X (F (F X))) : ((number --> number) --> (number --> number)) => ((number --> number) --> (number --> number))", + "(/. X Y (if (= X Y) X Y)) : (symbol --> (symbol --> symbol)) => (symbol --> (symbol --> symbol))", + "[\"hi\"] : spec-box => spec-box", + "[\"\"] : spec-box => false", + "[37] : spec-box => false", +} + +local function same_list(a, b) + if #a ~= #b then return false end + for i = 1, #a do if a[i] ~= b[i] then return false end end + return true +end +local function show(r) return table.concat(r.verdicts, " | ") .. "\n" .. r.out .. r.stderr end + +local NOTICE = "native typechecker unavailable" + +-- vendored and hoisted kernels: native, with no fallback, and the same +-- verdicts as the kernel's KL typechecker +for _, k in ipairs({ "vendored", "hoist" }) do + local native = child(KDIR[k]) + local legacy = child(KDIR[k], "SHEN_TYPECHECK_NATIVE=off") + check(same_list(legacy.verdicts, WANT), k .. ": KL typechecker verdicts as expected\n" .. show(legacy)) + check(same_list(native.verdicts, legacy.verdicts), k .. ": native verdicts == KL verdicts\n" .. show(native)) + check(native.problems == 0, k .. ": native typechecker conforms (" .. tostring(native.problems) .. " problems)\n" .. native.stderr) + check(not native.stderr:find(NOTICE, 1, true), k .. ": no fallback notice\n" .. native.stderr) + local strict = child(KDIR[k], "SHEN_TYPECHECK_NATIVE=require") + check(same_list(strict.verdicts, WANT), k .. ": verdicts under SHEN_TYPECHECK_NATIVE=require\n" .. show(strict)) +end + +do + local v = child(KDIR.vendored) + check(v.added == nil or v.added == "", "vendored: driver closure adds nothing (" .. tostring(v.added) .. ")") + check(v.eqterm["shen.by-hypothesis"] == "yes", "vendored: by-hypothesis compares on the arena") + local h = child(KDIR.hoist) + check(h.added == "shen.by-hypothesis-h", "hoist: driver closure adds shen.by-hypothesis-h (" .. tostring(h.added) .. ")") + check(h.eqterm["shen.by-hypothesis-h"] == "yes", "hoist: by-hypothesis-h compares on the arena") +end + +-- a hand-ported defun the kernel changed: fall back to the KL typechecker +-- (same verdicts as the changed kernel), say so, and refuse under require +do + local legacy = child(KDIR.mutated, "SHEN_TYPECHECK_NATIVE=off") + local native = child(KDIR.mutated) + check(legacy.verdicts[8] == "[\"hi\"] : spec-box => false", + "mutated: the changed kernel consults no datatype\n" .. show(legacy)) + check(same_list(native.verdicts, legacy.verdicts), "mutated: native verdicts == mutated kernel's\n" .. show(native)) + check(native.stderr:find(NOTICE, 1, true) and native.stderr:find("shen.search-user-datatypes", 1, true), + "mutated: stderr names the changed defun\n" .. native.stderr) + local strict = child(KDIR.mutated, "SHEN_TYPECHECK_NATIVE=require", "raw") + check(strict.verdicts[1] and strict.verdicts[1]:find("ERROR", 1, true) + and strict.verdicts[1]:find("SHEN_TYPECHECK_NATIVE=require", 1, true), + "mutated: require mode raises\n" .. show(strict)) +end + +-- a driver that calls a predicate outside t-star.kl: caught before any query +do + local legacy = child(KDIR.moved, "SHEN_TYPECHECK_NATIVE=off") + local native = child(KDIR.moved) + check(same_list(legacy.verdicts, WANT), "moved: KL typechecker verdicts as expected\n" .. show(legacy)) + check(same_list(native.verdicts, WANT), "moved: native falls back with the right verdicts\n" .. show(native)) + check(native.stderr:find("calls shen.by-hypothesis-h, which is neither", 1, true), + "moved: stderr names the unresolvable call\n" .. native.stderr) +end + +-- a predicate lost at run time: the query misses loudly and is rerun on the +-- KL engine, never answered from a nil call +do + local native = child(nil, nil, "drop-np") + check(same_list(native.verdicts, WANT), "drop-np: verdicts unchanged\n" .. show(native)) + check(native.stderr:find("no native predicate shen.by-hypothesis", 1, true), + "drop-np: stderr names the missing predicate\n" .. native.stderr) +end + +os.execute("rm -rf " .. sh_quote(tmp)) +io.write(string.format("kernel_conformance_spec: %d pass, %d fail\n", npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/typecheck_native.lua b/typecheck_native.lua index 441bd52..ec9f8df 100644 --- a/typecheck_native.lua +++ b/typecheck_native.lua @@ -39,6 +39,40 @@ local Symbol, Cons, NIL = R.Symbol, R.Cons, R.NIL local getmt = getmetatable -- t-star driver functions fed through the translator +-- Native-path fallback reporting. SHEN_TYPECHECK_NATIVE=require turns a +-- fallback into an error (for CI and kernel bring-up); otherwise each distinct +-- cause is reported once on stderr and the kernel's KL typechecker answers. +local REQUIRE_NATIVE = os.getenv("SHEN_TYPECHECK_NATIVE") == "require" +local reported = {} +local function fallback_notice(key, lines) + local msg = "native typechecker unavailable, using the kernel's KL " + .. "typechecker: " .. table.concat(lines, "; ") + if not reported[key] then + reported[key] = true + io.stderr:write("shen-lua: ", msg, "\n") + end + if REQUIRE_NATIVE then + P.ERR("SHEN_TYPECHECK_NATIVE=require: " .. msg) + end +end + +-- the hand-ported predicates (install_handports) a driver may call by name +local HANDPORTED = { + ["shen.lookupsig"] = true, + ["shen.search-user-datatypes"] = true, + ["shen.show"] = true, +} + +-- the drivers native_typecheck enters directly; the kernel must define them +local ENTRY_FNS = { + ["shen.insert-prolog-variables"] = true, + ["shen.toplevel-forms"] = true, +} + +-- t-star driver functions fed through the translator: the roots of the +-- driver closure computed in ensure_drivers (which also translates every +-- t-star.kl defun these call, e.g. a deref-hoisted kernel's +-- shen.by-hypothesis-h) local DRIVER_FNS = { ["shen.insert-prolog-variables"] = true, ["shen.toplevel-forms"] = true, @@ -58,6 +92,196 @@ local DRIVER_FNS = { ["shen.myassume"] = true, } +-- --------------------------------------------------------------------------- +-- Kernel conformance +-- +-- The drivers above are TRANSLATED from the kernel's own t-star.kl, so they +-- follow the kernel. Everything else on the native path is a hand-written +-- reimplementation of a kernel defun: the hand-ported drivers (shen.typecheck, +-- lookupsig/sigf, search-user-datatypes, show, maxinfexceeded?) and the +-- prolog.kl primitives and builtin goals the engine and translator implement +-- natively (prolog_engine.lua, prolog_compile.lua). If the kernel changes one +-- of those, the native typechecker would keep running the OLD semantics and +-- could answer differently from the kernel without any error. So before the +-- first native typecheck, every such defun in the loaded kernel is +-- fingerprinted and compared against the versions this port implements. A +-- mismatch turns the native typechecker off for the process: typechecks run +-- the kernel's own KL definitions (correct by construction, slower) and one +-- line on stderr names the defun. SHEN_TYPECHECK_NATIVE=require raises an +-- error instead. +-- +-- The fingerprint is a hash of the defun's KL form with its gensym variables +-- (V123 / W123 / Z123) renamed in first-occurrence order, so regenerating the +-- kernel (which renumbers every later gensym) does not change it. Regenerate +-- the table with scripts/kernel-fingerprints.lua when vendoring a kernel, and +-- only after checking that the native code still implements the new defun. +-- --------------------------------------------------------------------------- +local REIMPLEMENTED = { + -- t-star.kl hand ports + { "t-star", "shen.typecheck" }, + { "t-star", "shen.lookupsig" }, + { "t-star", "shen.sigf" }, + { "t-star", "shen.search-user-datatypes" }, + { "t-star", "shen.search-user-datatypes-h" }, -- deref-hoisted kernels + { "t-star", "shen.show" }, + { "t-star", "shen.maxinfexceeded?" }, + -- prolog.kl primitives the translator lowers and the engine implements + { "prolog", "shen.deref" }, + { "prolog", "shen.lazyderef" }, + { "prolog", "shen.newpv" }, + { "prolog", "shen.gc" }, + { "prolog", "shen.pvar?" }, + { "prolog", "shen.bind!" }, + { "prolog", "shen.bindv" }, + { "prolog", "shen.unwind" }, + { "prolog", "shen.incinfs" }, + { "prolog", "shen.cut" }, + { "prolog", "shen.unlock" }, + { "prolog", "shen.unlocked?" }, + { "prolog", "shen.lock" }, + { "prolog", "shen.fits?" }, + { "prolog", "shen.lzy=" }, + { "prolog", "shen.lzy=!" }, + { "prolog", "shen.occurs-check?" }, + -- prolog.kl builtin goals (prolog_compile.lua BUILTIN_GOALS) + { "prolog", "when" }, + { "prolog", "is" }, + { "prolog", "is!" }, + { "prolog", "bind" }, + { "prolog", "var?" }, + { "prolog", "return" }, + { "prolog", "fork" }, + { "prolog", "findall" }, + { "prolog", "call" }, +} +M.REIMPLEMENTED = REIMPLEMENTED + +-- name -> { [fingerprint] = kernel label }. "absent" = the defun may be +-- missing. Generated by scripts/kernel-fingerprints.lua. +local KNOWN = { + -- 43.1 = ShenOSKernel 43.1 as vendored (klambda/PROVENANCE.md). + -- 43.1+deref-hoist = 43.1 with shen-sources perf/typecheck-deref-hoist + -- (5596445): search-user-datatypes / by-hypothesis dereference once, + -- outside their loops, through new -h helpers. The native + -- search-user-datatypes hand port implements both shapes (it never + -- copies on deref); by-hypothesis(-h) are translated, not hand-ported. + ["shen.typecheck"] = { ["53f17a8525d2d3ab"] = "43.1, 43.1+deref-hoist" }, + ["shen.lookupsig"] = { ["7613493146348a37"] = "43.1, 43.1+deref-hoist" }, + ["shen.sigf"] = { ["9224dc17fb22e027"] = "43.1, 43.1+deref-hoist" }, + ["shen.search-user-datatypes"] = { ["f19996b77935e2cd"] = "43.1", ["bbdfd9a9f7dfc457"] = "43.1+deref-hoist" }, + ["shen.search-user-datatypes-h"] = { ["absent"] = "43.1", ["a3924a95ecff6737"] = "43.1+deref-hoist" }, + ["shen.show"] = { ["832effe9caab5eb7"] = "43.1, 43.1+deref-hoist" }, + ["shen.maxinfexceeded?"] = { ["effe20ceadf236e0"] = "43.1, 43.1+deref-hoist" }, + ["shen.deref"] = { ["108e6076d2b40cf4"] = "43.1, 43.1+deref-hoist" }, + ["shen.lazyderef"] = { ["6a71db925c09d82a"] = "43.1, 43.1+deref-hoist" }, + ["shen.newpv"] = { ["6b2ec5d6d1a0740e"] = "43.1, 43.1+deref-hoist" }, + ["shen.gc"] = { ["8bf58e06257f6ea8"] = "43.1, 43.1+deref-hoist" }, + ["shen.pvar?"] = { ["b9f8c2027d513bc8"] = "43.1, 43.1+deref-hoist" }, + ["shen.bind!"] = { ["a437a03592eeb09d"] = "43.1, 43.1+deref-hoist" }, + ["shen.bindv"] = { ["2dc4d6612453b0d5"] = "43.1, 43.1+deref-hoist" }, + ["shen.unwind"] = { ["6115d9d8ec8dff1a"] = "43.1, 43.1+deref-hoist" }, + ["shen.incinfs"] = { ["459195177ce076d3"] = "43.1, 43.1+deref-hoist" }, + ["shen.cut"] = { ["85bd1959adc59183"] = "43.1, 43.1+deref-hoist" }, + ["shen.unlock"] = { ["ab83443a7ddbe39e"] = "43.1, 43.1+deref-hoist" }, + ["shen.unlocked?"] = { ["0dc9377a64641254"] = "43.1, 43.1+deref-hoist" }, + ["shen.lock"] = { ["3f48989f248f0795"] = "43.1, 43.1+deref-hoist" }, + ["shen.fits?"] = { ["fa91134c44360b8c"] = "43.1, 43.1+deref-hoist" }, + ["shen.lzy="] = { ["07e5f098a2073b4a"] = "43.1, 43.1+deref-hoist" }, + ["shen.lzy=!"] = { ["19816edf8fae10a9"] = "43.1, 43.1+deref-hoist" }, + ["shen.occurs-check?"] = { ["7ed05eb3e8bc6b2b"] = "43.1, 43.1+deref-hoist" }, + ["when"] = { ["792bf299343da335"] = "43.1, 43.1+deref-hoist" }, + ["is"] = { ["5d0db39f0969e1b5"] = "43.1, 43.1+deref-hoist" }, + ["is!"] = { ["46de12012a59edad"] = "43.1, 43.1+deref-hoist" }, + ["bind"] = { ["2235ce58a13b088c"] = "43.1, 43.1+deref-hoist" }, + ["var?"] = { ["ed30fbf8c4b400a8"] = "43.1, 43.1+deref-hoist" }, + ["return"] = { ["8fec19a7c421b3f1"] = "43.1, 43.1+deref-hoist" }, + ["fork"] = { ["fb09c42dd1e554ad"] = "43.1, 43.1+deref-hoist" }, + ["findall"] = { ["ffb1cd75749d0cf1"] = "43.1, 43.1+deref-hoist" }, + ["call"] = { ["1a08e4ad48d6c247"] = "43.1, 43.1+deref-hoist" }, +} +M.KNOWN = KNOWN + +local function canon(form) + local rename, n, out = {}, 0, {} + local function walk(x) + if getmt(x) == Cons then + out[#out + 1] = "(" + while getmt(x) == Cons do walk(x[1]); x = x[2] end + if x ~= NIL then out[#out + 1] = "."; walk(x) end + out[#out + 1] = ")" + elseif getmt(x) == Symbol then + local nm = x.name + if nm:find("^[VWZ]%d+$") then + local r = rename[nm] + if not r then n = n + 1; r = "_" .. n; rename[nm] = r end + nm = r + end + out[#out + 1] = nm + elseif type(x) == "string" then + out[#out + 1] = string.format("%q", x) + elseif type(x) == "number" then + out[#out + 1] = string.format("%.17g", x) + elseif x == NIL then + out[#out + 1] = "()" + else + out[#out + 1] = tostring(x) + end + end + walk(form) + return table.concat(out, " ") +end + +-- two independent 32-bit polynomial hashes (plain doubles, no bit library) +local function digest(str) + local h1, h2 = 5381, 7 + for i = 1, #str do + local b = str:byte(i) + h1 = (h1 * 33 + b) % 4294967296 + h2 = (h2 * 131 + b) % 4294967296 + end + return string.format("%08x%08x", h1, h2) +end + +function M.fingerprint(form) return digest(canon(form)) end + +-- name -> defun form, for every (defun ...) in a KL source string +local function defuns_of(src) + local defs = {} + local SYM_defun = R.intern("defun") + for _, form in ipairs(R.read_all(src)) do + if getmt(form) == Cons and form[1] == SYM_defun and getmt(form[2]) == Cons + and getmt(form[2][1]) == Symbol then + defs[form[2][1].name] = form + end + end + return defs +end +M.defuns_of = defuns_of + +-- fingerprint of `name` in `defs`, or "absent" +function M.fingerprint_in(defs, name) + local form = defs[name] + return form and M.fingerprint(form) or "absent" +end + +-- problems (list of strings) for the reimplemented defuns of a kernel whose +-- sources are given as { ["t-star"] = defs, prolog = defs } +local function conformance_problems(defsets) + local problems = {} + for _, e in ipairs(REIMPLEMENTED) do + local file, name = e[1], e[2] + local fp = M.fingerprint_in(defsets[file], name) + local known = KNOWN[name] + if not (known and known[fp]) then + problems[#problems + 1] = string.format( + "kernel defun %s (%s.kl) does not match any version the native " + .. "typechecker implements (fingerprint %s)", name, file, fp) + end + end + return problems +end +M.conformance_problems = conformance_problems + -- --------------------------------------------------------------------------- -- NativeSig: declare wrapper -- --------------------------------------------------------------------------- @@ -375,10 +599,16 @@ function M.install(Pmod, Emod) if not tstar_src then error("typecheck_native: cannot locate t-star.kl") end + local prolog_src = read_kl("prolog") M.translate_errors = {} local n_ok, n_fail = 0, 0 M.n_ok, M.n_fail = 0, 0 -- 0 until ensure_drivers runs on the first typecheck local drivers_ready = false + -- Native-path problems found by ensure_drivers (nil = none): a kernel + -- defun the port reimplements has changed, or a driver calls a predicate + -- with no native definition. Either one routes every typecheck to the + -- kernel's KL typechecker (see typecheck_dispatch). + local native_problems = nil local function ensure_drivers() if drivers_ready then return end -- Translate into FRESH accumulators and commit `drivers_ready` (plus the @@ -389,24 +619,74 @@ function M.install(Pmod, Emod) -- native_typecheck over a half-filled NP. Re-running is safe: the driver -- source is fixed, so NP[name] writes are idempotent and the counters are -- local, not accumulated across attempts. - local ok, fail, errors = 0, 0, {} - for _, form in ipairs(R.read_all(tstar_src)) do - if getmt(form) == Cons and getmt(form[2][1]) == Symbol - and DRIVER_FNS[form[2][1].name] then - local fn, err = PC.translate_defun(form) - local name = form[2][1].name - if fn then - NP[name] = fn - ok = ok + 1 - else - errors[name] = err - fail = fail + 1 + local ok, fail, errors, problems = 0, 0, {}, {} + local tdefs = defuns_of(tstar_src) + local pdefs = prolog_src and defuns_of(prolog_src) or {} + if not prolog_src then + problems[#problems + 1] = "cannot locate prolog.kl to check the kernel" + end + for _, msg in ipairs(conformance_problems({ ["t-star"] = tdefs, prolog = pdefs })) do + problems[#problems + 1] = msg + end + + -- The driver set is the closure of DRIVER_FNS under "calls by name": + -- every predicate a translated driver calls must be a t-star.kl defun + -- (translated here too) or a hand port. So a kernel that splits a driver + -- into helpers (43.1 with the deref hoist adds shen.by-hypothesis-h) is + -- followed automatically, and one that calls something the port cannot + -- run is caught here, before any query, instead of as nil(...) mid-search. + local queue, queued = {}, {} + local function enqueue(name) + if not queued[name] then queued[name] = true; queue[#queue + 1] = name end + end + local roots = {} + for name in pairs(DRIVER_FNS) do roots[#roots + 1] = name end + table.sort(roots) + for _, name in ipairs(roots) do + if tdefs[name] then enqueue(name) + elseif ENTRY_FNS[name] then + problems[#problems + 1] = "t-star.kl has no defun " .. name + end + end + local added = {} + local qi = 1 + while qi <= #queue do + local name = queue[qi]; qi = qi + 1 + local fn, err, _, _, callees = PC.translate_defun(tdefs[name]) + if fn then + NP[name] = fn + ok = ok + 1 + else + errors[name] = err + fail = fail + 1 + end + local cs = {} + for c in pairs(callees or {}) do cs[#cs + 1] = c end + table.sort(cs) + for _, c in ipairs(cs) do + if not (HANDPORTED[c] or queued[c]) then + if tdefs[c] then + enqueue(c) + if not DRIVER_FNS[c] then added[#added + 1] = c end + else + problems[#problems + 1] = string.format( + "driver %s calls %s, which is neither a t-star.kl defun nor a " + .. "native hand port", name, c) + end end end end -- hand-ports (shen.lookupsig / shen.sigf, ...): also native-path only. install_handports() M.translate_errors = errors + for name, err in pairs(errors) do + problems[#problems + 1] = "cannot translate driver " .. name .. ": " + .. tostring(err):sub(1, 200) + end + M.drivers = queue + M.drivers_added = added + native_problems = (#problems > 0) and problems or nil + M.native_problems = native_problems n_ok, n_fail = ok, fail M.n_ok, M.n_fail = ok, fail drivers_ready = true @@ -444,14 +724,25 @@ function M.install(Pmod, Emod) -- search hits an untranslatable datatype predicate (NATIVE_MISS), the -- engine state is already restored by native_typecheck's pcall path — -- rerun the whole query on the legacy engine. + -- The same fallback covers a call to a predicate with no native + -- definition (E.native_miss) and a kernel that fails the conformance + -- check (native_problems): the answer then comes from the kernel's own + -- KL typechecker, and stderr says why, once per cause. + -- SHEN_TYPECHECK_NATIVE=require raises an error instead of falling back. local orig_typecheck_ref = nil -- resolved lazily: t-star loads before us local function typecheck_dispatch(expr, type_) ensure_drivers() -- translate the drivers on first typecheck (see above) - if n_fail == 0 and M.sigs_complete + if native_problems then + fallback_notice("problems", native_problems) + elseif n_fail == 0 and M.sigs_complete and P.GLOBALS["shen.*spy*"] ~= true then local ok, r = pcall(native_typecheck, expr, type_) if ok then return r end - if r ~= M.NATIVE_MISS then error(r, 0) end + if E.is_native_miss(r) then + fallback_notice("miss:" .. tostring(r.name), { tostring(r) }) + elseif r ~= M.NATIVE_MISS then + error(r, 0) + end end return orig_typecheck_ref(expr, type_) end