diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5c01db..bcff775 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,11 @@ jobs: # Tier 2: canonical kernel certification. Always run for real. - name: Kernel certification (make certify) - run: make certify LUA=luajit + run: SHEN_TYPECHECK_NATIVE=require make certify LUA=luajit + + # The native typechecker and Prolog engine against recorded results. + - name: Golden typecheck corpus (make golden) + run: SHEN_TYPECHECK_NATIVE=require make golden LUA=luajit coverage: name: Coverage (best-effort) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb8004..ce3beb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,71 @@ Notable changes to shen-lua. Release notes for earlier versions are under ## [Unreleased] +### Changed + +- **Startup and compile cost.** `variable?` no longer throws on a non-symbol; the + fasl records a global push as a delta (SHENFASL9), the reader has native paths for + plain tokens, macroexpansion keeps unchanged cells, the stdlib datatypes replay from + the signature sidecar (SHENSIG2), and a replayed `declare` skips the variancy query + when the compile run found no old type. The GC pause adapts past a small live heap + and stops adapting once the host sets its own; the default stepmul is 1000, which + trades work for memory (a string-building loop: +7% instructions, 78 to 47 MB). +- **Calls and closures.** Fixed-arity `APPn`/`CURn` helpers, uncurried lambda groups, + closed lambdas built once, hot helpers bound as locals, count sinking. `(= F G)` can + now be true for two evaluations of the same closed lambda. +- **Lists, vectors, strings, Prolog.** `hd`/`tl` read directly where `(cons? E)` + proves a cons; `address->` stores with `rawset`; a self-tail accumulator is a double + from the start; native `shen.mod` below 2^53; the Prolog engine reuses stack + segments and has loop-free deref and unify. +- **Kernel cache key** covers every codegen switch: `SHEN_UNCURRY`, + `SHEN_HOIST_LAMBDA`, `SHEN_SINK_COUNT` and `SHEN_FREEZE_CONT` as well as `SHEN_TRMC`. +- **Calling a function value from Lua.** A compiled lambda called with fewer arguments + than it takes returns the partial application over the ones it got, wherever the host + got it (`shen.eval`, `shen.value`, a callback argument, an element of a list): `f(1)(2)` + and `f(1, 2)` are the same. It tests its last parameter for nil on entry, since nil is + never a Shen value. **Edge:** a host function that returns nothing (nil) into Shen, + whose result is then passed as a lambda's last argument, gets that lambda's partial + application rather than a call. Through the Lua bridge (`lua.call` and the + marshalers) this cannot happen, since a nil result becomes `()`; only a function + installed directly in `shen.prims.F` could do it. `shen.prims.F[name]`, a defun's own + function, still takes exactly its arity when called raw, as before. + Also deliberate, from the host only: a raw call with too many arguments, + `add(1, 2, 3)` for a two-parameter lambda, ignores the extra and gives 3, where 0.12.0 + returned a function. +- **CI and gates.** `make certify` fails when a test fails, the suite stops early or + fewer tests run than the kernel release has (154 for 43.2); it used to exit 0 whatever + happened. CI runs it with `SHEN_TYPECHECK_NATIVE=require`, and runs the new + `make golden` (typechecker and Prolog results, y-combinator inference count). A spec + file missing from `scripts/run-tests.lua` fails the run. +- **Lua 5.5** boots: its collector options changed. + +### Fixed (behaviour changes) + +- A test that is not a boolean raises: the test of `if` and `cond`, both operands of + `and`/`or`, and the argument of `not`. `(if 1 a b)` used to give `a`, and `(not 5)` + compiled gave `false`. A predicate with too few arguments is not a boolean. + **Limit:** a call in tail position as the last operand of `and`/`or`, in a defun or a + lambda, stays a tail call and is not checked itself; a test of the function's result + is what raises. So `(define f X -> (and true (g X)))` with `g` returning 5 gives + `(f 5)` = 5 unless the caller tests it. shen-go does the same; Shen/JIT and + Shen/Scheme raise. +- `(str "a")` gives `"\"a\""`, quoted, as on the other ports. +- `absvector` rejects a length that is not a non-negative integer; `address->` and + `<-address` reject an index outside the vector; `set`/`value` raise on a non-symbol; + `hash` raises on a bound that is not a positive integer; `close` is idempotent; + `(get-time unix)` is the Unix time; `(eval-kl sym)` gives the symbol. +- `(thaw F)` for a compiled lambda that takes arguments gives a partial application, as + before uncurrying; it no longer runs the body with nil. For a one-parameter lambda that + is the lambda itself; 0.12.0 ran its body with nil. +- A compile that raises, or a compile attempt the compiler throws away, leaves no state + behind for later code. One such case read an undeclared local as nil and returned a + wrong answer with no error (count sinking on a branch compiled twice). +- A host's own Lua function is called as given whatever its chunk is named; a host's + yield through a stack segment keeps its exact values; on PUC Lua a hook set during a + job does not leak into the next. +- A fasl replays a macro's `(set ...)` even when the global already held that value + when the file was compiled. + ## [0.12.0] - 2026-11-05 ### Changed diff --git a/Makefile b/Makefile index 65a67b3..c4eb5c6 100644 --- a/Makefile +++ b/Makefile @@ -8,13 +8,14 @@ # LUA selects the interpreter (default: luajit, the project's primary host). LUA ?= luajit -.PHONY: all test certify test-all coverage help +.PHONY: all test certify golden test-all coverage help all: test help: @echo "make test - run the port-authored spec suite (fast)" @echo "make certify - run the canonical Shen kernel certification suite" + @echo "make golden - typechecker and Prolog results against the golden corpus (LuaJIT)" @echo "make test-all - run both tiers" @echo "make coverage - run luacov over the port specs (best-effort)" @@ -29,6 +30,10 @@ test: certify: $(LUA) run-kernel-tests.lua +# golden: 27 typecheck and Prolog results, and the y-combinator's inference count. +golden: + $(LUA) bench/golden_typecheck.lua compare + # test-all: both tiers — our specs plus the canonical certification. test-all: test certify diff --git a/bench/golden_typecheck.lua b/bench/golden_typecheck.lua index 3bd6693..ddf8e13 100644 --- a/bench/golden_typecheck.lua +++ b/bench/golden_typecheck.lua @@ -27,8 +27,8 @@ ffi.cdef[[int chdir(const char*); char *getcwd(char*, size_t);]] local cwdbuf = ffi.new("char[4096]") assert(ffi.C.getcwd(cwdbuf, 4096) ~= nil) local ROOT = ffi.string(cwdbuf) -assert(ffi.C.chdir("../cl-source/ShenOSKernel-42/tests") == 0, - "tests dir not found (expected ../cl-source/ShenOSKernel-42/tests)") +local TESTS = os.getenv("SHEN_TESTS_DIR") or "tests" +assert(ffi.C.chdir(TESTS) == 0, "tests dir not found: " .. TESTS) -- same environment fixes typecheck_alloc.lua needs local mf = P.F["shen.macros"] @@ -136,10 +136,13 @@ local GOLDEN = ROOT .. "/" .. (arg[0]:gsub("[^/]*$", "")) .. "golden_typecheck.g local mode = arg[1] or "compare" local results = {} +local INFERENCES for _, entry in ipairs(CORPUS) do local name, fn = entry[1], entry[2] + local infs0 = P.F["inferences"]() local ok, res = pcall(fn) results[name] = ok and serialize(res) or ("#") + if name == "tc-ycombinator" then INFERENCES = P.F["inferences"]() - infs0 end end if mode == "capture" then @@ -171,6 +174,11 @@ elseif mode == "compare" then pass = pass + 1 end end + -- the kernel's own count for the y-combinator: engines must agree with it too + if INFERENCES ~= 431741 then + fail = fail + 1 + io.write("FAIL tc-ycombinator inferences: ", tostring(INFERENCES), ", wanted 431741\n") + end io.write(string.format("golden compare: %d pass, %d fail\n", pass, fail)) os.exit(fail == 0 and 0 or 1) else diff --git a/boot.lua b/boot.lua index 580580d..14676b8 100644 --- a/boot.lua +++ b/boot.lua @@ -96,16 +96,51 @@ do -- pause=200), and most of that churn is short-lived list cells. Raising -- the pause to 400 (heap may grow to 4x live before a full cycle) cuts -- suite CPU ~15-20% for about 2x peak RSS (30MB -> 60MB on that suite). + -- (Past a small heap the pause adapts, below.) + -- A stepmul of 1000 finishes each cycle sooner, so less garbage allocated + -- during the cycle counts as live toward the next threshold. Same + -- instructions and CPU; peak footprint on the kernel suite 186 -> 162 MB, on + -- nrev 40-56 -> 38 MB. Each GC step does more work at once, which an embedder + -- that cares about pause length may want to set back. + -- A larger pause is not a free win: 800,1000 costs hof 1132 MB (from 130) and + -- the kernel suite 281 MB, for nrev -4% instructions and -20% CPU. -- SHEN_GC=off keeps the host's defaults (embedders that manage the GC -- themselves should set it); SHEN_GC="pause[,stepmul]" sets explicit - -- values (e.g. SHEN_GC=800,100 buys another ~10% on batch runs at ~110MB; - -- SHEN_GC=200 is LuaJIT's default pause). + -- values (SHEN_GC=200,200 is LuaJIT's default). local gc = os.getenv("SHEN_GC") if gc ~= "off" then local pause, stepmul if gc and gc ~= "" then pause, stepmul = gc:match("^(%d+),?(%d*)$") end + -- Lua 5.5 removed "setpause" and "setstepmul" for "param" (and has no newproxy) + if _VERSION == "Lua 5.5" then + collectgarbage("param", "pause", tonumber(pause) or 400) + collectgarbage("param", "stepmul", tonumber(stepmul ~= "" and stepmul) or 1000) + else collectgarbage("setpause", tonumber(pause) or 400) - if stepmul and stepmul ~= "" then collectgarbage("setstepmul", tonumber(stepmul)) end + if not pause and newproxy then + -- 400 is cheap on a small live heap and wasteful on a big one: the 186 MB + -- the kernel suite peaked at was 36 MB live. So at the end of each cycle + -- aim for live + max(live, 48 MB) of heap, within pause 150..400. A small + -- heap, where the allocation-heavy benchmarks live, stays at 400. + -- A pause that is not the one set here was set by the host: leave it, and + -- stop adapting. + local GROW_KB = 48 * 1024 + local last = 400 + local function arm() + local sentinel = newproxy(true) + getmetatable(sentinel).__gc = function() + local want = 100 + 100 * GROW_KB / collectgarbage("count") + want = math.floor(want > 400 and 400 or want < 150 and 150 or want) + local current = collectgarbage("setpause", want) + if current ~= last then collectgarbage("setpause", current); return end + last = want + arm() + end + end + arm() + end + collectgarbage("setstepmul", tonumber(stepmul ~= "" and stepmul) or 1000) + end end end @@ -287,8 +322,11 @@ end local function cache_key() local h = fnv1a(jit and (jit.version .. jit.arch) or _VERSION) h = fnv1a(CACHE_FORMAT .. table.concat(FILES, ","), h) - -- codegen switches that change compiled output (SHEN_TRMC -> compiler.lua C.TRMC) - h = fnv1a("trmc=" .. tostring(os.getenv("SHEN_TRMC") ~= "off"), h) + -- codegen switches that change compiled output (compiler.lua): each is read as + -- "off" or not there, so a cache built one way is not reused the other way + for _, sw in ipairs({ "SHEN_TRMC", "SHEN_UNCURRY", "SHEN_HOIST_LAMBDA", "SHEN_SINK_COUNT", "SHEN_FREEZE_CONT" }) do + h = fnv1a(sw .. "=" .. tostring(os.getenv(sw) ~= "off"), h) + end for _, m in ipairs({ "compiler", "runtime", "prims" }) do h = fnv1a(module_source(m), h) end @@ -802,7 +840,11 @@ end -- Known: (destroy ...) at the REPL between loads is not in the key. -- SHEN_FASL=off disables; SHEN_FASL_DIR overrides ~/.cache/shen-lua-fasl; -- SHEN_FASL_DEBUG=1 logs hits/misses to stderr. -local FASL_FORMAT = "SHENFASL7" -- 7: a nested load run inside a chunk is no +local FASL_FORMAT = "SHENFASL9" -- 9: "d" records whether variancy found an old + -- type, so replay can skip the query otherwise; + -- 8: "h" pushes onto a global instead of + -- storing its whole list at each step; + -- 7: a nested load run inside a chunk is no -- longer spliced into the outer fasl (#92); -- 6: "dv" (shen.*datatypes* by name) replaces -- "dt" (re-run shen.process-datatype); @@ -859,7 +901,9 @@ end -- | E\n #bytes\n bytes per-form value/type echo (stoutput) -- | A\n shen.*lambdatable* delta (entries -- rebuilt by name via shen.lambda-entry) --- | G\n }* (set ...) outside any chunk +-- | G\n (set ...) outside any chunk +-- | H\n }* ditto, when the value is the global's +-- old value with one element pushed -- narity\n {ar SP name\n}* kbase\n nkdata\n entries gensym\n local function fasl_serialize(rec, arity0) if rec.uncacheable then error(rec.uncacheable) end @@ -872,6 +916,7 @@ local function fasl_serialize(rec, arity0) parts[#parts+1] = "D\n" kdata_ser(r.name, parts) kdata_ser(r.typ, parts) + kdata_ser(r.old == true, parts) elseif r.k == "m" then parts[#parts+1] = "M\n" kdata_ser(r.name, parts) @@ -902,6 +947,10 @@ local function fasl_serialize(rec, arity0) -- mirrors the "C" chunk framing — no trailing separator, the next -- record's kind letter starts immediately after the bytes. parts[#parts+1] = "E\n" .. #r.bytes .. "\n" .. r.bytes + elseif r.k == "h" then + parts[#parts+1] = "H\n" + kdata_ser(r.name, parts) + kdata_ser(r.head, parts) else -- "g" parts[#parts+1] = "G\n" kdata_ser(r.name, parts) @@ -964,8 +1013,8 @@ local function fasl_parse(data, pos) recs[i] = { k = "c", name = nm, dump = data:sub(pos, pos + len - 1) } pos = pos + len elseif k == "D" then - local v = de_n(2); if not v then return nil end - recs[i] = { k = "d", name = v[1], typ = v[2] } + local v = de_n(3); if not v then return nil end + recs[i] = { k = "d", name = v[1], typ = v[2], old = v[3] } elseif k == "M" then local v = de_n(1); if not v then return nil end recs[i] = { k = "m", name = v[1] } @@ -995,6 +1044,9 @@ local function fasl_parse(data, pos) elseif k == "G" then local v = de_n(2); if not v then return nil end recs[i] = { k = "g", name = v[1], val = v[2] } + elseif k == "H" then + local v = de_n(2); if not v then return nil end + recs[i] = { k = "h", name = v[1], head = v[2] } else return nil end end local na = tonumber(line() or ""); if not na then return nil end @@ -1025,7 +1077,27 @@ local function fasl_replay(cached) P.load_chunk(r.dump, r.name)() elseif r.k == "d" then -- through the live F["declare"] so engine sig-table wrappers see it - P.F["declare"](r.name, r.typ) + local declared = r.old + local t = P.GLOBALS["shen.*sigf*"] + while R.is_cons(t) and not declared do + declared = R.is_cons(t[1]) and t[1][1] == r.name + t = t[2] + end + if declared then + P.F["declare"](r.name, r.typ) + else + -- declare's check that a new type agrees with the old one, which is + -- a System S query on the kernel's Prolog: 70M instructions each, 80% + -- of the kernel suite on a warm fasl. It only warns, and with no old + -- type there is nothing to warn about: neither a signature, nor a type + -- from a datatype rule for (fn F), which the compile run recorded in + -- r.old and the key's datatype names hold to this load. + local variancy = P.F["shen.variancy"] + P.F["shen.variancy"] = function() return false end + local ok, err = pcall(P.F["declare"], r.name, r.typ) + P.F["shen.variancy"] = variancy + if not ok then error(err, 0) end + end elseif r.k == "m" then -- the macro's defun chunk replayed above; rebuild the (name . fn) pair local fn = P.F[r.name.name] @@ -1086,6 +1158,9 @@ local function fasl_replay(cached) if not P.HUSH_LOAD_ECHO then P.F["pr"](r.bytes, P.GLOBALS["*stoutput*"]) end + elseif r.k == "h" then + local cur = P.GLOBALS[r.name.name] + P.F["set"](r.name, R.cons(r.head, cur == nil and R.NIL or cur)) else -- "g" P.F["set"](r.name, r.val) end @@ -1177,8 +1252,18 @@ local function install_fasl() end wrap_recorded("declare", function(rec, name, typ) - return { k = "d", name = name, typ = typ } + rec.declaring = { k = "d", name = name, typ = typ } + return rec.declaring end) + -- shen.variancy reaches this only when (fn F) already has a type + do + local variants = F["shen.variants?"] + F["shen.variants?"] = function(...) + local rec = FASL_STACK[#FASL_STACK] + if rec and rec.declaring then rec.declaring.old = true end + return variants(...) + end + end wrap_recorded("shen.record-macro", function(rec, name, fn) return { k = "m", name = name } end) @@ -1218,7 +1303,8 @@ local function install_fasl() return { k = "p", x = x, pointer = pointer, y = y } end) wrap_recorded("set", function(rec, name, val) - local nm = R.is_symbol(name) and name.name or tostring(name) + if not R.is_symbol(name) then return nil end -- the primitive will raise + local nm = name.name -- the gensym counter churns on every expansion-time gensym; the replay -- fast-forward (max) covers it without hundreds of noise records if nm == "shen.*gensym*" then return nil end @@ -1279,6 +1365,20 @@ local function install_fasl() for i = #rev, 1, -1 do names = R.cons(rev[i], names) end return { k = "dv", global = name, names = names } end + -- shen.record-kl sets shen.*userdefs* at every define, and storing the whole + -- list each time made a fasl quadratic in its definitions (1500 defines: + -- 1.1M list cells, 12 of its 14 MB). The recorder runs BEFORE the original + -- set, so the global still holds the old value: store only what changed. + -- A set to the value already there records nothing only if this fasl has recorded + -- that value last: the session replaying it need not hold what this one held. + local old = R.is_symbol(name) and P.GLOBALS[nm] + rec.last_set = rec.last_set or {} + local seen = rec.last_set[nm] + rec.last_set[nm] = val + if seen ~= nil and rawequal(seen, val) and rawequal(old, val) then return nil end + if old and R.is_cons(val) and rawequal(val[2], old) then + return { k = "h", name = name, head = val[1] } + end return { k = "g", name = name, val = val } end) @@ -1428,20 +1528,26 @@ local STDLIB_LOADED = false -- replays them into shen.*sigf* (and the native typechecker's signature -- table) in the original order. SHEN_KERNEL_CACHE=off disables it with the -- kernel cache. -local SIGS_FORMAT = "SHENSIG1" +-- +-- The file also holds the record stream of (stlib.initialise-datatypes), in the +-- user fasl's format: its two datatypes run the whole rule compiler and were +-- 40% of a warm boot's instructions. The records need the fasl recorder, so a +-- boot with SHEN_FASL=off stores none and runs the phase. +local SIGS_FORMAT = "SHENSIG2" -- 2: datatype records after the counters local function stlib_sigs_path() if P.KERNEL_CACHE_DATA then return nil end -- single-file bundle local p = cache_path() return p and (p .. ".stlib") or nil end -local function write_sigs(path, key, decl, counters) +local function write_sigs(path, key, decl, counters, datatypes) local parts = { SIGS_FORMAT, "\n", key, "\n", tostring(#decl), "\n" } for _, e in ipairs(decl) do parts[#parts+1] = e.name .. "\n" .. #e.dump .. "\n" .. e.dump kdata_ser(e.typ, parts) end parts[#parts+1] = tostring(counters.gensym) .. "\n" .. tostring(counters.infs) .. "\n" + parts[#parts+1] = datatypes or "" local tmp = path .. ".tmp" local fh = open_for_write(tmp) if not fh then return end @@ -1478,7 +1584,50 @@ local function read_sigs(path, key) if not ok then return nil end local gensym = tonumber(line() or ""); if not gensym then return nil end local infs = tonumber(line() or ""); if not infs then return nil end - return { decl = decl, gensym = gensym, infs = infs } + local datatypes + if pos <= #data then + datatypes = fasl_parse(data, pos) + if not datatypes then return nil end + end + return { decl = decl, gensym = gensym, infs = infs, datatypes = datatypes } +end + +-- The sidecar, read once per boot: both phases below use it. +local STLIB_SIGS, STLIB_SIGS_READ +local function stlib_cached() + if not STLIB_SIGS_READ then + STLIB_SIGS_READ = true + local path = stlib_sigs_path() + if path then + STLIB_SIGS = read_sigs(path, kernel_key() .. "|" .. SIGS_FORMAT) + end + end + return STLIB_SIGS +end + +-- (stlib.initialise-datatypes), from its records when the sidecar has them, +-- else for real, recording them for write_sigs (stlib_types, the next phase +-- that matters) when the fasl recorder is live. +local STLIB_DT_BLOB +local function stlib_datatypes() + local run = P.F["stlib.initialise-datatypes"] + local cached = stlib_cached() + if cached and cached.datatypes then return fasl_replay(cached.datatypes) end + if cached or not FASL_INSTALLED or not stlib_sigs_path() then return run() end + local rec = { n = 0, in_chunk = false } + local arity0 = {} + for k, v in pairs(C.ARITY) do arity0[k] = v end + FASL_STACK[#FASL_STACK + 1] = rec + local saved_rec, saved_nokdata = P.FASL_REC, C.NO_KDATA + P.FASL_REC = rec + C.NO_KDATA = true -- recorded chunks must be relocatable + local ok, err = pcall(run) + FASL_STACK[#FASL_STACK] = nil + P.FASL_REC = saved_rec + C.NO_KDATA = saved_nokdata + if not ok then error(err, 0) end + local sok, blob = pcall(fasl_serialize, rec, arity0) + if sok then STLIB_DT_BLOB = blob end end local function stlib_types(verbose) @@ -1488,7 +1637,7 @@ local function stlib_types(verbose) local key = kernel_key() .. "|" .. SIGS_FORMAT local tn = package.loaded["typecheck_native"] local on_sig = tn and tn.record_sig or nil - local cached = read_sigs(path, key) + local cached = stlib_cached() if cached then -- load every dump before running any, so a foreign-arch file falls back -- to the real declares with nothing half-applied @@ -1529,7 +1678,7 @@ local function stlib_types(verbose) write_sigs(path, key, out, { gensym = type(P.GLOBALS["shen.*gensym*"]) == "number" and P.GLOBALS["shen.*gensym*"] or 0, infs = type(P.GLOBALS["shen.*infs*"]) == "number" and P.GLOBALS["shen.*infs*"] or 0, - }) + }, STLIB_DT_BLOB) end end @@ -1563,6 +1712,7 @@ local function load_stdlib(verbose) local pok, perr = pcall(function() for _, ph in ipairs(STDLIB_PHASES) do if ph == "types" then stlib_types(verbose) + elseif ph == "datatypes" then stlib_datatypes() else P.F["stlib.initialise-" .. ph]() end end end) diff --git a/compiler.lua b/compiler.lua index c6c8204..c9fef19 100644 --- a/compiler.lua +++ b/compiler.lua @@ -371,6 +371,7 @@ for _, n in ipairs({ "difference", "union", "intersection", "occurrences", "subst", "==", "fail", "get", "put", "unput", "hdstr", "string->symbol", "pr", }) do LEAF[n] = true end +local sink_branch -- fwd (count sinking) local function nt_call(fexpr, argstr, n) NTCOUNT = NTCOUNT + 1 if CNT then @@ -422,6 +423,31 @@ local function unknown_call(name, argstr, n, tail) return "APP(" .. symlit(name) .. ", " .. argstr .. ")" end C.MAX_CALLN = MAX_CALLN +-- APP with a fixed argument count (prims.lua), for a call through a value +local MAX_APPN = 4 +C.MAX_APPN = MAX_APPN +local function app_name(n) + if n >= 1 and n <= MAX_APPN then return "APP" .. n end + return "APP" +end +-- A one-parameter lambda needs no MKFUN where APP can recover its arity from +-- debug.getinfo (is_raw_lambda in prims.lua); Lua 5.1 has no nparams. +local LAM1_RAW = debug and debug.getinfo and debug.getinfo(print, "u").nparams ~= nil +-- A compiled lambda called with fewer arguments than it takes sees nil in its last +-- parameter: nil is never a Shen value. It returns the partial application over what it +-- got (UPART, prims.lua) rather than run its body with nil. Compiled calls pass all the +-- arguments or go through APP; this is for a host calling it raw, and for thaw. +local function upart_guard(params, n) + local last = params:match("([%w_]+)%s*$") + local given = params:match("^(.*),%s*[%w_]+%s*$") + return "if " .. last .. " == nil then local p = UPART(" .. n + .. (given and (", " .. given) or "") .. "); return p end " +end +local function lam1(params, body) + local fn = "function(" .. params .. ") " .. upart_guard(params, 1) .. body .. " end" + if LAM1_RAW then return fn end + return "MKFUN(1, " .. fn .. ")" +end -- ------------------------------------------------------------------ -- atom / literal compilation @@ -486,11 +512,18 @@ end -- in scope). Self-calls use `impl` so recursion does not hash F at all. local IN_IMPL = false local DEFUN_NAME = nil +-- The body being compiled is a control form in value position, called and +-- returned from as a helper (see cexpr), so nothing in it is a tail call of the +-- enclosing function. VALUE_NEXT asks hoist_kc to mark the body it compiles. +local VALUE_BODY, VALUE_NEXT = false, false +local IN_FN_BODY = false local FREF_LIST = {} local FREF_MAP = {} local function callee_ref(name) if not IN_IMPL then return ftab_ref(name) end if DEFUN_NAME and name == DEFUN_NAME then return "impl" end + -- the fall-through clause of a define: looked up only if it runs + if name == "shen.f-error" then return ftab_ref(name) end local ln = FREF_MAP[name] if ln then return ln end ln = "f" .. (#FREF_LIST + 1) @@ -667,6 +700,60 @@ local function chain_key(form, env) return (h.name == "hd" and "h" or "t") .. k, d + 1 end +-- Proven conses. A test (cons? E), E a chain rooted at a bound variable, proves +-- E is a cons in the code the test dominates, so hd/tl of E there need no check +-- of their own: E[1] / E[2] instead of an HD/TL call. Pattern-match bodies are +-- mostly this shape. It saves an interpreted call per access, and on a trace it +-- keeps recording from stopping at the HD or is_cons root trace, which made +-- TRMC builders' traces come out differently from run to run (nrev: 185 billion +-- or 287 billion instructions). Variables never change in a dominated region: a +-- Lua name is bound once, and the one reassignment, of a self-tail loop's +-- params, is followed by `goto tco` and the test runs again. Conses are +-- immutable as to being conses, and HD/TL ignore a redefined hd/tl on a cons +-- already, so nothing observable changes. Keys are chain_key's. +local PROVEN = {} +local function add_cons_facts(test, env, into) + if not is_cons(test) then return end + local h = test[1] + if not (is_symbol(h) and not env[h.name]) then return end + local rest = test[2] + if not (is_cons(rest)) then return end + if h.name == "cons?" and rest[2] == NIL and C.ARITY["cons?"] == 1 then + local k = chain_key(rest[1], env) + if k then into[k] = true end + elseif h.name == "and" and is_cons(rest[2]) and rest[2][2] == NIL then + add_cons_facts(rest[1], env, into) + add_cons_facts(rest[2][1], env, into) + end +end +-- f(...) with test's facts added to PROVEN. A flat table with an undo list: a +-- then-spine nests one scope per pattern test, past Lua's 100 __index links. +local function with_cons_facts(test, env, f, ...) + local facts = {} + add_cons_facts(test, env, facts) + local added = {} + for k in pairs(facts) do + if not PROVEN[k] then PROVEN[k] = true; added[#added + 1] = k end + end + local r = f(...) + for i = 1, #added do PROVEN[added[i]] = nil end + return r +end +function C.proven_count() + local n = 0 + for _ in pairs(PROVEN) do n = n + 1 end + return n +end +local function proven_cons(form, env) + local k = chain_key(form, env) + return k ~= nil and PROVEN[k] == true +end +-- field i of a cons-valued Lua expression +local function cell_field(expr, i) + if expr:match("^[%a_][%w_]*$") then return expr .. "[" .. i .. "]" end + return "(" .. expr .. ")[" .. i .. "]" +end + -- Bind one computed step to a fresh local (or PT slot past the local budget). -- Returns the reference string, or nil if no slot is available. local function chain_slot(expr) @@ -693,7 +780,12 @@ local function chain_materialize(form, env) local inner = chain_materialize(form[2][1], env) -- HD/TL helpers (see ccall): same is_cons check as F["hd"]/F["tl"], but a -- direct ENV lookup rather than an F-table hash at every pattern step. - local expr = (form[1].name == "hd" and "HD(" or "TL(") .. inner .. ")" + local expr + if proven_cons(form[2][1], env) then + expr = cell_field(inner, form[1].name == "hd" and 1 or 2) + else + expr = (form[1].name == "hd" and "HD(" or "TL(") .. inner .. ")" + end local name = chain_slot(expr) if not name then return expr end -- slot budget exhausted: stay inline CCACHE[key] = name @@ -928,7 +1020,7 @@ local function order_free(form, env) return true -- number/string/boolean/(), symbol literal or local variable end -local function curried_call(form, env, tail) +local function curried_call(form, env, tail, last) local args = {} local cur = form while is_cons(cur) and is_cons(car(cur)) do @@ -937,29 +1029,53 @@ local function curried_call(form, env, tail) table.insert(args, 1, car(rest)) cur = car(cur) end - -- cur must be (fn SYM): `fn` not rebound, SYM a symbol literal - if not (is_cons(cur) and is_symbol(car(cur)) and car(cur).name == "fn" - and not env["fn"]) then + if not is_cons(cur) then return nil end + local chead, rest = car(cur), cdr(cur) + if not (is_symbol(chead) and is_cons(rest) and cdr(rest) == NIL) then return nil end + local gref + if chead.name == "fn" and not env["fn"] then + -- (fn SYM): SYM a symbol literal + local sym = car(rest) + if not (is_symbol(sym) and not env[sym.name]) then return nil end + if #args < 1 or #args > MAX_CALLN then return nil end + for i = 2, #args do + if not order_free(args[i], env) then return nil end + end + gref = callee_ref("fn") .. "(" .. symlit(sym.name) .. ")" + elseif env[chead.name] then + -- a call through a local variable, ((((G a1) a2) ... an)): Shen's + -- (G a1 ... an) for a G it knows nothing about. G and a1 go first in + -- both forms, whatever a1 is. + table.insert(args, 1, car(rest)) + if #args < 2 or #args > MAX_CALLN then return nil end + gref = env[chead.name] + local n = #args + local free = true + for i = 2, n do free = free and order_free(args[i], env) end + if not free then + -- only the last argument may do anything: the earlier ones are read + -- twice, and PRE needs them before it runs + for i = 1, n - 1 do + if not order_free(args[i], env) then return nil end + end + local cs = {} + for i = 1, n - 1 do cs[i] = cexpr(args[i], env) end + local head = gref .. ", " .. table.concat(cs, ", ") + local a = head .. ", PRE" .. n .. "(" .. head .. "), " .. (last or cexpr(args[n], env)) + if not tail then return nt_call("CUR" .. n .. "P", a, n + 2) end + return "CUR" .. n .. "P(" .. a .. ")" + end + else return nil end - local rest = cdr(cur) - if not (is_cons(rest) and cdr(rest) == NIL) then return nil end - local sym = car(rest) - if not (is_symbol(sym) and not env[sym.name]) then return nil end local n = #args - if n < 1 or n > MAX_CALLN then return nil end - for i = 2, n do - if not order_free(args[i], env) then return nil end - end - local fnref = callee_ref("fn") local cargs = {} - for i = 1, n do cargs[i] = cexpr(args[i], env) end + for i = 1, n - 1 do cargs[i] = cexpr(args[i], env) end + cargs[n] = last or cexpr(args[n], env) if not tail then - return nt_call("CUR" .. n, fnref .. "(" .. symlit(sym.name) .. "), " - .. table.concat(cargs, ", "), n + 1) + return nt_call("CUR" .. n, gref .. ", " .. table.concat(cargs, ", "), n + 1) end - return "CUR" .. n .. "(" .. fnref .. "(" .. symlit(sym.name) .. "), " - .. table.concat(cargs, ", ") .. ")" + return "CUR" .. n .. "(" .. gref .. ", " .. table.concat(cargs, ", ") .. ")" end -- The form whose call cexpr_tail is compiling: the body of a lambda or @@ -973,6 +1089,36 @@ local function cexpr_tail(form, env) return s end +-- what a test can skip its boolean check for (see bc) +local BOOLEAN_FORMS = {} +for _, n in ipairs({ + "not", "cons?", "empty?", "number?", "string?", "symbol?", "boolean?", "absvector?", + "integer?", "variable?", "tuple?", "vector?", "shen.+string?", "shen.+vector?", + "shen.pvar?", "shen.ccons?", "shen.unlocked?", "shen.bc", +}) do BOOLEAN_FORMS[n] = 1 end +for _, n in ipairs({ "=", "==", "<", ">", "<=", ">=", "and", "or", "element?", "shen.hds=?" }) do + BOOLEAN_FORMS[n] = 2 +end +-- only a saturated call: a predicate with too few arguments is a closure +local function gives_boolean(form, env) + if form == true or form == false then return true end + if not is_cons(form) then return false end + local h = car(form) + if not (is_symbol(h) and not env[h.name]) then return false end + local want = BOOLEAN_FORMS[h.name] + return want ~= nil and #to_array(cdr(form)) == want +end + +-- cexpr_tail for the whole body of a lambda or a freeze: whatever enclosed it, its +-- tail is a tail call of a function of its own (see leave_as_tail_call) +local function fn_body_tail(form, env) + local saved_fn, saved_value = IN_FN_BODY, VALUE_BODY + IN_FN_BODY, VALUE_BODY = true, false + local s = cexpr_tail(form, env) + IN_FN_BODY, VALUE_BODY = saved_fn, saved_value + return s +end + -- compile a call (F a1..an) in expression position; `tail`: it is the -- value of a Lua `return` (ctail), so it is not counted (nt_call) local function ccall(form, env, tail) @@ -1089,12 +1235,16 @@ local function ccall(form, env, tail) return "type(" .. argstr .. ")==\"boolean\"" end if name == "not" and ar == 1 then - return "not (" .. argstr .. ")" + if gives_boolean(args[1], env) then return "not (" .. argstr .. ")" end + return "not BC(" .. argstr .. ")" end + if name == "shen.bc" and ar == 1 then return "BC(" .. argstr .. ")" end if name == "hd" and ar == 1 then + if proven_cons(args[1], env) then return cell_field(argstr, 1) end return "HD(" .. argstr .. ")" end if name == "tl" and ar == 1 then + if proven_cons(args[1], env) then return cell_field(argstr, 2) end return "TL(" .. argstr .. ")" end if name == "thaw" and ar == 1 then @@ -1177,7 +1327,7 @@ local function ccall(form, env, tail) return unknown_call(name, argstr, #args, tail) end else - local cur = curried_call(form, env, tail) + local cur = curried_call(form, env, tail, #cargs == 1 and cargs[1] or nil) if cur and not (hoist and NTCOUNT ~= n0) then return cur end -- head is a bound variable or a compound expression -> generic apply local hv = cexpr(head, env) @@ -1185,11 +1335,12 @@ local function ccall(form, env, tail) hv = hoist_args(hv) argstr = table.concat(cargs, ", ") end + local app = app_name(#args) if not tail then - return nt_call("APP", hv .. (#args > 0 and (", " .. argstr) or ""), #args + 1) + return nt_call(app, hv .. (#args > 0 and (", " .. argstr) or ""), #args + 1) end if #args == 0 then return "APP(" .. hv .. ")" end - return "APP(" .. hv .. ", " .. argstr .. ")" + return app .. "(" .. hv .. ", " .. argstr .. ")" end end @@ -1274,6 +1425,29 @@ end -- partials / variables, arity mismatches, shadowed name) keeps ordinary -- codegen and still works: the loop only replaces the tail self-call; the -- function value installed in F is unchanged. +-- A loop's accumulator, (f V (+ V E)) with E not a literal, may leave int32 +-- range. Under dual-number LuaJIT (arm64) the loop's trace is specialised to an +-- int V, and once V is a double every iteration fails that guard and runs the +-- loop's head plus a side trace, about 10 times the cost of the loop itself. +-- ADDF and friends always give a double, so the trace is specialised to that. +local ACCUMULATOR = { ["+"] = "ADD", ["-"] = "SUB", ["*"] = "MUL" } +local function accumulator(form, lname, env, code) + if not is_cons(form) or not is_symbol(car(form)) then return code end + local helper = ACCUMULATOR[car(form).name] + if not helper or env[car(form).name] then return code end + local operands = to_array(cdr(form)) + if #operands ~= 2 then return code end + local a, b = operands[1], operands[2] + local function is_param(x) return is_symbol(x) and env[x.name] == lname end + local other + if is_param(a) then other = b + elseif is_param(b) and helper ~= "SUB" then other = a + else return code end + if type(other) == "number" then return code end + if code:sub(1, #helper + 1) ~= helper .. "(" then return code end + return helper .. "F" .. code:sub(#helper + 1) +end + local function try_self_tail(form, env) if not SELF then return nil end local head = car(form) @@ -1292,7 +1466,9 @@ local function try_self_tail(form, env) end if #args == 0 then return "goto tco" end local cargs = {} - for i = 1, #args do cargs[i] = cexpr(args[i], env) end + for i = 1, #args do + cargs[i] = accumulator(args[i], SELF.lnames[i], env, cexpr(args[i], env)) + end return table.concat(SELF.lnames, ", ") .. " = " .. table.concat(cargs, ", ") .. "; goto tco" end @@ -1520,7 +1696,53 @@ local function const_lua(form, env) return nil end +-- Compiling a form and then throwing away its text must throw away what compiling +-- left behind as well. Above all CCACHE: a cached accessor names a local whose +-- declaration was in the discarded text, and reading it afterwards reads an undeclared +-- global, so nil, with no error. Returns the function that undoes the attempt. The +-- attempt runs with a child CCACHE, which it restores on either outcome. +local function compile_attempt() + local cc, fnl = CCACHE, FNL + local np, npt, pt, nloc, nosplit = fnl.np, fnl.npt, fnl.pt, fnl.nloc, fnl.nosplit + local nbodies, ctx_nt, ctx_sd = CTX and #CTX.cbodies, CTX and CTX.nt, CTX and CTX.sd + local ntcount, tcount, tailpre = NTCOUNT, TCOUNT, TAILPRE + local hoist, nhoist, hpure = HOIST, HOIST and #HOIST.out, HOIST and HOIST.pure + local self_, self_used = SELF, SELF and SELF.used + local nfref = FREF_LIST and #FREF_LIST + CCACHE = child_cache() + local function keep() CCACHE = cc end + local function undo() + CCACHE = cc + fnl.np, fnl.npt, fnl.pt, fnl.nloc, fnl.nosplit = np, npt, pt, nloc, nosplit + if CTX then + for i = #CTX.cbodies, nbodies + 1, -1 do CTX.cbodies[i] = nil end + CTX.nt, CTX.sd = ctx_nt, ctx_sd + end + NTCOUNT, TCOUNT, TAILPRE = ntcount, tcount, tailpre + if hoist then + for i = #hoist.out, nhoist + 1, -1 do hoist.out[i] = nil end + hoist.pure = hpure + end + if self_ then self_.used = self_used end + if FREF_LIST then + for i = #FREF_LIST, nfref + 1, -1 do + FREF_MAP[FREF_LIST[i].name] = nil + FREF_LIST[i] = nil + end + end + end + return undo, keep +end + +local try_trap_opt_body local function try_trap_opt(form, env) + local undo, keep = compile_attempt() + local r = try_trap_opt_body(form, env) + if r == nil then undo() else keep() end + return r +end + +try_trap_opt_body = function(form, env) if not (is_cons(form) and is_symbol(form[1]) and form[1].name == "trap-error" and not env["trap-error"]) then return nil @@ -1785,6 +2007,10 @@ local WIDE_SPLIT_LOCALS = 80 -- see the lambda case of cexpr; deeper than any curried chain in the kernel local CURRY_DEPTH = 32 +-- Deepest lambda group compiled as one function; parameters hold registers. +local MAX_UNCURRY = 4 +C.UNCURRY = os.getenv("SHEN_UNCURRY") ~= "off" +C.HOIST_LAMBDA = os.getenv("SHEN_HOIST_LAMBDA") ~= "off" local function is_ident(s) return type(s) == "string" and s:match("^[%a_][%w_]*$") ~= nil @@ -1876,6 +2102,8 @@ local function hoist_kc(form, env, lnames, own) local saved_cont = push_inline_cont() remap_cont(remap) local saved_impl, saved_cnt = IN_IMPL, CNT + local saved_value = VALUE_BODY + VALUE_BODY, VALUE_NEXT = VALUE_NEXT, false SELF = nil; HOIST = nil; CCACHE = {}; FNL = new_fnl(true, nslots); IN_IMPL = false local owner if own then @@ -1889,6 +2117,7 @@ local function hoist_kc(form, env, lnames, own) if FNL.pt then body_stmts = "local PT = {}; " .. body_stmts end SELF = saved_self; HOIST = saved_h; CCACHE = saved_cc; FNL = saved_fnl CONT = saved_cont; IN_IMPL = saved_impl; CNT = saved_cnt + VALUE_BODY = saved_value local idx = #CTX.cbodies + 1 if owner and owner.used then local dv = gen("d") @@ -1929,6 +2158,21 @@ local function try_split_let(form, env) return ret(hoist_kc(form, env, lnames, true)) end +-- A lambda that reads no outer local is a constant: built once when its defun +-- is loaded, in KC, instead of per evaluation. Creating a closure (FNEW) is a +-- bytecode LuaJIT cannot trace, so the defun creating one per iteration would +-- otherwise never compile. Only its identity changes: two evaluations of one +-- such lambda are now the same function. +local function hoist_closed(form, env, src) + if not (CTX and C.HOIST_LAMBDA) then return src end + local fv = {} + collect_free(form, env, {}, fv) + if next(fv) ~= nil then return src end + local idx = #CTX.cbodies + 1 + CTX.cbodies[idx] = src + return "KC[" .. idx .. "]" +end + -- The statements of a lambda body (`params`: its parameter list), compiled -- as a function of its own that counts itself like a defun's impl (see -- NT_SRC): a lambda called by a tail call holds no counted caller frame. @@ -1937,10 +2181,13 @@ local function counted_body(form, env, params) local saved_self, saved_h, saved_cc, saved_fnl = SELF, HOIST, CCACHE, FNL local saved_cont = push_inline_cont() local saved_impl, saved_cnt = IN_IMPL, CNT + local saved_value = VALUE_BODY + VALUE_BODY = false SELF = nil; HOIST = nil; CCACHE = {}; FNL = new_fnl(true, 1); IN_IMPL = false local owner = { used = false, tok = "\1" .. gen("k") .. "\1" } CNT = { owner = owner, ret = true } local body = ctail(form, env) + VALUE_BODY = saved_value if FNL.pt then body = "local PT = {}; " .. body end SELF = saved_self; HOIST = saved_h; CCACHE = saved_cc; FNL = saved_fnl CONT = saved_cont; IN_IMPL = saved_impl; CNT = saved_cnt @@ -2020,6 +2267,8 @@ function cexpr(form, env) -- would illegally cross the function boundary. Likewise reset the -- chain-hoist state: KC[i] has its own local budget, and locals cached -- in the enclosing function are not in scope here. + -- the whole body of a freeze or a lambda is in tail position (see ccall) + VALUE_NEXT = form ~= TAIL_FORM local call = hoist_kc(form, env) -- The KC call itself may raise or side-effect: later siblings in the -- enclosing statement must not hoist chains ahead of it. @@ -2033,7 +2282,10 @@ function cexpr(form, env) local saved_cont = push_inline_cont() local saved_impl = IN_IMPL SELF = nil; HOIST = nil; CCACHE = {}; FNL = new_fnl(true); IN_IMPL = false + local saved_value = VALUE_BODY + VALUE_BODY = VALUE_BODY or form ~= TAIL_FORM local body_stmts = ctail(form, env) + VALUE_BODY = saved_value if FNL.pt then body_stmts = "local PT = {}; " .. body_stmts end SELF = saved_self; HOIST = saved_h; CCACHE = saved_cc; FNL = saved_fnl CONT = saved_cont; IN_IMPL = saved_impl @@ -2064,10 +2316,35 @@ function cexpr(form, env) for i = 1, depth do e2 = extend(e2, vars[i].name, t .. "[" .. i .. "]") end local saved_h, saved_impl, saved_cnt = HOIST, IN_IMPL, CNT HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame - local bodyc = cexpr_tail(inner, e2) + local bodyc = fn_body_tail(inner, e2) HOIST = saved_h; IN_IMPL = saved_impl; CNT = saved_cnt return "CURRY(" .. depth .. ", function(" .. t .. ") return " .. bodyc .. " end)" end + -- (lambda X (lambda Y B)) is one function of the whole group. Creating + -- the inner closure had no effect but the allocation, and a call with + -- every argument (CURn, APPn) now makes none. A partial application + -- goes through PARTIAL, as for a defun. + -- (Not in wide mode, which budgets each closure's upvalues below.) + if depth >= 2 and depth <= MAX_UNCURRY and C.UNCURRY and not WIDE then + local lns, e2 = {}, env + for i = 1, depth do + lns[i] = gen("v") + e2 = extend(e2, vars[i].name, lns[i]) + end + local plist = table.concat(lns, ", ") + local fnsrc + if CTX then + fnsrc = "function(" .. plist .. ") " .. upart_guard(plist, depth) + .. counted_body(inner, e2, plist) .. " end" + else + local saved_h, saved_impl, saved_cnt = HOIST, IN_IMPL, CNT + HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame + local bodyc = fn_body_tail(inner, e2) + HOIST = saved_h; IN_IMPL = saved_impl; CNT = saved_cnt + fnsrc = "function(" .. plist .. ") " .. upart_guard(plist, depth) .. "return " .. bodyc .. " end" + end + return hoist_closed(form, env, "MKFUN(" .. depth .. ", " .. fnsrc .. ")") + end local ln = gen("v") if WIDE and CTX then -- each distinct outer name the closure reads is one upvalue @@ -2088,7 +2365,7 @@ function cexpr(form, env) local saved_h, saved_impl, saved_cont, saved_cnt = HOIST, IN_IMPL, CONT, CNT push_inline_cont(); remap_cont(remap) HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame - local bodyc = cexpr_tail(body, extend(remap_env(env, remap), v.name, ln)) + local bodyc = fn_body_tail(body, extend(remap_env(env, remap), v.name, ln)) HOIST = saved_h; IN_IMPL = saved_impl; CONT = saved_cont; CNT = saved_cnt local idx = #CTX.cbodies + 1 CTX.cbodies[idx] = "function(" .. ln .. ", " .. t .. ") return " .. bodyc .. " end" @@ -2100,13 +2377,13 @@ function cexpr(form, env) -- Not impl-scope: `fN` locals live in impl, not this nested function. if CTX then -- a function of its own that counts itself, like a defun's impl - return "MKFUN(1, function(" .. ln .. ") " .. counted_body(body, e2, ln) .. " end)" + return hoist_closed(form, env, lam1(ln, counted_body(body, e2, ln))) end local saved_h, saved_impl, saved_cnt = HOIST, IN_IMPL, CNT HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame - local bodyc = cexpr_tail(body, e2) + local bodyc = fn_body_tail(body, e2) HOIST = saved_h; IN_IMPL = saved_impl; CNT = saved_cnt - return "MKFUN(1, function(" .. ln .. ") return " .. bodyc .. " end)" + return lam1(ln, "return " .. bodyc) elseif op == "freeze" then local body = car(cdr(form)) if CTX then @@ -2127,7 +2404,7 @@ function cexpr(form, env) local saved_cont = CONT if remap then push_inline_cont(); remap_cont(remap) end HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame - local body_str = cexpr_tail(body, remap_env(env, remap)) + local body_str = fn_body_tail(body, remap_env(env, remap)) HOIST = saved_h; IN_IMPL = saved_impl; CONT = saved_cont; CNT = saved_cnt local idx = #CTX.cbodies + 1 CTX.cbodies[idx] = "function(" .. params .. ") return " @@ -2137,7 +2414,7 @@ function cexpr(form, env) end local saved_h, saved_impl, saved_cnt = HOIST, IN_IMPL, CNT HOIST = nil; IN_IMPL = false; CNT = nil -- runs outside this frame - local bodyc = cexpr_tail(body, env) + local bodyc = fn_body_tail(body, env) HOIST = saved_h; IN_IMPL = saved_impl; CNT = saved_cnt return "MKFUN(0, function() return " .. bodyc .. " end)" elseif op == "defun" then @@ -2157,6 +2434,72 @@ function cexpr(form, env) end end +-- Count sinking. A counting defun only needs its count on the paths that make +-- a non-tail call, and in pattern-matching code half the calls take a clause +-- that makes none: a function's leading tests run uncounted, and each branch +-- opens the count itself if it needs it. The segment path restarts the +-- function from the top, so everything before the count must be safe to run +-- twice: tests built from SINK_TEST primitives over variables and literals, +-- which change nothing (one that raises never reaches the count). +local SINK_TEST = {} +for _, n in ipairs({ + "=", ">", "<", ">=", "<=", "cons?", "number?", "string?", "symbol?", + "boolean?", "empty?", "absvector?", "tuple?", "integer?", "variable?", + "hd", "tl", "not", "and", "or", "if", "+", "-", "*", +}) do SINK_TEST[n] = true end +local function sink_test(form, env) + if not is_cons(form) then return true end + local h = car(form) + if not (is_symbol(h) and not env[h.name] and SINK_TEST[h.name]) then return false end + local cur = cdr(form) + while is_cons(cur) do + if not sink_test(car(cur), env) then return false end + cur = cdr(cur) + end + return true +end +local function sinkable(form, env) + if not is_cons(form) then return false end + local h = car(form) + if not (is_symbol(h) and not env[h.name]) then return false end + if h.name == "if" then return sink_test(car(cdr(form)), env) end + if h.name == "cond" then + local cur = cdr(form) + while is_cons(cur) do + if not sink_test(car(car(cur)), env) then return false end + cur = cdr(cur) + end + return true + end + return false +end +-- `form` in tail position of a counting defun with no count open yet. +sink_branch = function(form, env) + local saved = CNT + local owner = { used = false, tok = "\1" .. gen("k") .. "\1" } + local sink = sinkable(form, env) + CNT = { owner = owner, ret = true, sink = sink, params = saved.params } + local undo, keep = compile_attempt() + local src = ctail(form, env) + if sink and owner.used then + -- a test made a counted call after all: the count opens here. Starting over + -- discards this text, so discard what compiling it left behind too. + undo() + sink = false + CNT.sink = false + owner.used = false + src = ctail(form, env) + else + keep() + end + CNT = saved + if sink or not owner.used then return (src:gsub(owner.tok, "")) end + local dv = gen("d") + return "local " .. dv .. " = SD[1]; if " .. dv .. " >= " .. SEG_FRAMES + .. " then return SEG(impl" .. saved.params .. ") end; SD[1] = " .. dv .. " + 1; " + .. (src:gsub(owner.tok, "SD[1] = " .. dv .. "; ")) +end + -- ------------------------------------------------------------------ -- Statement-nesting depth ctail will emit for `form`, used to pick which -- branch of an `if` gets nested inside the guard block (the other is emitted @@ -2208,6 +2551,79 @@ local function ctail_depth(form, env) return 0 end +-- ------------------------------------------------------------------ +-- Strict tests: the test of an if or cond clause and both operands of and/or +-- must be booleans. (if 1 a b) used to take the then branch with no error, since +-- Lua treats any value but nil and false as true. A form that can only give a +-- boolean goes unchecked; anything else is compiled as (shen.bc FORM), which is +-- BC(FORM), an ordinary call that raises on anything else. +-- ------------------------------------------------------------------ +local function bc(form, env) + if gives_boolean(form, env) then return form end + return R.cons(R.intern("shen.bc"), R.cons(form, NIL)) +end +-- The last operand of and/or is in tail position, and a tail call stays one: +-- (and (p X) (next Xs)) must not grow the stack, whichever function it calls. So +-- in a defun's tail the check goes to the leaves of its control forms, and a call +-- is left unchecked. Its value is this function's result, which whoever tests it +-- checks; only a caller that uses the result without testing it can see a +-- non-boolean. The same goes for a lambda or freeze body with no enclosing defun. In a +-- top-level form or a control form in value position nothing is a tail call, so a call +-- there is checked as well. +local NOT_A_TAIL_CALL = {} +for _, n in ipairs({ + "trap-error", "lambda", "freeze", "+", "-", "*", "/", "cons", "hd", "tl", "@p", + "@v", "@s", "cn", "str", "pos", "tlstr", "intern", "absvector", "<-address", + "address->", "n->string", "string->n", "value", "set", "get-time", +}) do NOT_A_TAIL_CALL[n] = true end +local function leave_as_tail_call(h, env) + if VALUE_BODY or not (DEFUN_NAME or IN_FN_BODY) then return false end + return not (is_symbol(h) and not env[h.name] and NOT_A_TAIL_CALL[h.name]) +end +local function bc_tail(form, env) + if not is_cons(form) then return bc(form, env) end + local h = car(form) + if not is_symbol(h) or env[h.name] then + if gives_boolean(form, env) or not leave_as_tail_call(h, env) then return bc(form, env) end + return form + end + local op = h.name + local function rebuild(items) + local r = NIL + for i = #items, 1, -1 do r = R.cons(items[i], r) end + return r + end + if op == "if" then + local a = to_array(form) + if #a == 4 then return rebuild({ h, a[2], bc_tail(a[3], env), bc_tail(a[4], env) }) end + elseif op == "cond" then + local a = to_array(form) + for i = 2, #a do + local cl = to_array(a[i]) + a[i] = rebuild({ cl[1], bc_tail(cl[2], env) }) + end + return rebuild(a) + elseif op == "do" then + local a = to_array(form) + a[#a] = bc_tail(a[#a], env) + return rebuild(a) + elseif op == "let" then + local a = to_array(form) + if #a == 4 and is_symbol(a[2]) then + local inner = {} + for k, v in pairs(env) do inner[k] = v end + inner[a[2].name] = a[2].name + return rebuild({ h, a[2], a[3], bc_tail(a[4], inner) }) + end + elseif op == "type" then + local a = to_array(form) + a[2] = bc_tail(a[2], env) + return rebuild(a) + end + if gives_boolean(form, env) or not leave_as_tail_call(h, env) then return bc(form, env) end + return form +end + -- ------------------------------------------------------------------ -- ctail : tail position (emits statements; control-flow returns) -- ------------------------------------------------------------------ @@ -2218,6 +2634,8 @@ function ctail(form, env) local head = car(form) if is_symbol(head) and not env[head.name] then local op = head.name + -- under count sinking (see sink_branch) a branch opens its own count + local ctail = (CNT and CNT.sink) and sink_branch or ctail if op == "if" then local test = car(cdr(form)) local th = car(cdr(cdr(form))) @@ -2230,7 +2648,7 @@ function ctail(form, env) -- was not taken -- semantics identical to the old fully-nested -- `if ... then ... else ... end`, but a then-spine of N pattern tests -- now costs O(1) nesting instead of N (LuaJIT parser rejects ~200). - local pre, testc = cstmt_expr(test, env) + local pre, testc = cstmt_expr(bc(test, env), env) local dt = ctail_depth(th, env) local de = el and ctail_depth(el, env) or 0 if de <= dt then @@ -2241,12 +2659,12 @@ function ctail(form, env) local els = el and ctail(el, env) or ret("false") CCACHE = saved_cc return pre .. "if not (" .. testc .. ") then " .. els .. " end " - .. ctail(th, env) + .. with_cons_facts(test, env, ctail, th, env) else -- nest then, flatten else local saved_cc = CCACHE CCACHE = child_cache() - local ths = ctail(th, env) + local ths = with_cons_facts(test, env, ctail, th, env) CCACHE = saved_cc return pre .. "if (" .. testc .. ") then " .. ths .. " end " .. ctail(el, env) @@ -2262,12 +2680,12 @@ function ctail(form, env) -- each result sits inside its own then-block: block-scope its chain cache local saved_cc = CCACHE CCACHE = child_cache() - local resc = ctail(res, env) + local resc = with_cons_facts(test, env, ctail, res, env) CCACHE = saved_cc if i == 1 then - parts[#parts+1] = "if (" .. cexpr(test, env) .. ") then " .. resc + parts[#parts+1] = "if (" .. cexpr(bc(test, env), env) .. ") then " .. resc else - parts[#parts+1] = " elseif (" .. cexpr(test, env) .. ") then " .. resc + parts[#parts+1] = " elseif (" .. cexpr(bc(test, env), env) .. ") then " .. resc end end parts[#parts+1] = " else return ERR(\"cond failure\") end" @@ -2308,12 +2726,13 @@ function ctail(form, env) -- inside `then` cost one syntax level per test. The early-exit form is -- semantically identical and costs O(1) nesting per link. local a = car(cdr(form)); local b = car(cdr(cdr(form))) - local pre, ac = cstmt_expr(a, env) - return pre .. "if not (" .. ac .. ") then " .. ret("false") .. " end " .. ctail(b, env) + local pre, ac = cstmt_expr(bc(a, env), env) + return pre .. "if not (" .. ac .. ") then " .. ret("false") .. " end " + .. with_cons_facts(a, env, ctail, bc_tail(b, env), env) elseif op == "or" then local a = car(cdr(form)); local b = car(cdr(cdr(form))) - local pre, ac = cstmt_expr(a, env) - return pre .. "if (" .. ac .. ") then " .. ret("true") .. " end " .. ctail(b, env) + local pre, ac = cstmt_expr(bc(a, env), env) + return pre .. "if (" .. ac .. ") then " .. ret("true") .. " end " .. ctail(bc_tail(b, env), env) elseif op == "trap-error" then local n0 = NTCOUNT local opt = try_trap_opt(form, env) @@ -2398,6 +2817,38 @@ end -- top-level form compilation -- ------------------------------------------------------------------ -- compile (defun NAME (PARAMS) BODY) -> Lua source registering into F-table. +local function SD_REF_SEEN(src) return src:find("SD[1]", 1, true) ~= nil end +C.SINK_COUNT = os.getenv("SHEN_SINK_COUNT") ~= "off" + +-- Hot runtime helpers a defun's chunk binds as locals. A chunk global is a +-- hash lookup in ENV plus an identity guard on every use, in the interpreter +-- and on a trace alike; a never-reassigned local is an immutable upvalue, +-- which a trace folds to a constant. Only ENV entries fixed before any chunk +-- runs may be listed (not GET_OR, say, which install_native_stdlib sets later). +-- Only the names a body mentions are bound: each is an upvalue, and Lua allows +-- 60 per function (past that the width retry recompiles in wide mode). +local HOT_HELPERS = { + "ADD", "SUB", "MUL", "DIV", "GT", "LT", "GE", "LE", "EQ", "HD", "TL", + "APP1", "APP2", "APP3", "APP4", "APP", "MKFUN", "F", "NIL", + "CUR2", "CUR3", "CUR4", "PRE2", "PRE3", "PRE4", "CUR2P", "CUR3P", "CUR4P", + "TRMC_C", "TRMC_T", "CMT", "getmetatable", "setmetatable", "BC", "UPART", +} +local function helper_imports(a, b, c) + -- wide mode budgets its upvalues without these + if WIDE then return "" end + local names = {} + local find = string.find + for i = 1, #HOT_HELPERS do + local h = HOT_HELPERS[i] + if find(a, h, 1, true) or find(b, h, 1, true) or (c and find(c, h, 1, true)) then + names[#names + 1] = h + end + end + if #names == 0 then return "" end + local l = table.concat(names, ", ") + return "local " .. l .. " = " .. l .. "; " +end + local function cdefun(form) local name = car(cdr(form)).name local params = to_array(car(cdr(cdr(form)))) @@ -2459,7 +2910,15 @@ local function cdefun(form) owner = { used = false, tok = "\1" .. gen("k") .. "\1" } CNT = { owner = owner, ret = true } end - local body_src = ctail(body, env) + local body_src + if owner and SELF == nil and C.SINK_COUNT then + CNT.params = #lnames > 0 and (", " .. table.concat(lnames, ", ")) or "" + body_src = sink_branch(body, env) + -- each branch that needs the count opens it; none is left for the top + CTX.sd = CTX.sd or SD_REF_SEEN(body_src) + else + body_src = ctail(body, env) + end CNT = saved_cnt local fref_decls = {} for i, r in ipairs(FREF_LIST) do @@ -2527,6 +2986,9 @@ local function cdefun(form) if CTX.nt or CTX.sd or (owner and owner.used) then nt_init = "local SD, SEG, SEGSELF = SD, SEG, SEGSELF; " .. (CTX.nt and NT_SRC or "") end + -- (TLOOP below uses these whether or not the body names them) + nt_init = helper_imports(kc_init, body_src, trmc_used and "setmetatable CMT NIL TRMC_C TRMC_T") + .. nt_init CTX = saved local plist = table.concat(lnames, ", ") local src @@ -2585,21 +3047,50 @@ end -- expose expression compiler + a returning-chunk helper for eval C.cexpr = cexpr C.ctail = ctail +-- Run one whole compile. Its state lives in module locals that each compiler +-- function saves on entry and restores on the way out, which an error skips: a +-- malformed form left CTX set, and every later compile then emitted KC[i] or NT +-- references into a chunk that declares neither, so the session could no longer +-- evaluate anything. C.ARITY is restored too, so a defun that failed to compile +-- does not change how later calls to the old definition are compiled. +local function atomic_compile(arity_name, f, ...) + local s_ctx, s_self, s_cont, s_ntcount, s_cnt, s_tcount, s_tailpre = CTX, SELF, CONT, NTCOUNT, CNT, TCOUNT, TAILPRE + local s_in, s_dn, s_fl, s_fm, s_cc, s_hoist, s_fnl = IN_IMPL, DEFUN_NAME, FREF_LIST, FREF_MAP, CCACHE, HOIST, FNL + local s_wide, s_tf = WIDE, TAIL_FORM + local s_value, s_fn = VALUE_BODY, IN_FN_BODY + local s_arity = arity_name and C.ARITY[arity_name] + local ok, res = pcall(f, ...) + if ok then return res end + CTX, SELF, CONT, NTCOUNT, CNT, TCOUNT, TAILPRE = s_ctx, s_self, s_cont, s_ntcount, s_cnt, s_tcount, s_tailpre + IN_IMPL, DEFUN_NAME, FREF_LIST, FREF_MAP, CCACHE, HOIST, FNL = s_in, s_dn, s_fl, s_fm, s_cc, s_hoist, s_fnl + WIDE, TAIL_FORM = s_wide, s_tf + VALUE_BODY, VALUE_NEXT = s_value, false + IN_FN_BODY = s_fn + for k in pairs(PROVEN) do PROVEN[k] = nil end + if arity_name then C.ARITY[arity_name] = s_arity end + error(res, 0) +end + function C.compile_expr_chunk(form) -- the chunk's one call is not counted (nt_call): a single frame - return "return (" .. cexpr_tail(form, {}) .. ")" + return atomic_compile(nil, function() + return "return (" .. cexpr_tail(form, {}) .. ")" + end) end -- compile any top-level form to a Lua statement string function C.compile_top(form, wide) if is_cons(form) and is_symbol(car(form)) and car(form).name == "defun" then - return cdefun_mode(form, wide) + local nm = is_cons(cdr(form)) and car(cdr(form)) + return atomic_compile(is_symbol(nm) and nm.name or nil, cdefun_mode, form, wide) else -- a top-level expression: evaluate for side effects. Wrapped in do..end -- so a whole kernel file can be concatenated into ONE chunk (boot.lua's -- bytecode cache) without accumulating `local _` slots toward Lua's -- 200-locals-per-scope limit. - return "do local _ = " .. cexpr(form, {}) .. "; end" + return atomic_compile(nil, function() + return "do local _ = " .. cexpr(form, {}) .. "; end" + end) end end diff --git a/prims.lua b/prims.lua index 11597e9..7d262ed 100644 --- a/prims.lua +++ b/prims.lua @@ -6,6 +6,8 @@ local C = require("compiler") local cons, is_cons, NIL = R.cons, R.is_cons, R.NIL local Symbol, intern, is_symbol = R.Symbol, R.intern, R.is_symbol local Excn, mkexcn = R.Excn, R.mkexcn +local rawset = rawset +local NEGZERO = -0.0 local Vmt = R.Vmt -- absvector metatable (pure-array layout; shared with runtime.lua) -- counted non-tail calls (runtime.lua stack segments, #93), written inline -- where a native calls back into Shen code (a shared trampoline would look @@ -127,9 +129,25 @@ end P.Thunk = Thunk local APP -- fwd +local MAX_PARTIAL1 = 12 +local PARTIAL1 = {} +for ar = 2, MAX_PARTIAL1 do + local ps = {} + for i = 1, ar - 1 do ps[i] = "x" .. i end + local plist = table.concat(ps, ", ") + PARTIAL1[ar] = assert((loadstring or load)( + "return function(f, a) return function(" .. plist .. ") return f(a, " .. plist .. ") end end", + "=PARTIAL1"))() +end local function PARTIAL(f, ar, have) local need = ar - #have local g + -- One argument at a time is what a curried chain does: a closure over it, + -- and no tables on the call. (A chain through a defun of arity n calls n-1 + -- of these per use.) + if #have == 1 and ar <= MAX_PARTIAL1 then + return MKFUN(need, PARTIAL1[ar](f, have[1])) + end g = function(...) local extra = {...} local all = {} @@ -141,6 +159,30 @@ local function PARTIAL(f, ar, have) return MKFUN(need, g) end +-- A compiled one-parameter lambda carries no FA entry (see the lambda case of +-- cexpr in compiler.lua): recording it was a new key in a weak table for every +-- closure created. An unrecorded function otherwise means "assume exact" +-- (primitives, a host's function), so APP tells the lambda apart here, on the +-- only path that needs to: applying it to other than one argument. +local getinfo = debug and debug.getinfo +-- The source names of chunks load_chunk loaded: compiled Shen, as opposed to a +-- host's own Lua, whatever it called its chunk. Recording this per chunk costs +-- nothing per closure, unlike marking each lambda as it is created. +local COMPILED_SOURCE = {} +local RAW_LAMBDA = setmetatable({}, {__mode="k"}) -- what getinfo said, per function +local function is_raw_lambda(f) + local r = RAW_LAMBDA[f] + if r ~= nil then return r end + r = false + if getinfo then + local i = getinfo(f, "uS") + r = i.nparams == 1 and not i.isvararg and i.what ~= "main" + and COMPILED_SOURCE[i.source] == true + end + RAW_LAMBDA[f] = r + return r +end + APP = function(f, ...) -- Fast path: the operand is overwhelmingly a Lua function (a compiled Shen -- function or a continuation/thunk). Check that first so the common case @@ -149,7 +191,13 @@ APP = function(f, ...) if type(f) == "function" then local n = select("#", ...) local ar = FA[f] - if ar == nil or ar == n then -- primitive/unknown-arity fn: assume exact + if ar == nil then + if n == 1 or not is_raw_lambda(f) then -- primitive/unknown-arity fn: assume exact + return f(...) + end + ar = 1 + end + if ar == n then return f(...) elseif n < ar then return PARTIAL(f, ar, {...}) @@ -179,6 +227,21 @@ APP = function(f, ...) end P.APP, P.MKFUN, P.PARTIAL = APP, MKFUN, PARTIAL +-- What a compiled lambda of n parameters returns when called with fewer (see upart_guard +-- in compiler.lua): the arguments up to the first nil, partially applied. Called from the +-- lambda itself, not as a tail call, so level 2 is that lambda. +local getinfo_f = debug and debug.getinfo +local function UPART(n, ...) + local f = getinfo_f(2, "f").func + local have, k = { ... }, 0 + while k < n - 1 and have[k + 1] ~= nil do k = k + 1 end + if FA[f] == nil and n > 1 then FA[f] = n end + if k == 0 then return f end + for i = k + 1, #have do have[i] = nil end + return PARTIAL(f, n, have) +end +P.UPART = UPART + -- ---- equality ------------------------------------------------------------ -- Lists are compared by walking the cdr spine in a loop and recursing only -- into elements: the old car-and-cdr recursion was one Lua frame per cell, @@ -266,6 +329,12 @@ defprim("string?", 1, function(x) return type(x)=="string" end) defprim("symbol?", 1, function(x) return is_symbol(x) end) defprim("boolean?",1, function(x) return type(x)=="boolean" end) defprim("not", 1, function(x) if type(x)~="boolean" then ERR("not: not boolean") end return not x end) +-- what a test of a form that may not give a boolean compiles to (compiler.lua, bc) +local function boolean_check(x) + if x == true or x == false then return x end + ERR(R.to_str(x) .. " is not a boolean") +end +defprim("shen.bc", 1, boolean_check) defprim("integer?",1, function(x) return type(x)=="number" and x==math.floor(x) and x~=math.huge and x~=-math.huge end) -- symbols / strings @@ -285,7 +354,7 @@ local numToStr = R.num_to_str defprim("str", 1, function(x) local t = type(x) if t=="number" then return numToStr(x) - elseif t=="string" then return x -- str of a string is the string (with quotes? Shen: str adds nothing here) + elseif t=="string" then return '"' .. x .. '"' -- quoted, as on shen-cl, shen-scheme, shen-go, shen-c elseif t=="boolean" then return x and "true" or "false" elseif is_symbol(x) then return x.name elseif x==NIL then ERR("str: cannot convert ()") @@ -515,12 +584,15 @@ defprim("string->symbol", 1, function(s) return intern(s) end) defprim("empty?", 1, function(x) return x==NIL end) -- globals (dual namespace) +-- Only a symbol names a global: keying on tostring() made (set "x" 1) and +-- (set x 1) the same variable, and (set 1 2) define one called "1". defprim("set", 2, function(sym, v) - local key = is_symbol(sym) and sym.name or tostring(sym) - GLOBALS[key] = v; return v + if not is_symbol(sym) then ERR(R.to_str(sym) .. " is not a symbol") end + GLOBALS[sym.name] = v; return v end) defprim("value", 1, function(sym) - local key = is_symbol(sym) and sym.name or tostring(sym) + if not is_symbol(sym) then ERR(R.to_str(sym) .. " is not a symbol") end + local key = sym.name local v = GLOBALS[key] if v == nil then ERR("variable " .. key .. " has no value") end return v @@ -537,6 +609,13 @@ end) local FAILOBJ = intern("shen.fail!") P.FAILOBJ = FAILOBJ defprim("absvector", 1, function(n) + -- Validate first: a negative or fractional length would build a table whose + -- length word disagrees with its slots (and a non-number used to surface as + -- a raw Lua arithmetic error), and the printer dies on the result outside + -- any handler the program set up. + if type(n) ~= "number" or n < 0 or n % 1 ~= 0 then + ERR(R.to_str(n) .. " is not a valid vector length") + end local v = { [1] = n } for i=0,n-1 do v[i+2] = FAILOBJ end return setmetatable(v, Vmt) @@ -550,12 +629,17 @@ local function addr_oob(v, i) ERR(R.to_str and (tostring(i) .. " is not a valid index") or "invalid index") end P.addr_oob = addr_oob +-- A negative index is out of range too: v[1] holds the length word. A write to +-- a slot that does not exist raises as well, rather than growing the table. defprim("<-address", 2, function(v, i) local w = v[i+2] - if w == nil then addr_oob(v, i) end + if w == nil or i < 0 then addr_oob(v, i) end return w end) -defprim("address->", 3, function(v, i, x) v[i+2]=x; return v end) +defprim("address->", 3, function(v, i, x) + if v[i+2] == nil or i < 0 then addr_oob(v, i) end + rawset(v, i+2, x); return v +end) -- freeze/thaw : thunks are 0-arity functions (kernel: (defun thaw (V) (V))) -- Hot path in the Prolog/typechecker CPS: a thawed value is always a 0-arity @@ -575,12 +659,14 @@ defprim("type", 2, function(x, _ty) return x end) local load_form -- fwd (defined below) defprim("eval-kl", 1, function(form) return P.eval(form) end) --- get-time : (get-time Sym), Sym in {run, real, unix} +-- get-time : (get-time Sym), Sym in {run, real, unix}; anything else raises local t0_real = os.time() defprim("get-time", 1, function(sym) - local name = is_symbol(sym) and sym.name or tostring(sym) + local name = is_symbol(sym) and sym.name if name == "run" then return os.clock() - else return os.time() - t0_real end -- real / unix : wall seconds since boot + elseif name == "unix" then return os.time() + elseif name == "real" then return os.time() - t0_real -- wall seconds since boot + else ERR("get-time: invalid option: " .. R.to_str(sym)) end end) -- ---- streams ------------------------------------------------------------- @@ -715,7 +801,20 @@ end) -- close (STREAM) -> () defprim("close", 1, function(st) - if is_stream(st) and st.close then st.close() end + if not is_stream(st) then ERR("close: not a stream") end + -- Closing twice is harmless; using a closed stream raises a Shen error, not + -- Lua's "attempt to use a closed file". Replacing the functions keeps the + -- read and write paths free of a closed check. + if st.close then + st.close() + st.close = nil + if st.kind == "in" then + st.readbyte = function() ERR("read-byte: stream is closed") end + st.pb, st.eof = nil, false + else + st.write = function() ERR("write-byte: stream is closed") end + end + end return NIL end) @@ -876,9 +975,9 @@ function P.install_native_prolog() -- Per-query capacity: shen.prolog-vector allocates ONE absvector of -- (value shen.*prolog-memory*) slots per query, so the kernel's -- shen.nextticket writes slot n and the query dies once n reaches the - -- vector size. Our absvector is a Lua table, and address-> is an - -- unchecked store, so without this the vector would silently grow and a - -- query that raises on every other port would answer here. + -- vector size. This native path stores without going through address->, + -- so without this the vector would silently grow and a query that raises + -- on every other port would answer here. local size = vec[1] if type(size) == "number" and n >= size then ERR("prolog vector index out of range: a single query may allocate at " @@ -1147,14 +1246,65 @@ function P.install_native_stdlib() end local function map(f, lst) return map_h(f, lst, NIL) end + -- shen.walk (macros.kl): apply a macro to a form bottom-up, as + -- (F (map (/. Z (shen.walk F Z)) X)). macroexpand makes one walk per macro, + -- several per form, and nearly always nothing changes, yet the kernel's rebuilds + -- the whole tree each time and macroexpand then compares it to the old one + -- cell by cell. Keep the old cells until a child really changes, so the usual + -- walk allocates nothing and that comparison ends at once. Same calls in the + -- same order; forms are immutable, so sharing is not observable. An improper + -- list goes to the kernel's, for its error. + local orig_walk = F["shen.walk"] + local function walk(f, x) + if not is_cons(x) then return APP(f, x) end + local t = x + repeat t = t[2] until not is_cons(t) + if t ~= NIL then return orig_walk(f, x) end + local head, last = nil, nil + local cell = x + repeat + local e = cell[1] + local v + if is_cons(e) then + -- a counted call, as in map_h + local d = SD[1] + if d < SEG_FRAMES then SD[1] = d + 1; v = walk(f, e); SD[1] = d + else v = SEGRUN(walk, f, e) end + else + v = APP(f, e) + end + if head then + local c = cons(v, NIL) + last[2] = c; last = c + elseif v ~= e then + -- first change: copy the cells before this one + local p = x + while p ~= cell do + local c = cons(p[1], NIL) + if head then last[2] = c else head = c end + last = c + p = p[2] + end + local c = cons(v, NIL) + if head then last[2] = c else head = c end + last = c + end + cell = cell[2] + until cell == NIL + return APP(f, head or x) + end + -- variable? : a symbol whose name is an uppercase letter followed by -- alphanums (kernel chain variable?/analyse-variable?/uppercase?/alphanums?, -- where alphanum = letter | digit | the misc set "=-*/+_?$!@~.><&%'#`"). -- The kernel version walks the name char-by-char through trap-error + -- hdstr/tlstr string churn; here one pattern match, memoized on the - -- interned symbol itself so each distinct symbol pays it once. Non-symbols - -- delegate to the original for byte-identical edge semantics. - local orig_variable = F["variable?"] + -- interned symbol itself so each distinct symbol pays it once. A non-symbol + -- is never a variable: the kernel version answers false outright for + -- booleans, numbers and strings, and for anything else (str V) raises inside + -- its trap-error, so it is false again, by way of a thrown error. Thrown + -- errors are expensive here (on arm64 LuaJIT unwinds through the system + -- unwinder) and the define/yacc parser asks this about every cons it meets. local VARPAT = "^[A-Z][%w=%-%*/%+_%?%$!@~%.><&%%'#`]*$" local function variable_q(x) if is_symbol(x) then @@ -1165,7 +1315,7 @@ function P.install_native_stdlib() end return v end - return orig_variable(x) + return false end -- pr (issue #22): the canonical kernel `pr` (klambda/writer.kl) gates ALL @@ -1226,6 +1376,21 @@ function P.install_native_stdlib() -- is never second-guessed. Boot alone calls hash ~800 times (all on symbol -- keys); a native fold there and at runtime avoids the per-call explode -- list + map + KL recursion. + -- shen.mod (sys.kl:123) builds a list of doubled divisors and subtracts them: + -- for N in the range below that takes a few thousand instructions, and gives + -- exactly N % D, since every double involved is an exact integer. Anything + -- else, including the negative N it returns as is and the divisors it never + -- returns from, stays with the kernel's definition. + local orig_mod = F["shen.mod"] + local function shen_mod(n, d) + if type(n) == "number" and type(d) == "number" + and n >= 0 and n < 9007199254740992 and d >= 1 and d < 9007199254740992 + and n == mfloor(n) and d == mfloor(d) then + return n % d + end + return orig_mod(n, d) + end + local orig_hash = F["hash"] local function hashkey_bytes(s) local acc = 1 @@ -1252,6 +1417,11 @@ function P.install_native_stdlib() end end local function hash(v, bound) + -- The kernel's shen.mod never returns for a bound of 0 or below (non-symbol + -- keys hung the process) and x % 0 is NaN (symbol keys returned it). + if type(bound) ~= "number" or bound < 1 or bound % 1 ~= 0 then + ERR("hash: " .. R.to_str(bound) .. " is not a positive integer") + end local s if is_symbol(v) then s = v.name elseif type(v) == "string" then s = v @@ -1453,7 +1623,105 @@ function P.install_native_stdlib() } -- try the alternatives from number `from` on at v: returns the kind -- ("open", "item", "skip", or nil for ), the rest, and the value + -- + -- Fast paths, by the token's first code. A token's first code rules out most + -- alternatives, and the plain cases of the rest take a few lines here, where + -- the KL combinators spent about 20,000 instructions per input byte. Anything + -- else takes the ordered choice below, so these must only ever agree with it: + -- a digit run followed by . or e, a string with c#, a non-ASCII byte and all + -- punctuation go to the kernel's rules. + -- In a do block: install_native_stdlib is near the limit of 200 locals. + local sx_fast + local sx_dollar = intern("$") + do + local SX_WS, SX_SYM0, SX_SYMN, SX_CH = {}, {}, {}, {} + for c = 0, 127 do SX_CH[c] = string.char(c) end + for _, c in ipairs{ 9, 10, 13, 32 } do SX_WS[c] = true end + for c = 97, 122 do SX_SYM0[c] = true end + for c = 65, 90 do SX_SYM0[c] = true end + for ch in ("=-*/+_?$!@~.><&%'#`"):gmatch(".") do SX_SYM0[ch:byte()] = true end + for c in pairs(SX_SYM0) do SX_SYMN[c] = true end + local SX_DIGIT, SX_NUMCONT = {}, { [45] = true, [43] = true, [46] = true } + for c = 48, 57 do SX_SYMN[c] = true; SX_DIGIT[c] = true; SX_NUMCONT[c] = true end + local sx_buf = {} + local sx_vector = intern("vector") + -- : the caller knows the first code qualifies + local function sx_sym(v) + local n = 0 + local buf, ch, symn = sx_buf, SX_CH, SX_SYMN + repeat + n = n + 1; buf[n] = ch[v[1]] + v = v[2] + until not is_cons(v) or not symn[v[1]] + local s = table.concat(buf, "", 1, n) + if s == "<>" then return "item", v, cons(sx_vector, cons(0, NIL)) end + return "item", v, F["intern"](s) + end + -- with no c# escape and only ASCII, else false + local function sx_str(v) + local n = 0 + local buf, ch = sx_buf, SX_CH + v = v[2] + while is_cons(v) do + local c = v[1] + if c == 34 then return "item", v[2], table.concat(buf, "", 1, n) end + if c == 99 or type(c) ~= "number" or c > 127 or c < 0 then + -- c can begin c#NN; so leave only that case to the kernel's rules + if c ~= 99 or (is_cons(v[2]) and v[2][1] == 35) then return false end + end + n = n + 1; buf[n] = ch[c] + v = v[2] + end + return false + end + -- for a run of at most 15 digits not followed by . or e: exact in a + -- double, so the kernel's sum of digit * 10^k reaches the same value + local function sx_int(v) + local acc, n = 0, 0 + while is_cons(v) do + local c = v[1] + if not SX_DIGIT[c] then + if c == 46 or c == 101 then return false end + break + end + acc = acc * 10 + (c - 48) + n = n + 1 + if n > 15 then return false end + v = v[2] + end + return "item", v, acc + end + -- kind, rest, value like sx_step; false to take the ordered choice + sx_fast = function(v) + if not is_cons(v) then return nil end + local c = v[1] + if SX_WS[c] then + repeat v = v[2] until not is_cons(v) or not SX_WS[v[1]] + return "skip", v + end + if c == 91 then return "open", v[2], SX_LSB end + if c == 40 then return "open", v[2], SX_LRB end + if c == 93 or c == 41 then return nil end -- no alternative takes a closer + if SX_SYM0[c] then + local t = v[2] + local d = is_cons(t) and t[1] + if c == 45 or c == 43 then -- - and +: takes signs + if SX_NUMCONT[d] then return false end + elseif c == 46 then -- .: takes .digits + if SX_DIGIT[d] then return false end + end + return sx_sym(v) + end + if c == 34 then return sx_str(v) end + if SX_DIGIT[c] then return sx_int(v) end + return false + end + end local function sx_step(v, from) + if from == 1 then + local kind, rest, val = sx_fast(v) + if kind ~= false then return kind, rest, val end + end if from <= SX_LSB then local r = F["shen."](v) if r ~= fail_sym then return "open", r[1], SX_LSB end @@ -1498,17 +1766,23 @@ function P.install_native_stdlib() for i = frame.n, 1, -1 do local it = frame[i] if it.alt == SX_LSB then acc = cons(F["shen.cons-form"](it.inner), acc) - elseif it.alt == SX_LRB then acc = F["shen.add-sexpr"](it.inner, acc) + elseif it.alt == SX_LRB then + local inner = it.inner + -- add-sexpr only treats ($ X) specially + if is_cons(inner) and inner[1] == sx_dollar then + acc = F["shen.add-sexpr"](inner, acc) + else + acc = cons(inner, acc) + end else acc = cons(it[1], acc) end end if sp == 0 then return cons(v, cons(acc, NIL)) end local closed = frame frame = stack[sp]; stack[sp] = nil; sp = sp - 1 - local r = F[closed.alt == SX_LSB and "shen." or "shen."](v) - if r ~= fail_sym then + if is_cons(v) and v[1] == (closed.alt == SX_LSB and 93 or 41) then frame.n = frame.n + 1 frame[frame.n] = { alt = closed.alt, inner = acc } - v = r[1] + v = v[2] else -- no closer: the bracket alternative fails; try the next ones there v = closed.pos @@ -2259,6 +2533,7 @@ function P.install_native_stdlib() install("shen.", whitespaces, 1) install("fn", fn_fast, 1) install("hash", hash, 2) + install("shen.mod", shen_mod, 2) install("pr", pr, 2) install("variable?", variable_q, 1) install("fail", fail, 0) @@ -2274,6 +2549,7 @@ function P.install_native_stdlib() install("reverse", reverse, 1) install("shen.map-h", map_h, 3) install("map", map, 2) + install("shen.walk", walk, 2) install("not", native_not, 1) install("boolean?", boolean_q, 1) install("empty?", empty_q, 1) @@ -2501,6 +2777,11 @@ local ENV = { GE = function(a,b) if type(a)=="number" and type(b)=="number" then return a>=b end return F[">="](a,b) end, LE = function(a,b) if type(a)=="number" and type(b)=="number" then return a<=b end return F["<="](a,b) end, EQ = function(a,b) if type(a)=="number" then return a==b end return equal(a,b) end, + -- A loop's accumulator (compiler.lua accumulator): the same values as ADD, + -- SUB and MUL, always as a double. x + -0.0 is x for every x. + ADDF = function(a,b) if type(a)=="number" and type(b)=="number" then return a+(b+NEGZERO) end return F["+"](a,b) end, + SUBF = function(a,b) if type(a)=="number" and type(b)=="number" then return a-(b+NEGZERO) end return F["-"](a,b) end, + MULF = function(a,b) if type(a)=="number" and type(b)=="number" then return a*(b+NEGZERO) end return F["*"](a,b) end, -- CALL0..CALLn are generated below the table (see unknown_call in compiler.lua). -- TRMC step markers (see TRMC in compiler.lua): never Shen values. TRMC_C = setmetatable({}, { __tostring = function() return "#" end }), @@ -2635,12 +2916,24 @@ local ENV = { end return F["n->string"](n) end, + BC = boolean_check, + UPART = UPART, ADDR = function(v, i) local w = v[i + 2] - if w == nil then return F["<-address"](v, i) end + if w == nil or i < 0 then return F["<-address"](v, i) end return w end, - ADSET = function(v, i, x) v[i + 2] = x; return v end, + -- rawset: a table with a metatable (Vmt has no metamethods) makes the trace + -- guard the type of the old value in the slot, so overwriting a vector with + -- values of another type left the trace on every element. + -- The range check reads the length word and not the slot, for the same reason: + -- loading the slot to compare it with nil guards its type too. A write at the + -- slot itself is what F["address->"] checks on the way to raising. A cons has + -- v[1] too, so anything but a vector takes that path whatever its head is. + ADSET = function(v, i, x) + if getmetatable(v) ~= Vmt or i < 0 or i >= v[1] or i % 1 ~= 0 then return F["address->"](v, i, x) end + rawset(v, i + 2, x); return v + end, VMT = Vmt, -- allow compiled code to reach a few Lua builtins safely -- trap-error's pcall restores the stack-segment count (runtime.lua, #93) @@ -2680,20 +2973,75 @@ do .. "end\n" ENV["CALL" .. n] = assert((loadstring or load)(src, "=CALL" .. n))(F, FA, APP, intern) end + -- APP1..APPn: APP with the argument count fixed, so the common exact call + -- is a plain call instead of a vararg forward. Anything else takes APP. + for n = 1, C.MAX_APPN do + local ps = {} + for i = 1, n do ps[i] = "a" .. i end + local plist = table.concat(ps, ", ") + local exact = n == 1 and "ar == 1 or ar == nil" or ("ar == " .. n) + local src = "local F, FA, APP, type, rawget, getmetatable, Symbol = ...\n" + .. "return function(f, " .. plist .. ", ...)\n" + .. " if type(f) ~= 'function' and getmetatable(f) == Symbol then\n" + .. " local g = F[f.name]\n" + .. " if g ~= nil then f = g end\n" + .. " end\n" + .. " if type(f) == 'function' then\n" + .. " local ar = rawget(FA, f)\n" + .. " if " .. exact .. " then return f(" .. plist .. ") end\n" + .. " end\n" + .. " return APP(f, " .. plist .. ")\n" + .. "end\n" + ENV["APP" .. n] = assert((loadstring or load)(src, "=APP" .. n))(F, FA, APP, type, rawget, getmetatable, R.Symbol) + end -- CUR1..CURn: a curried chain ((((fn f) a1) a2) ... an) whose (fn f) has -- already been evaluated to g (see curried_call in compiler.lua). Arity n: -- one direct call. Otherwise the original nested applications, verbatim. + -- CURnP(g, a1..an-1, PREn(g, a1..an-1), an): the same chain when an is not + -- order-free. PREn runs the partial applications right then, before an is + -- evaluated, unless the arity is n, when they cannot run code at all. + local CUR_DIRECT = setmetatable({}, { __tostring = function() return "#" end }) + -- a symbol head: the function it names now, wrapped so CURnP can tell it from a + -- partial application that happens to have arity n. The last argument may rebind + -- the name, and a nested application would already have settled on this one. + local CUR_RESOLVED = { __tostring = function() return "#" end } + for n = 2, C.MAX_CALLN do + local ps = {} + for i = 1, n - 1 do ps[i] = "a" .. i end + local nested = "g" + for i = 1, n - 1 do nested = "APP(" .. nested .. ", a" .. i .. ")" end + local plist = table.concat(ps, ", ") + local src = "local F, FA, APP, D, RESOLVED, rawget, setmetatable, getmetatable, Symbol = ...\n" + .. "return function(g, " .. plist .. ", ...)\n" + .. " if rawget(FA, g) == " .. n .. " then return D end\n" + .. " if getmetatable(g) == Symbol then\n" + .. " local f = F[g.name]\n" + .. " if f ~= nil and rawget(FA, f) == " .. n .. " then return setmetatable({ f }, RESOLVED) end\n" + .. " end\n" + .. " return " .. nested .. "\n" + .. "end, function(g, " .. plist .. ", r, an, ...)\n" + .. " if r == D then return g(" .. plist .. ", an) end\n" + .. " if getmetatable(r) == RESOLVED then return r[1](" .. plist .. ", an) end\n" + .. " return APP(r, an)\n" + .. "end\n" + ENV["PRE" .. n], ENV["CUR" .. n .. "P"] = + assert((loadstring or load)(src, "=CUR" .. n .. "P"))(F, FA, APP, CUR_DIRECT, CUR_RESOLVED, rawget, setmetatable, getmetatable, R.Symbol) + end for n = 1, C.MAX_CALLN do local ps = {} for i = 1, n do ps[i] = "a" .. i end local nested = "g" for i = 1, n do nested = "APP(" .. nested .. ", a" .. i .. ")" end - local src = "local FA, APP = ...\n" - .. "return function(g, " .. table.concat(ps, ", ") .. ")\n" - .. " if FA[g] == " .. n .. " then return g(" .. table.concat(ps, ", ") .. ") end\n" + local src = "local F, FA, APP, rawget, getmetatable, Symbol = ...\n" + .. "return function(g, " .. table.concat(ps, ", ") .. ", ...)\n" + .. " if rawget(FA, g) == " .. n .. " then return g(" .. table.concat(ps, ", ") .. ") end\n" + .. " if getmetatable(g) == Symbol then\n" + .. " local f = F[g.name]\n" + .. " if f ~= nil and rawget(FA, f) == " .. n .. " then return f(" .. table.concat(ps, ", ") .. ") end\n" + .. " end\n" .. " return " .. nested .. "\n" .. "end\n" - ENV["CUR" .. n] = assert((loadstring or load)(src, "=CUR" .. n))(FA, APP) + ENV["CUR" .. n] = assert((loadstring or load)(src, "=CUR" .. n))(F, FA, APP, rawget, getmetatable, R.Symbol) end end @@ -2707,6 +3055,7 @@ if math.tointeger then ENV.SUB = function(a,b) if type(a)=="number" and type(b)=="number" then return a+0.0-b end return F["-"](a,b) end ENV.MUL = function(a,b) if type(a)=="number" and type(b)=="number" then return (a+0.0)*b end return F["*"](a,b) end ENV.DIV = function(a,b) if type(a)=="number" and type(b)=="number" and b~=0 then return (a+0.0)/b end return F["/"](a,b) end + ENV.ADDF, ENV.SUBF, ENV.MULF = ENV.ADD, ENV.SUB, ENV.MUL end local loadstring = loadstring or load @@ -2724,6 +3073,7 @@ local function load_chunk(code, chunkname) chunk, err = load(code, chunkname, nil, ENV) if not chunk then error("Lua load error: "..tostring(err)) end end + if getinfo then COMPILED_SOURCE[getinfo(chunk, "S").source] = true end return chunk end P.load_chunk = load_chunk @@ -2809,13 +3159,9 @@ function P.eval(form) local t = type(form) if t == "number" or t == "string" or t == "boolean" then return form end if form == NIL then return form end - if is_symbol(form) then - -- Bare symbols evaluate to their value in the global var namespace if - -- bound, otherwise to themselves (KL convention). - local v = GLOBALS[form.name] - if v ~= nil then return v end - return form - end + -- A bare symbol evaluates to itself, bound as a global or not: the global + -- namespace is reached only through (value S). + if is_symbol(form) then return form end if t == "table" and not is_cons(form) then -- absvector, stream, exception, or other opaque object: self-evaluating. return form diff --git a/prolog_engine.lua b/prolog_engine.lua index 16275c3..ab2415a 100644 --- a/prolog_engine.lua +++ b/prolog_engine.lua @@ -200,13 +200,26 @@ function M.is_var(v) return v >= VAR_BASE and v < CONS_BASE end function M.is_atom(v) return v < VAR_BASE end -- follow bound-var chains; stops at an unbound var or a non-var -local function lazyderef(v) +-- +-- Chains of more than one step are rare, so the loop stays out of line. A loop +-- here would be an inner loop of every trace through a predicate, and LuaJIT +-- aborts those: on the Prolog benchmark that was most of the aborted traces. +local function deref_chain(v) while v >= VAR_BASE and v < CONS_BASE do local b = vbind[v - VAR_BASE] if b >= 0 then v = b else break end end return v end +local function lazyderef(v) + if v >= VAR_BASE and v < CONS_BASE then + local b = vbind[v - VAR_BASE] + if b < 0 then return v end + if b >= VAR_BASE and b < CONS_BASE then return deref_chain(b) end + return b + end + return v +end M.lazyderef = lazyderef -- --------------------------------------------------------------------------- @@ -493,25 +506,16 @@ M.occurs = occurs -- On structural success thaws cont; if the cont fails, bindings made HERE are -- unwound (batch, to the entry trail mark) — semantically identical to the -- legacy per-bind! unwind chain. Returns the cont's value, or false. -local function unify_core(rootA, rootB, cont, oc) +local function unify_lists(x, y, cont, oc) local n = 1 - gA[0] = rootA; gB[0] = rootB + gA[0] = x; gB[0] = y local tmark = trail_top local tn = tmark local ok = true while n > 0 do n = n - 1 - local x = gA[n] - local y = gB[n] - -- inline lazyderef - while x >= VAR_BASE and x < CONS_BASE do - local b = vbind[x - VAR_BASE] - if b >= 0 then x = b else break end - end - while y >= VAR_BASE and y < CONS_BASE do - local b = vbind[y - VAR_BASE] - if b >= 0 then y = b else break end - end + local x = lazyderef(gA[n]) + local y = lazyderef(gB[n]) if x == y then -- identical atom / same var / same cons cell: success at this goal elseif x >= VAR_BASE and x < CONS_BASE then @@ -559,6 +563,42 @@ local function unify_core(rootA, rootB, cont, oc) end end +-- Most unifications bind one variable or compare atoms, and those need neither +-- the goal stack nor a loop, which would be an inner loop of the caller's trace. +local function unify_core(x, y, cont, oc) + x = lazyderef(x) + y = lazyderef(y) + local idx, val + if x == y then + return thawH(cont) + elseif x >= VAR_BASE and x < CONS_BASE then + if oc and y >= CONS_BASE and occurs(x - VAR_BASE, y) then return false end + idx, val = x - VAR_BASE, y + elseif y >= VAR_BASE and y < CONS_BASE then + if oc and x >= CONS_BASE and occurs(y - VAR_BASE, x) then return false end + idx, val = y - VAR_BASE, x + elseif x >= CONS_BASE and y >= CONS_BASE then + return unify_lists(x, y, cont, oc) + else + return false + end + vbind[idx] = val + local tn = trail_top + if tn >= tcap then trail, tcap = grown(trail, tcap, tn + 1) end + trail[tn] = idx + trail_top = tn + 1 + local d = seg_depth + local r + if d < SEG_FRAMES then seg_depth = d + 1; r = thawH(cont); seg_depth = d + else r = thaw_segment(cont) end + if r == false then + vbind[idx] = -1 + trail_top = tn + return false + end + return r +end + function M.unify(a, b, cont) return unify_core(a, b, cont, false) end -- lzy= function M.unify_oc(a, b, cont) return unify_core(a, b, cont, true) end -- lzy=! diff --git a/run-kernel-tests.lua b/run-kernel-tests.lua index 1b9702b..d27ac1e 100644 --- a/run-kernel-tests.lua +++ b/run-kernel-tests.lua @@ -106,6 +106,20 @@ if not ok1 then end -- Now the big one +-- Count what the harness reports as each test ends. Its own *passed* and *failed* +-- will not do: kerneltests.shen ends with (reset), which zeroes them. +local passed, failed = 0, 0 +for name, count in pairs({ + ["passed"] = function() passed = passed + 1 end, -- exported by its package + ["failed"] = function() failed = failed + 1 end, + ["test-harness.err"] = function() failed = failed + 1 end, +}) do + local orig = assert(P.F[name], name .. " is not defined by the harness") + local wrapped = function(...) count(); return orig(...) end + P.F[name] = wrapped + P.FA[wrapped] = P.FA[orig] +end + print("Loading kerneltests.shen (this runs the actual reports)...") local ok2, e2 = xpcall(function() P.F["load"]("kerneltests.shen") @@ -119,18 +133,21 @@ end) print("\n=== Test run finished ===") print("kerneltests result:", ok2 and "ok" or e2) --- Final counters from the harness. The harness defines *passed* / *failed* --- inside its `(package test-harness ...)` block, so the bare symbol names get --- prefixed to `test-harness.*passed*` / `test-harness.*failed*`. -local function getg(...) - for _, name in ipairs({...}) do - local v = P.GLOBALS[name] - if v ~= nil then return v end - end - return "?" +-- The suite passes only if it finished, nothing failed and every test ran. The number +-- of tests belongs to the kernel release: a new kernel needs an entry here. +-- SHEN_EXPECTED_TESTS is for this script's own spec. +local EXPECTED_BY_KERNEL = { ["43.2"] = 154 } +local version = P.F["version"]() +local EXPECTED = tonumber(os.getenv("SHEN_EXPECTED_TESTS")) or EXPECTED_BY_KERNEL[version] +if not EXPECTED then + print("FAILED: no expected test count for kernel " .. tostring(version) + .. "; add it to EXPECTED_BY_KERNEL in run-kernel-tests.lua") + os.exit(1) +end +print(string.format("Counters: passed=%d failed=%d expected=%d", passed, failed, EXPECTED)) +if not ok2 then print("FAILED: the suite stopped early"); os.exit(1) end +if failed > 0 then print("FAILED: " .. failed .. " test(s) failed"); os.exit(1) end +if passed ~= EXPECTED then + print("FAILED: " .. passed .. " test(s) ran, expected " .. EXPECTED); os.exit(1) end -print(string.format("Counters: passed=%s failed=%s", - getg("test-harness.*passed*", "*passed*"), - getg("test-harness.*failed*", "*failed*"))) - print("\nDone.") diff --git a/runtime.lua b/runtime.lua index d8975f6..e9ed5e7 100644 --- a/runtime.lua +++ b/runtime.lua @@ -327,10 +327,54 @@ do local function pack(...) return { n = select("#", ...), ... } end local S = { SD = SD, FRAMES = SEG_FRAMES } - -- Run f(...) on a fresh coroutine stack and return its value. Errors are - -- rethrown unchanged (Shen exception objects included); a yield from - -- inside (an embedding host's) is passed through to the enclosing - -- coroutine. + -- Run f(...) on a coroutine stack of its own and return its value. Errors + -- are rethrown unchanged (Shen exception objects included); a yield from + -- inside (an embedding host's) is passed through to the enclosing coroutine. + -- + -- Finished segments are reused. A deep Prolog query or recursion opens a + -- segment every few hundred frames, and a fresh coroutine costs its object + -- plus a stack regrown from nothing by doubling. A reused one keeps its + -- stack, which the collector still shrinks when it stays idle. The pcall in + -- seg_body keeps the coroutine alive across an error; DONE tells a finished + -- job from a host's yield. A segment left suspended never comes back here. + local DONE = {} + local pool, npool = {}, 0 + local POOL_MAX = 32 + local function seg_body(f, ...) + return seg_body(co_yield(DONE, pcall(f, ...))) + end + local function seg_host(co, res) -- cold: a host's yield inside f + while res[1] and res[2] ~= DONE do + if co_status(co) == "dead" then break end + local up = pack(pcall(co_yield, unpack(res, 2, res.n))) + if not up[1] then return up end -- no coroutine to yield to + res = pack(co_resume(co, unpack(up, 2, up.n))) + end + return res + end + -- PUC Lua hooks are per thread, and one set during a job would stay on a pooled + -- coroutine. LuaJIT's are global. + local thread_hook = not jit and debug and debug.gethook + local function seg_done(co, saved, saved_n, ...) + local ok, tag, pok, v = ... + if not (ok and tag == DONE) then + local res = seg_host(co, pack(...)) + if res[1] and res[2] == DONE then + ok, tag, pok, v = true, DONE, res[3], res[4] + elseif res[1] then -- body returned: cannot happen + ok, tag, pok, v = true, DONE, true, res[2] + else + ok, tag, pok, v = true, nil, false, res[2] + end + end + -- assign, not decrement: segments abandoned while suspended never return + SD[1], nseg = saved, saved_n + if tag == DONE and npool < POOL_MAX and not (thread_hook and thread_hook(co)) then + npool = npool + 1; pool[npool] = co + end + if not pok then error(v, 0) end + return v + end function S.run(f, ...) if nseg >= SEG_MAX then ERR("Lua stack depth limit exceeded (" .. SEG_MAX .. " stack segments)") @@ -338,17 +382,13 @@ do local saved, saved_n = SD[1], nseg nseg = nseg + 1; SD[1] = 0 total = total + 1 - local co = co_create(f) - local res = pack(co_resume(co, ...)) - while res[1] and co_status(co) ~= "dead" do - local up = pack(pcall(co_yield, unpack(res, 2, res.n))) - if not up[1] then res = up; break end -- no coroutine to yield to - res = pack(co_resume(co, unpack(up, 2, up.n))) + local co + if npool > 0 then + co = pool[npool]; pool[npool] = nil; npool = npool - 1 + else + co = co_create(seg_body) end - -- assign, not decrement: segments abandoned while suspended never return - SD[1], nseg = saved, saved_n - if not res[1] then error(res[2], 0) end - return res[2] + return seg_done(co, saved, saved_n, co_resume(co, f, ...)) end local run = S.run -- S.run for the function that called it (a lambda has no name to pass) diff --git a/scripts/codegen-fuzz.py b/scripts/codegen-fuzz.py new file mode 100644 index 0000000..c55c278 --- /dev/null +++ b/scripts/codegen-fuzz.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +# Writes the deterministic program test/fixtures/codegen-fuzz-112.shen: scripts/codegen-fuzz.py 112 100 +import random,sys +seed=int(sys.argv[1]); nf=int(sys.argv[2]) if len(sys.argv)>2 else 120 +r=random.Random(seed) +POOL=['[[[1 2] [3 4]] [[5] 6] 7 8]','[[[1 2] [3 4]] [[5] 6] 7 8]','[[1 [2 [3 [4]]]] 5]','0','1','2.5','"s"','a','[]','[1]','[1 2]','[[1] 2]','[[1 2] [3]]','[a | b]','[[[1]] | c]','true','false'] +class G: + def __init__(s,vars,fname=None): s.vars=list(vars); s.fname=fname; s.n=0 + def fresh(s): + s.n+=1; return 'V%d'%s.n + def atom(s): + c=r.random() + if c<0.6 and s.vars: return r.choice(s.vars) + return r.choice(POOL[:9]+['1','a']) + def chain(s): + e=r.choice(s.vars) if s.vars else '[1 2]' + for _ in range(r.randint(0,3)): + e='(%s %s)'%(r.choice(['hd','tl']),e) + return e + def b(s,d): + if d<=0: return r.choice(['true','false','(cons? %s)'%s.chain()]) + c=r.randint(0,11) + if c<=3: return '(cons? %s)'%s.chain() + if c==4: return '(and %s %s)'%(s.b(d-1),s.b(d-1)) + if c==5: return '(or %s %s)'%(s.b(d-1),s.b(d-1)) + if c==6: return '(not %s)'%s.b(d-1) + if c==7: return '(= %s %s)'%(s.v(d-1),s.v(d-1)) + if c==8: return '(if %s %s %s)'%(s.b(d-1),s.b(d-1),s.b(d-1)) + if c==9: return '(number? %s)'%s.v(d-1) + if c==10: return '(> (pl %s) %s)'%(s.v(d-1),r.choice(['0','1'])) + return '(let %s %s %s)'%s.letb(d,lambda:s.b(d-1)) + def letb(s,d,k): + v=r.choice(s.vars) if s.vars and r.random()<0.3 else s.fresh() + val=s.v(d-1) + add=v not in s.vars + if add: s.vars.append(v) + body=k() + if add: s.vars.remove(v) + return (v,val,body) + def lam(s,n,d): + vs=[r.choice(s.vars) if s.vars and r.random()<0.2 else s.fresh() for _ in range(n)] + added=[v for v in vs if v not in s.vars] + s.vars.extend(added) + body=s.v(d-1) + for v in set(added): s.vars.remove(v) + return '(/. %s %s)'%(' '.join(vs),body) + def v(s,d): + if d<=0: return s.chain() if r.random()<0.5 else s.atom() + c=r.randint(0,21) + if c<=2: return s.chain() + if c==3: return '[%s | %s]'%(s.v(d-1),s.v(d-1)) + if c<=6: return '(if %s %s %s)'%(s.b(d-1),s.v(d-1),s.v(d-1)) + if c==7: + cl=' '.join('(%s %s)'%(s.b(d-1),s.v(d-1)) for _ in range(r.randint(1,3))) + if r.random()<0.7: cl+=' (true %s)'%s.v(d-1) + return '(cond %s)'%cl + if c==8: return '(let %s %s %s)'%s.letb(d,lambda:s.v(d-1)) + if c==9: return '(tr %s %s)'%(r.randint(0,9),s.v(d-1)) + if c==10: return '(do (tr %s 0) %s)'%(r.randint(0,9),s.v(d-1)) + if c==11: return '(trap-error %s (/. E (cn "E:" (error-to-string E))))'%s.v(d-1) + if c==12: return '(thaw (freeze %s))'%s.v(d-1) + if c==13: # apply lambda directly, group sizes + n=r.randint(1,5); m=r.randint(0,n+1) + args=' '.join(s.v(d-1) for _ in range(m)) + return '(%s %s)'%(s.lam(n,d),args) if m else '(%s)'%s.lam(n,d) + if c==14: # bound function + n=r.randint(1,4); F=s.fresh(); l=s.lam(n,d); m=r.randint(1,n+1) + args=' '.join(s.v(d-1) for _ in range(m)) + return '(let %s %s (%s %s))'%(F,l,F,args) + if c==15: + F=s.fresh(); g=r.choice(['two','three','one','curry2','(two 1)','(three 1 2)','(fn two)','undefinedf','7']); m=r.randint(1,4) + args=' '.join(s.v(d-1) for _ in range(m)) + return '(let %s %s (%s %s))'%(F,g,F,args) + if c==16 and s.fname: + return '(if (> N 0) (%s (- N 1) %s %s) %s)'%(s.fname,s.v(d-1),s.v(d-1),s.v(d-1)) + if c==17 and s.fname: + return '(if (> N 0) (cons %s (%s (- N 1) %s %s)) %s)'%(s.v(d-1),s.fname,s.v(d-1),s.v(d-1),s.v(d-1)) + if c==18 and s.fname: + return '(if (> N 0) (%s (- N 1) %s (/. %s %s)) %s)'%(s.fname,s.v(d-1),'Q',s.v(d-1),s.v(d-1)) + if c==19: return '(two %s %s)'%(s.v(d-1),s.v(d-1)) + if c==20: return '(and %s %s)'%(s.b(d-1),s.b(d-1)) + if c==21: return '(or %s %s)'%(s.b(d-1),s.b(d-1)) + return s.atom() +print('''(set trace []) +(define tr K V -> (do (set trace [K | (value trace)]) V)) +(define pl X -> (if (number? X) X 0)) +(define two A B -> [two A B]) +(define three A B C -> [three A B C]) +(define one A -> [one A]) +(define curry2 A -> (/. B [curry2 A B])) +(define nz V -> (if (= (shen.fnp V) true) fun (if (cons? V) (cons (nz (hd V)) (nz (tl V))) V))) +(define shen.fnp V -> (let S (str-any V) (and (>= (length (explode S)) 5) (= (hdn S 5) "funex")))) +(define hdn S 0 -> "" S N -> (cn (hdstr S) (hdn (tlstr S) (- N 1)))) +(define str-any V -> (trap-error (make-string "~S" V) (/. E "err"))) +(define go Id F -> (do (set trace []) (let R (trap-error (nz (thaw F)) (/. E (cn "ERR " (error-to-string E)))) (output "~A ~S ~S~%" Id R (value trace))))) +''') +inp=[('0',a,b) for a in POOL[:0]] +for i in range(nf): + fn='f%d'%i + style=r.randint(0,3) + g=G(['X','Y','N'] if style!=3 else ['X','Y'], fn if style!=3 else None) + d=r.randint(2,5) + if style==0: + print('(define %s N X Y -> %s)'%(fn,g.v(d))) + elif style==1: + g2=G(['H','T','Y','N'],fn) + print('(define %s N [H | T] Y -> %s\n N X Y -> %s)'%(fn,g2.v(d),g.v(d))) + elif style==2: + g2=G(['A','B','C','Y','N'],fn) + print('(define %s N [[A | B] C] Y -> %s\n N X [A | B] -> %s\n N X Y -> %s)'%(fn,g2.v(d),G(['X','A','B','N'],fn).v(d),g.v(d))) + for k in range(5): + a,b=r.choice(POOL),r.choice(POOL) + if style==3: + print('(go t%d_%d (freeze (let X %s (let Y %s %s))))'%(i,k,a,b,g.v(d))) + else: + print('(go t%d_%d (freeze (%s %d %s %s)))'%(i,k,fn,r.randint(0,3),a,b)) diff --git a/scripts/run-tests.lua b/scripts/run-tests.lua index a62f75f..f465192 100644 --- a/scripts/run-tests.lua +++ b/scripts/run-tests.lua @@ -27,14 +27,22 @@ if not LUA or LUA == "" then LUA = have("luajit") and "luajit" or "lua" end --- Discover specs deterministically (sorted) so the run order is stable. +-- The specs, sorted so the run order is stable; a spec missing here fails the run. local specs = { + "test/accumulator_spec.lua", "test/boot_cache_spec.lua", + "test/calls_spec.lua", + "test/certify_spec.lua", "test/cli_spec.lua", "test/checked_integer_spec.lua", + "test/codegen_diff_spec.lua", + "test/contract_spec.lua", + "test/seg_pool_spec.lua", + "test/gc_pause_spec.lua", "test/datatype_consume_spec.lua", "test/engine_spec.lua", "test/eqterm_spec.lua", + "test/embed_values_spec.lua", "test/error_robustness_spec.lua", "test/eval_order_spec.lua", "test/features_spec.lua", @@ -54,6 +62,7 @@ local specs = { "test/stack_segments_spec.lua", "test/stdlib_spec.lua", "test/tailcall_spec.lua", + "test/system_s_sigf_spec.lua", "test/typecheck_api_spec.lua", "test/typecheck_lazy_spec.lua", "test/utf8_strings_spec.lua", @@ -62,6 +71,18 @@ local specs = { local function sh_quote(s) return "'" .. tostring(s):gsub("'", "'\\''") .. "'" end +-- A spec not in this list would never run, and nothing would say so. +local unlisted = {} +do + local listed = {} + for _, s in ipairs(specs) do listed[s] = true end + local h = io.popen("cd " .. sh_quote(root ~= "" and root or ".") .. " && ls test/*_spec.lua") + for s in h:lines() do + if not listed[s] then unlisted[#unlisted + 1] = s end + end + h:close() +end + -- Run one spec, echo its output, and recover its exit code via a sentinel -- (LuaJIT io.popen:close does not report child status). local function run_spec(path) @@ -100,6 +121,7 @@ end print("============================================") print(string.format("TOTAL: %d pass, %d fail across %d specs", total_pass, total_fail, #specs)) +for _, s in ipairs(unlisted) do failed_specs[#failed_specs + 1] = s .. " (not in the list in scripts/run-tests.lua)" end if #failed_specs > 0 then print("FAILED SPECS:") for _, s in ipairs(failed_specs) do print(" - " .. s) end diff --git a/test/accumulator_spec.lua b/test/accumulator_spec.lua new file mode 100644 index 0000000..a766d86 --- /dev/null +++ b/test/accumulator_spec.lua @@ -0,0 +1,65 @@ +-- test/accumulator_spec.lua: a self-tail loop's accumulator (compiler.lua +-- accumulator) is computed as a double and must keep the meaning of + - *; also native shen.mod. +-- luajit test/accumulator_spec.lua +package.path = (arg[0]:gsub("test/[^/]*$", "")) .. "?.lua;" .. package.path + +local shen = require("shen") +shen.boot{ quiet = true } +local ev = shen.eval + +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 + +ev("(define ac-sum 0 _ A -> A N E A -> (ac-sum (- N 1) E (+ A E)))") +ev("(define ac-rsum 0 _ A -> A N E A -> (ac-rsum (- N 1) E (+ E A)))") +ev("(define ac-sub 0 _ A -> A N E A -> (ac-sub (- N 1) E (- A E)))") +ev("(define ac-mul 0 _ A -> A N E A -> (ac-mul (- N 1) E (* A E)))") + +-- past int32, and as printed text: an integral double prints as an integer +check(ev("(ac-sum 300 10000000 0)") == 3000000000, "sum past 2^31") +check(ev("(str (ac-sum 300 10000000 0))") == "3000000000", "sum prints as an integer") +check(ev("(ac-rsum 300 10000000 0)") == 3000000000, "reversed operands") +check(ev("(ac-sub 300 10000000 0)") == -3000000000, "sub past -2^31") +check(ev("(ac-sub 3 10 100)") == 70, "sub keeps operand order") +check(ev("(ac-mul 40 2 1)") == 2^40, "mul past 2^31") +check(ev("(str (ac-sum 3 2 1))") == "7", "small sum prints as an integer") +check(ev("(str (ac-sum 2 1.5 0))") == "3", "fractions") +check(ev("(str (ac-sum 3 0.5 0))") == "1.5", "fractions kept") +-- enough iterations to compile the loop: same values under a trace +check(ev("(ac-sum 100000 100000 0)") == 10000000000, "sum under a trace") +check(ev("(ac-mul 100000 1 3)") == 3, "mul under a trace") + +-- a value still indexes a vector, compares and hashes as the same number +check(ev("(let V (absvector 3) (do (address-> V (ac-sum 2 1 0) 9) (<-address V 2)))") == 9, + "accumulated value as an index") +check(ev("(= (ac-sum 2 1 0) 2)") == true, "accumulated value compares equal") +check(ev("(do (put ac-key ac-prop 5) (get ac-key ac-prop))") == 5, "get and put unaffected") + +-- operands that are not numbers are still errors, never coerced +local ok = pcall(ev, '(ac-sum 1 "3" 1)') +check(not ok, "string operand in + is an error") +ok = pcall(ev, '(ac-sub 1 "3" 1)') +check(not ok, "string operand in - is an error") +ok = pcall(ev, '(ac-mul 1 "3" 1)') +check(not ok, "string operand in * is an error") + +-- shen.mod: the native path agrees with the kernel's definition, which the +-- second table recomputes by the same doubling +ev("(define ac-mul2 N L -> (if (> (hd L) N) (tl L) (ac-mul2 N [(* 2 (hd L)) | L])))") +ev("(define ac-modh N L -> (cases (= N 0) 0 (empty? L) N (> (hd L) N) (ac-modh N (tl L)) true (ac-modh (- N (hd L)) L)))") +for _, p in ipairs{ {0,10}, {5,10}, {10,10}, {25,10}, {1,1}, {2147483648,7}, + {1e15,7}, {9007199254740991,3}, {1000000007,1000000006} } do + local n, d = string.format("%.0f", p[1]), string.format("%.0f", p[2]) + local got = ev("(shen.mod " .. n .. " " .. d .. ")") + local want = ev("(ac-modh " .. n .. " (ac-mul2 " .. n .. " [" .. d .. "]))") + check(got == want, "shen.mod " .. n .. " " .. d .. ": " .. tostring(got) .. " vs " .. tostring(want)) +end +check(ev("(shen.mod -15 10)") == -15, "shen.mod of a negative stays the kernel's answer") +check(ev("(shen.mod 7.5 2)") == 1.5, "shen.mod of a fraction stays the kernel's answer") +check(not pcall(ev, '(shen.mod "a" 2)'), "shen.mod of a string is an error") + +io.write(string.format("accumulator_spec: %d pass, %d fail\n", npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/boot_cache_spec.lua b/test/boot_cache_spec.lua index aa652f9..d788f7e 100644 --- a/test/boot_cache_spec.lua +++ b/test/boot_cache_spec.lua @@ -120,7 +120,7 @@ do check(ref:find("arity 2", 1, true) ~= nil, "#46: stdlib functions are registered (arity filter = 2)") check(kcold == ref, "#46: cold kernel-bytecode-cache boot == uncached boot") - check(sidecar_data ~= nil and sidecar_data:sub(1, 8) == "SHENSIG1", + check(sidecar_data ~= nil and sidecar_data:sub(1, 8) == "SHENSIG2", "#46: a cold boot writes the stdlib signature sidecar") check(kwarm == ref, "#46: WARM kernel-bytecode-cache + signature sidecar boot == uncached boot") check(kwarm2 == ref, "#46: warm boot with a fresh fasl dir == uncached boot") @@ -129,7 +129,7 @@ do -- or a half-applied replay. do local w = io.open(kcache .. ".stlib", "wb") - w:write((sidecar_data or "SHENSIG1\n"):sub(1, 4000)); w:close() + w:write((sidecar_data or "SHENSIG2\n"):sub(1, 4000)); w:close() local damaged = boot(kc .. " SHEN_FASL_DIR=" .. sh_quote(d1)) check(damaged == ref, "#46: a truncated signature sidecar falls back to the real declares") local again = boot(kc .. " SHEN_FASL_DIR=" .. sh_quote(d1)) @@ -166,6 +166,32 @@ do .. env .. " SHEN_FASL=off luajit " .. shen_bin .. " -e '(+ 1 2)'") end + -- A warm kernel cache built with a codegen switch one way is not reused the + -- other way: the boot rebuilds it, so the file changes. + do + local root = fresh() + local xdg, home = root .. "/xdg", root .. "/home" + local base = "XDG_CACHE_HOME=" .. sh_quote(xdg) .. " HOME=" .. sh_quote(home) + local function cache_data() + local name = ls(xdg .. "/shen-lua"):match("kernel%-%x+%.bin") + local h = name and io.open(xdg .. "/shen-lua/" .. name, "rb") + if not h then return nil end + local d = h:read("*a"); h:close(); return d + end + local out = boot_in(root .. "/cwd", base) + check(out:match("3%s*$") ~= nil, "codegen switches: default boot works") + local before = cache_data() + boot_in(root .. "/cwd", base) + check(before ~= nil and cache_data() == before, "codegen switches: a repeated boot hits the cache") + for _, sw in ipairs({ "SHEN_TRMC", "SHEN_UNCURRY", "SHEN_HOIST_LAMBDA", "SHEN_SINK_COUNT", "SHEN_FREEZE_CONT" }) do + before = cache_data() + out = boot_in(root .. "/cwd", base .. " " .. sw .. "=off") + check(out:match("3%s*$") ~= nil, "codegen switches: boot with " .. sw .. "=off works (got " .. out .. ")") + check(cache_data() ~= before, sw .. "=off does not reuse the default kernel cache") + boot_in(root .. "/cwd", base) + end + end + local root = fresh() local cwd, xdg, home = root .. "/cwd", root .. "/xdg", root .. "/home" local out = boot_in(cwd, "XDG_CACHE_HOME=" .. sh_quote(xdg) .. " HOME=" .. sh_quote(home)) diff --git a/test/calls_spec.lua b/test/calls_spec.lua new file mode 100644 index 0000000..c56094a --- /dev/null +++ b/test/calls_spec.lua @@ -0,0 +1,209 @@ +-- calls_spec.lua : calls through values and the compiled forms behind them. +-- Fixed-arity APPn, CURn over a variable, CURnP, uncurried and raw lambdas, +-- PARTIAL, closed lambdas in KC, count sinking. Each is an optimisation of the +-- curried chain, so each must behave exactly like one. +package.path = (arg[0]:gsub("test/[^/]*$", "")) .. "?.lua;" .. package.path +local shen = require("shen") +local npass, nfail = 0, 0 +local R = require("runtime") +local function show(v) return R.to_str(v) end +local function check(name, src, want) + local ok, got = pcall(function() return show(shen.eval(src)) end) + if ok and got == want then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name .. ": got " .. tostring(got) .. " want " .. want) end +end +local function defs(src) + local ok, e = pcall(shen.eval, src) + if not ok then + nfail = nfail + 1 + print("FAIL define: " .. src .. ": " .. tostring(type(e) == "table" and e.msg or e)) + end +end + +defs("(define app1 F A -> (F A))") +defs("(define app2 F A B -> (F A B))") +defs("(define app3 F A B C -> (F A B C))") +defs("(define clog X -> (do (set call-log [X | (value call-log)]) X))") +defs("(define kx2 -> (/. X Y (cons X Y)))") +defs("(define kx3 -> (/. X Y Z [X Y Z]))") +defs("(define mkc N -> (/. X Y (+ N (+ X Y))))") +defs("(define add2 X Y -> (+ X Y))") + +-- every way of applying a two-parameter lambda +check("lambda, all at once", "(app2 (kx2) 1 2)", "(1 . 2)") +check("lambda, one at a time", "((app1 (kx2) 1) 2)", "(1 . 2)") +check("lambda, partial held", "(let G ((kx2) 1) [(G 2) (G 3)])", "((1 . 2) (1 . 3))") +check("three, every split", "[(app3 (kx3) 1 2 3) ((app2 (kx3) 1 2) 3) (((app1 (kx3) 1) 2) 3) ((app1 (kx3) 1) 2 3)]", + "((1 2 3) (1 2 3) (1 2 3) (1 2 3))") +check("capturing lambda", "(app2 (mkc 10) 1 2)", "13") +check("capturing, partial", "((app1 (mkc 10) 1) 2)", "13") +check("two evaluations capture separately", "(let A (mkc 1) B (mkc 2) [(A 0 0) (B 0 0)])", "(1 2)") +check("shadowed names in a lambda group", "(((/. X (/. X X)) 1) 2)", "2") +check("KL over-application of a 1-ary lambda", + "(eval-kl [[lambda x [lambda y [cons x y]]] 1 2])", "(1 . 2)") +check("KL over-application via a variable", + "(eval-kl [let f [lambda x [let z [+ x 1] [lambda y [cons z y]]]] [f 1 2]])", "(2 . 2)") +check("zero-argument application of a lambda is a partial", + "(eval-kl [let f [lambda x [+ x 1]] [[f] 1]])", "2") + +-- over-application: a 1-ary function returning a function, called with two +defs("(define k1 X -> (let Y (+ X 1) (/. Z (+ Y Z))))") +check("defun over-applied through a variable", "(app2 (function k1) 1 2)", "4") +check("symbol as the function", "(app2 add2 1 2)", "3") +check("symbol, one at a time", "((app1 add2 1) 2)", "3") +check("symbol, over-applied", "(app2 k1 1 2)", "4") +check("(fn f)", "(app2 (fn add2) 1 2)", "3") +check("unknown symbol raises", "(trap-error (app2 nosuchfn 1 2) (/. E caught))", "caught") +check("non-function raises", "(trap-error (app1 1 2) (/. E caught))", "caught") + +-- redefinition is seen through every route, arity change included +defs("(define rd X Y -> (+ X Y))") +defs("(define call-rd F -> (F 1 2))") +check("before redefinition", "(call-rd rd)", "3") +defs("(define rd X Y -> (* 10 (+ X Y)))") +check("after redefinition", "(call-rd rd)", "30") +check("a binding of (fn rd) still calls the new definition", "(let G (fn rd) (do (eval [define rd X Y -> [+ X Y 1000]]) (G 1 2)))", "1003") +check("after redefinition again", "(call-rd rd)", "1003") +defs("(define rd X -> (/. Y (- X Y)))") +check("redefined to a smaller arity", "(call-rd rd)", "-1") +defs("(define rd X Y Z -> (+ X (+ Y Z)))") +check("redefined to a larger arity", "((call-rd rd) 3)", "6") + +-- evaluation order: (F A B) is ((F A) B), so a partial that does something +-- runs before B, and an error from it comes before B runs at all. +defs("(define eff1 X -> (do (clog [ran X]) (/. Y [X Y])))") +defs("(define app-impure F -> (F 1 (clog [arg])))") +check("over-application runs before an impure last argument", + "(do (set call-log []) (let R (app-impure (function eff1)) [R (reverse (value call-log))]))", + "((1 (arg)) ((ran 1) (arg)))") +defs("(define raise1 X -> (error \"first\"))") +check("an error in the partial comes before the last argument", + "(do (set call-log []) [(trap-error (app-impure (function raise1)) (/. E (error-to-string E))) (value call-log)])", + '("first" ())') +check("exact arity: arguments first, then the call", + "(do (set call-log []) (let R (app-impure (/. X Y [X Y])) [R (value call-log)]))", + "((1 (arg)) ((arg)))") +defs("(define hd-last F L -> (F 1 (hd L)))") +check("an impure last argument that raises", "(trap-error (hd-last (/. X Y [X Y]) 5) (/. E caught))", "caught") + +-- tail calls stay proper through every helper: two million would overflow any +-- Lua stack if one of these held a frame +defs("(define tc1 F 0 -> done F N -> (F F (- N 1)))") +defs("(define tc-direct F N -> (tc1 F N))") +check("tail call through APPn / CURn", "(tc1 (/. F N (tc-direct F N)) 2000000)", "done") +defs("(define tc2 F [] -> done F [X | Xs] -> (F F Xs))") +defs("(define nlist 0 A -> A N A -> (nlist (- N 1) [N | A]))") +check("tail call through CURnP", "(let L (nlist 1000000 []) (tc2 (/. F Xs (tc2 F Xs)) L))", "done") +defs("(define tc-lam 0 -> done N -> ((/. X (tc-lam X)) (- N 1)))") +check("tail call out of a closed lambda", "(tc-lam 2000000)", "done") + +-- deep non-tail recursion keeps working, counted or sunk +defs("(define deep-sum 0 -> 0 N -> (+ N (deep-sum (- N 1))))") +if jit then check("sunk count, deep", "(deep-sum 300000)", "45000150000") end -- PUC: C stack overflow, on 912596b too +defs("(define deep-via F 0 -> 0 F N -> (+ 1 (F F (- N 1))))") +if jit then check("through a two-parameter lambda, deep", "(deep-via (/. F N (deep-via F N)) 200000)", "200000") end -- PUC: C stack overflow, on 912596b too +defs("(define deep-1 0 -> 0 N -> (+ 1 ((/. X (deep-1 X)) (- N 1))))") +if jit then check("through a closed lambda, deep", "(deep-1 200000)", "200000") end -- PUC: C stack overflow, on 912596b too +-- A segment restarts its function from the top. Everything a sunk count +-- skips must run once: count what the body does. +defs("(define counted 0 -> 0 N -> (let I (set frames (+ 1 (value frames))) (+ 1 (counted (- N 1)))))") +check("body effects run once per frame across segments", + "(do (set frames 0) (counted 5000) (value frames))", "5000") +defs("(define counted-if N -> (if (= N 0) 0 (do (set frames (+ 1 (value frames))) (+ 1 (counted-if (- N 1))))))") +check("the same under if", "(do (set frames 0) (counted-if 5000) (value frames))", "5000") + +-- CURnP: the function is settled before the last argument runs, as a nested +-- application settles it, however the call is compiled +for _, n in ipairs({ "a1", "a2", "a3", "a4", "a5", "a6" }) do + defs("(define cp-" .. n .. " X Y -> (+ X Y))") + defs("(define cp-r" .. n .. " -> (eval-kl [defun cp-" .. n .. " [(intern \"X\") (intern \"Y\")] 0]))") +end +defs("(define cp-tail F -> (F 1 (do (cp-ra1) 2)))") +defs("(define cp-nontail F -> (+ 0 (F 1 (do (cp-ra2) 2))))") +defs("(define cp-tail-let -> (let F cp-a3 (F 1 (do (cp-ra3) 2))))") +defs("(define cp-nontail-let -> (let F cp-a4 (+ 0 (F 1 (do (cp-ra4) 2)))))") +defs("(define cp-nontail-fn F -> (+ 0 (F 1 (do (cp-ra5) 2))))") +check("symbol head, tail, last argument rebinds", "(cp-tail cp-a1)", "3") +check("symbol head, non-tail, last argument rebinds", "(cp-nontail cp-a2)", "3") +check("let-bound symbol, tail", "(cp-tail-let)", "3") +check("let-bound symbol, non-tail", "(cp-nontail-let)", "3") +-- (fn f) is late bound here, on 912596b and on Shen/JIT alike +check("(fn f), non-tail", "(cp-nontail-fn (fn cp-a5))", "0") +check("after the rebinding the new definition answers", "(cp-a2 1 2)", "0") +check("a result of arity 2 is applied to one more argument", + "(+ 0 ((let F (/. X Y Z (+ X (+ Y Z))) (F 1)) 2 (do (clog 3) 3)))", "6") + +-- a function loaded by the host is called as given, whatever its chunk's name; a +-- compiled lambda carries its arity +do + local P = require("prims") + local APP = P.APP + local load1 = loadstring or load + local host1 = load1("return function(x) return 'host:' .. tostring(x) end")() + local named = load1("return function(x) return 'named:' .. tostring(x) end", "my-chunk")() + local at = load1("return function(x) return 'at:' .. tostring(x) end", "=host")() + local cur = load1("return function(x) return function(y) return tostring(x) .. tostring(y) end end")() + local function same(name, want, ...) + local ok, got = pcall(APP, ...) + if ok and got == want then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name .. ": got " .. tostring(got) .. " want " .. tostring(want)) end + end + same("bare chunk, 2 args", "host:1", host1, 1, 2) + same("bare chunk, 0 args", "host:nil", host1) + same("bare chunk with a name, 2 args", "named:1", named, 1, 2) + same("=named chunk, 2 args", "at:1", at, 1, 2) + local ok, r = pcall(APP, cur, 1, 2) + if ok and type(r) == "function" and r(9) == "19" then npass = npass + 1 + else nfail = nfail + 1; print("FAIL bare curried chunk, 2 args: got " .. tostring(r)) end + local lam = shen.eval("(/. X (/. Y (+ X Y)))") + same("compiled lambda, 2 args", 3, lam, 1, 2) + local again = shen.eval("(/. X (+ X 1))") + same("compiled lambda, 1 arg", 2, again, 1) + defs("(define rawlam-def -> (/. X (/. Y (cons X Y))))") + local fromdef = shen.call("rawlam-def") + local v = APP(fromdef, 1, 2) + if R.is_cons(v) then npass = npass + 1 else nfail = nfail + 1; print("FAIL lambda from a defun, 2 args") end +end + +-- a branch compiled twice: the first attempt's cached accessor locals are gone with +-- its text, and the second must declare its own (count sinking) +defs("(define sk-pick N X Y -> (if (if (cons? (tl X)) (not (cons? Y)) false) (if (= (hd (tl X)) 1) a b) (/. Q N)))") +defs("(define sk-second N X Y -> (if (if (cons? (tl X)) (not (cons? Y)) false) (if (= (hd (tl X)) 41) 42 no) (/. Q N)))") +defs("(define sk-raises N X Y -> (if (if (cons? X) (not (cons? Y)) false) (if (= (hd (tl (tl N))) 1) a no) (/. Q N)))") +check("a then-branch reads its accessor after a discarded attempt", "(sk-pick 0 [0 1] 0)", "a") +check("the same with another value", "(sk-second 0 [0 41] 0)", "42") +check("an accessor that must raise still raises", + "(trap-error (sk-raises 7 [0] 0) (/. E raised))", "raised") +-- trap-error's fast path compiles its default before it knows it applies +defs("(define sk-trap X Y -> (trap-error (value (hd (tl X))) (/. E (if (= (hd (tl (tl Y))) 1) a b))))") +defs("(set sk-v 9)") +check("trap-error with an operand that is not a variable", "(sk-trap [0 sk-v] [0 0 1])", "9") + +-- a compile that raises leaves no facts behind +do + local C = require("compiler") + local before = C.proven_count() + local P = require("prims") + local form = P.F["hd"](P.F["read-from-string"]("(defun pv-bad (A) (if (cons? A) (do) 1))")) + local ok = pcall(C.compile_top, form) + if not ok then npass = npass + 1 else nfail = nfail + 1; print("FAIL the malformed form did not raise") end + if C.proven_count() == before then npass = npass + 1 + else nfail = nfail + 1; print("FAIL proven facts left after a failed compile: " .. C.proven_count()) end +end + +-- a host function has no recorded arity and is called as given +local P = require("prims") +P.F["host-count"] = function(...) return select("#", ...) end +check("host function through a symbol", "(app1 host-count 1)", "1") +-- fn and function go through the kernel's arity table, which a host function is +-- not in. They raise, as they did before the fixed-arity helpers. +for _, src in ipairs({ "(function host-count)", "(fn host-count)" }) do + local ok, err = pcall(shen.eval, src) + local msg = not ok and tostring(type(err) == "table" and err.msg or err) or "" + local raised = not ok and msg:find("host-count is undefined", 1, true) ~= nil + if raised then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. src .. ": " .. msg) end +end + +print(("calls_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/certify_spec.lua b/test/certify_spec.lua new file mode 100644 index 0000000..c7e6051 --- /dev/null +++ b/test/certify_spec.lua @@ -0,0 +1,54 @@ +-- certify_spec.lua : make certify can fail. Runs run-kernel-tests.lua over a tiny +-- suite (SHEN_TESTS_DIR) that passes, fails, stops early and runs too few tests. +local here = (arg[0]:gsub("test/[^/]*$", "")) +if here == "" then here = "./" end +local npass, nfail = 0, 0 +local function check(cond, name) + if cond then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name) end +end +local function sh_quote(s) return "'" .. tostring(s):gsub("'", "'\\''") .. "'" end +local LUA = (arg[-1] or "luajit") + +local dir = os.tmpname(); os.remove(dir); os.execute("mkdir -p " .. sh_quote(dir)) +os.execute("cp " .. sh_quote(here .. "tests/harness.shen") .. " " .. sh_quote(dir .. "/harness.shen")) +local function suite(body) + local f = assert(io.open(dir .. "/kerneltests.shen", "w")); f:write(body); f:close() +end +local function run(expected) + local cmd = "cd " .. sh_quote(here) .. " && SHEN_TESTS_DIR=" .. sh_quote(dir) + .. " SHEN_EXPECTED_TESTS=" .. expected .. " SHEN_FASL=off " .. LUA .. " run-kernel-tests.lua" + local h = io.popen("{ " .. cmd .. " ; } 2>&1; echo \"__EXIT__:$?\"") + local out = h:read("*a"); h:close() + return tonumber(out:match("__EXIT__:(%d+)%s*$")), out +end +local function case(name, body, expected, want, counters) + suite(body) + local code, out = run(expected) + check(code == want, name .. ": exit " .. tostring(code) .. ", wanted " .. want .. "\n" .. out:sub(-300)) + check(out:find(counters, 1, true) ~= nil, name .. ": counters, wanted " .. counters) +end +local good = '(report "t" 1 1 2 2)\n(reset)\n' +case("all pass", good, 2, 0, "Counters: passed=2 failed=0") +case("a wrong answer", '(report "t" 1 1 2 3)\n(reset)\n', 2, 1, "Counters: passed=1 failed=1") +case("an error in a test", '(report "t" 1 1 (simple-error "x") 2)\n(reset)\n', 2, 1, "Counters: passed=1 failed=1") +case("too few tests ran", good, 3, 1, "Counters: passed=2 failed=0") +case("the suite stops early", '(report "t" 1 1)\n(this is not closed\n', 1, 1, "Counters: passed=") +-- a kernel with no count on record fails and says what to do +do + suite(good) + local src = assert(io.open(here .. "run-kernel-tests.lua")):read("*a") + src = src:gsub('%["43%.2"%] = 154', '["0.0"] = 1') + local copy = dir .. "/rkt.lua" + local f = assert(io.open(copy, "w")); f:write(src); f:close() + local cmd = "cd " .. sh_quote(here) .. " && SHEN_TESTS_DIR=" .. sh_quote(dir) + .. " SHEN_FASL=off " .. LUA .. " " .. sh_quote(copy) + local h = io.popen("{ " .. cmd .. " ; } 2>&1; echo \"__EXIT__:$?\"") + local out = h:read("*a"); h:close() + check(tonumber(out:match("__EXIT__:(%d+)%s*$")) == 1, "unknown kernel: exit 1\n" .. out:sub(-200)) + check(out:find("no expected test count for kernel", 1, true) ~= nil, "unknown kernel: says why") +end +os.execute("rm -rf " .. sh_quote(dir)) + +print(("certify_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/cli_spec.lua b/test/cli_spec.lua index a5b1731..becb8dc 100644 --- a/test/cli_spec.lua +++ b/test/cli_spec.lua @@ -479,5 +479,139 @@ shen.call("load", %q) os.remove(fpath); os.remove(lpath) end +-- --------------------------------------------------------------------------- +-- A fasl stores shen.*userdefs* as one push per define, not the whole list +-- each time, which made it quadratic in the file's definitions. A hit must +-- rebuild the same list, redefinitions included. +-- --------------------------------------------------------------------------- +do + local fdir = os.tmpname(); os.remove(fdir) + local p = os.tmpname() .. ".shen" + local parts = { "(define ud-a -> 1)\n(define ud-b -> 2)\n(define ud-a -> 3)\n" } + for i = 1, 300 do parts[#parts + 1] = "(define ud-f" .. i .. " -> " .. i .. ")\n" end + local fh = io.open(p, "w"); fh:write(table.concat(parts)); fh:close() + local cmd = SHEN .. " --hush-load " .. sh_quote(p) + .. " -e " .. sh_quote("[(value shen.*userdefs*) (ud-a) (ud-f300)]") + local envp = "env SHEN_FASL_DIR=" .. sh_quote(fdir) .. " " + local cold, ccode = run(envp .. cmd) + local warm, wcode = run(envp .. "SHEN_FASL_DEBUG=1 " .. cmd) + local off = run("env SHEN_FASL=off " .. cmd) + check(ccode == 0 and wcode == 0, "userdefs: cold and warm runs exit 0") + check(warm:find("[fasl] hit", 1, true) ~= nil, "userdefs: second run is a fasl HIT") + warm = warm:gsub("%[fasl%][^\n]*\n", "") + check(cold:find("ud-f300", 1, true) ~= nil, "userdefs: the list is printed") + check(cold == off, "userdefs: cold run matches SHEN_FASL=off") + check(warm == off, "userdefs: warm hit rebuilds the same list") + local total = 0 + local ls = io.popen("cat " .. sh_quote(fdir) .. "/*.fasl | wc -c") + total = tonumber(ls:read("*a")); ls:close() + check(total and total < 250000, "userdefs: fasl of 300 defines stays linear, got " .. tostring(total)) + os.remove(p); os.execute("rm -rf " .. sh_quote(fdir)) +end + +-- --------------------------------------------------------------------------- +-- A fasl hit skips declare's variancy query when the name has no signature yet, +-- which is the case with nothing to warn about. Changing a type still warns the +-- same cold, warm and with SHEN_FASL=off, and the signature takes effect. +-- --------------------------------------------------------------------------- +do + local fdir = os.tmpname(); os.remove(fdir) + local p = os.tmpname() .. ".shen" + local fh = io.open(p, "w") + fh:write("(define var-g {number --> number} X -> X)\n" + .. "(define var-g {string --> string} X -> X)\n" + .. "(define var-h {number --> number} X -> (+ X 1))\n"); fh:close() + local lp = os.tmpname() .. ".lua" + fh = io.open(lp, "w") + fh:write("package.path = ", string.format("%q", here .. "?.lua;"), " .. package.path\n", + "local shen = require('shen')\nshen.boot{quiet = true}\n", + "shen.eval('(tc +)')\nshen.call('load', ", string.format("%q", p), ")\nshen.eval('(tc -)')\n", + "print('new type', shen.typecheck('(var-g \"s\")', 'string') ~= false)\n", + "print('old type', shen.typecheck('(var-g 1)', 'number') ~= false)\n", + "print('other', shen.typecheck('(var-h 1)', 'number') ~= false)\n") + fh:close() + local cmd = "luajit " .. sh_quote(lp) + local envp = "env SHEN_FASL_DIR=" .. sh_quote(fdir) .. " " + local function warnings(out) + local n = 0 + for _ in out:gmatch("changing the type of var%-g") do n = n + 1 end + return n + end + local cold, ccode = run(envp .. cmd) + local warm, wcode = run(envp .. "SHEN_FASL_DEBUG=1 " .. cmd) + local off = run("env SHEN_FASL=off " .. cmd) + check(ccode == 0 and wcode == 0, "variancy: cold and warm runs exit 0") + check(warm:find("[fasl] hit", 1, true) ~= nil, "variancy: second run is a fasl HIT") + check(warnings(off) == 1, "variancy: the warning is printed once: " .. off) + check(warnings(cold) == 1, "variancy: cold run warns as SHEN_FASL=off does") + check(warnings(warm) == 1, "variancy: warm hit warns as SHEN_FASL=off does") + for what, out in pairs({ off = off, cold = cold, warm = warm }) do + check(out:find("new type\ttrue", 1, true) ~= nil, "variancy: " .. what .. ": the new type holds") + check(out:find("old type\tfalse", 1, true) ~= nil, "variancy: " .. what .. ": the old type is gone") + check(out:find("other\ttrue", 1, true) ~= nil, "variancy: " .. what .. ": the other signature holds") + end + os.remove(lp) + os.remove(p); os.execute("rm -rf " .. sh_quote(fdir)) +end + +-- --------------------------------------------------------------------------- +-- The same for a name with no signature that a user datatype rule types through +-- (fn F): its warning does not depend on the cache either. +-- --------------------------------------------------------------------------- +do + local fdir = os.tmpname(); os.remove(fdir) + local p = os.tmpname() .. ".shen" + local fh = io.open(p, "w") + fh:write("(datatype var-dt\n ______\n (fn var-d) : (number --> number);)\n" + .. "(define var-d {string --> string} X -> X)\n" + .. "(define var-e {string --> string} X -> X)\n"); fh:close() + local lp = os.tmpname() .. ".lua" + fh = io.open(lp, "w") + fh:write("package.path = ", string.format("%q", here .. "?.lua;"), " .. package.path\n", + "local shen = require('shen')\nshen.boot{quiet = true}\n", + "shen.eval('(tc +)')\nshen.call('load', ", string.format("%q", p), ")\nshen.eval('(tc -)')\n") + fh:close() + local cmd = "luajit " .. sh_quote(lp) + local envp = "env SHEN_FASL_DIR=" .. sh_quote(fdir) .. " " + local function count(out, what) + local n = 0 + for _ in out:gmatch("changing the type of " .. what) do n = n + 1 end + return n + end + local off = run("env SHEN_FASL=off " .. cmd) + local cold = run(envp .. cmd) + local warm = run(envp .. "SHEN_FASL_DEBUG=1 " .. cmd) + check(warm:find("[fasl] hit", 1, true) ~= nil, "variancy (fn F): second run is a fasl HIT") + check(count(off, "var%-d") == 1, "variancy (fn F): warns without a fasl: " .. off) + check(count(cold, "var%-d") == 1, "variancy (fn F): cold run warns: " .. cold) + check(count(warm, "var%-d") == 1, "variancy (fn F): warm hit warns: " .. warm) + check(count(warm, "var%-e") == 0, "variancy (fn F): no warning for a name no rule types") + os.remove(lp) + os.remove(p); os.execute("rm -rf " .. sh_quote(fdir)) +end + +-- --------------------------------------------------------------------------- +-- A macro's (set ...) replays on a hit even if the global already held the value when +-- the file was compiled: the session replaying it may not hold it. +-- --------------------------------------------------------------------------- +do + local fdir = os.tmpname(); os.remove(fdir) + local p = os.tmpname() .. ".shen" + local fh = io.open(p, "w") + fh:write("(defmacro fa-mm [fa-mark X] -> (do (set fa-cfg 5) X))\n" + .. '(output "~S~%" (fa-mark 1))\n' + .. '(output "cfg=~S~%" (trap-error (value fa-cfg) (/. E unset)))\n'); fh:close() + local envp = "env SHEN_FASL_DIR=" .. sh_quote(fdir) .. " " + local shen_bin = "luajit " .. sh_quote(here .. "bin/shen") + local off = run("env SHEN_FASL=off " .. shen_bin .. " --hush-load " .. sh_quote(p)) + local cold = run(envp .. shen_bin .. " -e '(set fa-cfg 5)' --hush-load " .. sh_quote(p)) + local warm = run(envp .. "SHEN_FASL_DEBUG=1 " .. shen_bin .. " --hush-load " .. sh_quote(p)) + check(off:find("cfg=5", 1, true) ~= nil, "macro set: uncached: " .. off) + check(cold:find("cfg=5", 1, true) ~= nil, "macro set: compile run: " .. cold) + check(warm:find("[fasl] hit", 1, true) ~= nil, "macro set: second run is a fasl HIT: " .. warm) + check(warm:find("cfg=5", 1, true) ~= nil, "macro set: replay in a fresh session matches: " .. warm) + os.remove(p); os.execute("rm -rf " .. sh_quote(fdir)) +end + io.write(string.format("cli_spec: %d pass, %d fail\n", npass, nfail)) os.exit(nfail == 0 and 0 or 1) diff --git a/test/codegen_diff_spec.lua b/test/codegen_diff_spec.lua new file mode 100644 index 0000000..da5b37b --- /dev/null +++ b/test/codegen_diff_spec.lua @@ -0,0 +1,147 @@ +-- codegen_diff_spec.lua : compiled output against itself. Every optimisation switch off +-- gives the same results as all on, over a fixed fuzz program and an exhaustive set of +-- shapes built to reach count sinking's second compile. And no compiled chunk reads a +-- global that is not in the compiled environment: a local whose declaration got lost +-- reads as nil, with no error at all. +local here = (arg[0]:gsub("test/[^/]*$", "")) +if here == "" then here = "./" end +package.path = here .. "?.lua;" .. package.path +local npass, nfail = 0, 0 +local function check(cond, name) + if cond then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name) end +end +local function sh_quote(s) return "'" .. tostring(s):gsub("'", "'\\''") .. "'" end +local LUA = arg[-1] or "luajit" +local dir = os.tmpname(); os.remove(dir); os.execute("mkdir -p " .. sh_quote(dir)) + +-- the shapes: test x then x else, with arguments that reach each branch +local TESTS = { + "(if (cons? (tl (tl X))) (not (cons? Y)) false)", "(if (cons? (tl X)) (not (cons? Y)) false)", + "(if (cons? (tl (tl X))) (not (= Y 1)) false)", "(if (= Y 1) (not (cons? (tl (tl X)))) false)", + "(not (if (cons? (tl (tl X))) (not (cons? Y)) false))", + "(if (cons? (tl (tl X))) (not (cons? Y)) (or (cons? Y) false))", + "(if (cons? (tl (tl X))) (not (cons? Y)) (not (cons? N)))", + "(or (not (cons? Y)) (if (cons? (tl (tl X))) true false))", + "(and (not (cons? Y)) (cons? (tl (tl X))))", "(and (cons? (tl (tl X))) (not (cons? Y)))", + "(or (cons? (tl (tl X))) (not (cons? Y)))", +} +local THENS = { + "(if (number? (hd (tl N))) 1 2)", "(hd (tl N))", "(if (cons? (tl N)) (hd (tl N)) 0)", + "(if (= (hd (tl N)) 1) (hd (tl N)) (tl (tl N)))", "(if (= (hd (tl X)) 1) a b)", +} +local ELSES = { + "(/. Q N)", "(/. Q Q)", "(/. Q (/. R N))", "(freeze N)", "(/. Q (sf N))", "(sf N)", "(+ 1 (sf N))", + "(let A (/. Q N) A)", "0", +} +local ARGS = { + "[0 1 2] [0 1 2] 0", "[0 1] [0 1] 0", "0 [0 1] 0", "[0 1 2] [0] [1]", "7 [0] 0", "[1 1] 5 1", +} +local shapes = dir .. "/shapes.shen" +do + local f = assert(io.open(shapes, "w")) + f:write("(define sf X -> X)\n", + "(define show F -> (trap-error (let V (thaw F) (if (or (cons? V) (number? V) (symbol? V)) V fn)) (/. E raised)))\n") + local n = 0 + for _, t in ipairs(TESTS) do for _, th in ipairs(THENS) do for _, e in ipairs(ELSES) do + n = n + 1 + f:write("(define x", n, " N X Y -> (if ", t, " ", th, " ", e, "))\n") + for i, a in ipairs(ARGS) do + f:write('(output "', n, ".", i, " ~S~%\" (show (freeze (x", n, " ", a, "))))\n") + end + end end end + f:close() +end +local function run(path, sw) + local cache = dir .. "/kc-" .. (sw or "all") .. ".bin" + local cmd = "env SHEN_FASL=off SHEN_KERNEL_CACHE=" .. sh_quote(cache) .. (sw and (" " .. sw .. "=off") or "") + .. " " .. LUA .. " " .. sh_quote(here .. "bin/shen") .. " --hush-load " .. sh_quote(path) .. " 2>&1" + local h = io.popen(cmd); local out = h:read("*a"); h:close() + return (out:gsub("funex%d+", "funex"):gsub("[^ \n]*:%d+: ", "")) +end +local SWITCHES = { "SHEN_SINK_COUNT", "SHEN_UNCURRY", "SHEN_HOIST_LAMBDA", "SHEN_TRMC", "SHEN_FREEZE_CONT" } +local fuzz = here .. "test/fixtures/codegen-fuzz-112.shen" +for _, prog in ipairs({ { "shapes", shapes }, { "fuzz", fuzz } }) do + local want = run(prog[2]) + check(#want > 1000, prog[1] .. ": the program ran, got " .. want:sub(1, 200)) + for _, sw in ipairs(SWITCHES) do + local got = run(prog[2], sw) + if got == want then npass = npass + 1 else + local a, b = want:gmatch("[^\n]+"), got:gmatch("[^\n]+") + local x, y = a(), b() + while x and x == y do x, y = a(), b() end + nfail = nfail + 1 + print("FAIL " .. prog[1] .. " with " .. sw .. "=off differs:\n on: " .. tostring(x) .. "\n off: " .. tostring(y)) + end + end +end + +-- no undeclared global, in the kernel, the stdlib, the shapes and the fuzz program +if jit then + local getenv = os.getenv + local fresh = dir .. "/kc-scan.bin" + os.getenv = function(k) + if k == "SHEN_KERNEL_CACHE" then return fresh end + if k == "SHEN_FASL" then return "off" end + return getenv(k) + end + local C = require("compiler") + local chunks = {} + for _, name in ipairs({ "compile_top", "compile_expr_chunk" }) do + local orig = C[name] + C[name] = function(...) + local src = orig(...) + chunks[#chunks + 1] = src + return src + end + end + local shen = require("shen") + shen.boot{ quiet = true } + local nkernel = #chunks + check(nkernel > 500, "the scan saw the kernel compile, got " .. nkernel) + shen.prims.HUSH_LOAD_ECHO = true + -- what the programs print is not what this part checks + shen.prims.GLOBALS["*stoutput*"] = shen.call("open", "/dev/null", shen.runtime.intern("out")) + local function quiet_load(p) return pcall(shen.call, "load", p) end + local vmdef, util = require("jit.vmdef"), require("jit.util") + local bcnames = vmdef.bcnames + local function opname(ins) local i = ins % 256; return bcnames:sub(i * 6 + 1, i * 6 + 6):gsub(" ", "") end + local function scan(proto, seen) + local info = util.funcinfo(proto) + for pc = 1, info.bytecodes - 1 do + local ins = util.funcbc(proto, pc) + if not ins then break end + local op = opname(ins) + if op == "GGET" or op == "GSET" then + local d = math.floor(ins / 65536) + seen[util.funck(proto, -d - 1)] = true + end + end + for i = 1, info.gcconsts do + local k = util.funck(proto, -i) + if type(k) == "proto" then scan(k, seen) end + end + end + quiet_load(shapes) + quiet_load(fuzz) + local ENV, bad, n = shen.prims.ENV, {}, 0 + for _, src in ipairs(chunks) do + n = n + 1 + local fn = assert((loadstring or load)(src, "=scan")) + local seen = {} + scan(fn, seen) + for g in pairs(seen) do + if rawget(ENV, g) == nil and not bad[g] then bad[g] = src:sub(1, 120) end + end + end + check(n > nkernel + 500, "the scan saw the test programs, got " .. (n - nkernel)) + for g, s in pairs(bad) do + nfail = nfail + 1 + print("FAIL undeclared global " .. tostring(g) .. " in: " .. s) + end + if next(bad) == nil then npass = npass + 1 end +end + +os.execute("rm -rf " .. sh_quote(dir)) +print(("codegen_diff_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/contract_spec.lua b/test/contract_spec.lua new file mode 100644 index 0000000..daea7a5 --- /dev/null +++ b/test/contract_spec.lua @@ -0,0 +1,144 @@ +-- test/contract_spec.lua: PORT-AUTHORED regressions for bugs found by running +-- Shen/JIT's contract examples (contract/examples.shen in that repo) on +-- shen-lua. Those examples are one author's rules, not a conformance suite, so +-- only the cases judged real bugs are locked in here. +-- +-- luajit test/contract_spec.lua +local shen = require("shen") +shen.boot{ quiet = true } + +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 eval(src) return shen.eval(src) end +-- The error message of a caught error, or nil when nothing was raised. +local function caught(src) + local ok, res = pcall(shen.eval, '(trap-error (do ' .. src .. ' no-error) (/. E (error-to-string E)))') + if not ok then return "UNCAUGHT: " .. tostring(res) end + if type(res) == "string" then return res end + return nil +end + +-- absvector validates its length --------------------------------------------- +for _, bad in ipairs({ "-1", "1.5", "a", '"1"', "[]" }) do + local msg = caught("(absvector " .. bad .. ")") + check(msg and msg:find("not a valid vector length", 1, true) ~= nil, + "(absvector " .. bad .. ") raises a catchable length error, got " .. tostring(msg)) +end +check(eval("(absvector? (absvector 0))") == true, "(absvector 0) is still legal") +check(eval("(absvector? (absvector 2))") == true, "(absvector 2) is still legal") + +-- A form the compiler rejects leaves the compiler usable ------------------------ +eval("(defun contract-g (x) x)") +for _, bad in ipairs({ + "[defun contract-g [x] [lambda]]", -- inside a defun + "[defun contract-g [x y] [lambda]]", -- with a different arity + "[lambda]", -- outside any defun +}) do + check(caught("(eval-kl " .. bad .. ")") ~= nil, bad .. " is an error") + -- Forms that hoist a control form (KC) or need a depth counter (NT) referred + -- to names the chunk does not declare, once the state was left behind. + check(eval('(eval-kl [let v [absvector 1] [do [address-> v 0 2.5] [vector? v]]])') == true, + "after " .. bad .. ": a do in a let compiles") + check(eval("(= [1 2] (eval-kl [[fn append] [cons 1 []] [cons 2 []]]))") == true, + "after " .. bad .. ": a closure call compiles") + check(eval("(contract-g 5)") == 5, "after " .. bad .. ": the old definition still runs") + check(eval("(eval-kl [contract-g 5])") == 5, + "after " .. bad .. ": a new call compiles with the old arity") + eval("(define contract-h X -> (+ X 1))") + check(eval("(contract-h 1)") == 2, "after " .. bad .. ": define still works") +end + +-- A global is named by a symbol, nothing else ------------------------------------ +for _, bad in ipairs({ '1', '"contract-v"', '[]', '(absvector 1)' }) do + check((caught("(set " .. bad .. " 2)") or ""):find("is not a symbol", 1, true) ~= nil, + "(set " .. bad .. " 2) raises") + check((caught("(value " .. bad .. ")") or ""):find("is not a symbol", 1, true) ~= nil, + "(value " .. bad .. ") raises") +end +check((caught("(value contract-v)") or ""):find("has no value", 1, true) ~= nil, + "a rejected (set \"contract-v\" 2) did not bind the symbol contract-v") +eval("(set contract-v 3)") +check(eval("(value contract-v)") == 3, "(set sym v) still works") +check(eval("(let S contract-v (value S))") == 3, "(value S) through a variable still works") + +-- A symbol is self-evaluating, whether or not it names a global ------------------- +eval("(set contract-sym 7)") +check(eval("(= (eval-kl contract-sym) contract-sym)") == true, "(eval-kl sym) is sym") +check(eval("(= (eval contract-sym) contract-sym)") == true, "(eval sym) is sym") +check(eval("(= (eval-kl [if true contract-sym 1]) contract-sym)") == true, "a compiled symbol is sym") +check(eval("(eval-kl [value contract-sym])") == 7, "(value sym) still reads the global") + +-- hash takes a positive integer bound; these used to hang or return NaN ----------- +for _, key in ipairs({ "1", "a", '"a"' }) do + for _, bound in ipairs({ "0", "-1", "0.5", "a" }) do + local src = "(hash " .. key .. " " .. bound .. ")" + check((caught(src) or ""):find("not a positive integer", 1, true) ~= nil, src .. " raises") + end + local h = eval("(hash " .. key .. " 10)") + check(h >= 1 and h <= 10, "(hash " .. key .. " 10) is in range") +end + +-- close is idempotent, and a closed stream is an error ----------------------------- +do + local tmp = os.tmpname() + local q = '"' .. tmp .. '"' + eval("(close (open " .. q .. " out))") + check(caught("(let S (open " .. q .. " in) (do (close S) (close S)))") == nil, "close twice on input") + check(caught("(let S (open " .. q .. " out) (do (close S) (close S)))") == nil, "close twice on output") + check((caught("(let S (open " .. q .. " in) (do (close S) (read-byte S)))") or ""):find("stream is closed", 1, true) ~= nil, + "read-byte on a closed stream") + check((caught("(let S (open " .. q .. " in) (do (read-byte S) (close S) (read-byte S)))") or ""):find("stream is closed", 1, true) ~= nil, + "read-byte on a closed stream that had reached EOF") + check((caught("(let S (open " .. q .. " out) (do (close S) (write-byte 65 S)))") or ""):find("stream is closed", 1, true) ~= nil, + "write-byte on a closed stream") + check((caught("(close 1)") or ""):find("not a stream", 1, true) ~= nil, "(close 1) raises") + os.remove(tmp) +end + +-- get-time: unix is the Unix time, and an unknown option raises -------------------- +check(math.abs(eval("(get-time unix)") - os.time()) <= 2, "(get-time unix) is the Unix time") +check(type(eval("(get-time run)")) == "number", "(get-time run) is a number") +check(type(eval("(get-time real)")) == "number", "(get-time real) is a number") +for _, bad in ipairs({ "sideways", "1", '"unix"' }) do + check((caught("(get-time " .. bad .. ")") or ""):find("invalid option", 1, true) ~= nil, "(get-time " .. bad .. ") raises") +end + +-- str of a string adds the quotes ---------------------------------------------- +check(eval('(str "a")') == '"a"', '(str "a") is quoted') +check(eval('(str "")') == '""', '(str "") is quoted') +check(eval('(make-string "~A" "a")') == 'a', "~A still prints a string bare") +check(eval('(make-string "~S" "a")') == '"a"', "~S still quotes a string") +check(eval('(= (concat a 1) a1)') == true, "concat of atoms is unchanged") + +-- address-> and <-address stay inside the vector ----------------------------------- +for _, form in ipairs({ + "(address-> (absvector 1) 1 x)", "(address-> (absvector 1) 5 x)", + "(address-> (absvector 1) -1 x)", "(address-> (absvector 1) 0.5 x)", + "(address-> (absvector 0) 0 x)", "(<-address (absvector 1) -1)", + "(<-address (absvector 1) 1)", "(<-address (absvector 0) 0)", +}) do + check((caught(form) or ""):find("is not a valid index", 1, true) ~= nil, form .. " raises") +end +eval("(define contract-store V I X -> (address-> V I X))") +eval("(define contract-load V I -> (<-address V I))") +check((caught("(contract-store (absvector 1) 1 x)") or ""):find("is not a valid index", 1, true) ~= nil, + "a compiled address-> past the end raises") +check((caught("(contract-load (absvector 1) -1)") or ""):find("is not a valid index", 1, true) ~= nil, + "a compiled <-address reads no length word") +check(eval("(contract-load (contract-store (absvector 2) 1 7) 1)") == 7, "in-range slots still work") +check(eval("(let F address-> (<-address (F (absvector 1) 0 8) 0))") == 8, "address-> through a variable") +check((caught("(let F address-> (F (absvector 1) 3 8))") or ""):find("is not a valid index", 1, true) ~= nil, + "address-> through a variable checks too") +check((caught("(address-> (absvector 2) 2 8)") or ""):find("is not a valid index", 1, true) ~= nil, "address-> one past the end raises") +check((caught("(address-> (absvector 2) 1.5 8)") or ""):find("is not a valid index", 1, true) ~= nil, "address-> at a fraction raises") +check((caught("(address-> (cons 5 []) 3 8)") or ""):find("is not a valid index", 1, true) ~= nil, "address-> past a cons with a number at its head raises") + +io.write(string.format("contract_spec: %d passed, %d failed\n", npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/embed_values_spec.lua b/test/embed_values_spec.lua new file mode 100644 index 0000000..6a2ee3c --- /dev/null +++ b/test/embed_values_spec.lua @@ -0,0 +1,68 @@ +-- embed_values_spec.lua : a compiled function value can be called raw from Lua with +-- fewer arguments than it takes, wherever the host got it, and gives a partial: never +-- a body run with nil. Its identity is unchanged, so passing it back into Shen, storing +-- it and comparing it with = all behave as for any other value. +package.path = (arg[0]:gsub("test/[^/]*$", "")) .. "?.lua;" .. package.path +local shen = require("shen") +shen.boot{quiet = true} +local R = shen.runtime +local npass, nfail = 0, 0 +local function check(name, f, want) + local ok, got = pcall(f) + if ok and got == want then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name .. ": got " .. tostring(got) .. " want " .. tostring(want)) end +end +local function pair(c) return R.is_cons(c) and R.is_cons(c[2]) and c[1] == 1 and c[2][1] == 2 end +shen.eval("(define add2 X Y -> (+ X Y))") +shen.eval("(define retlam -> (/. X Y (+ X Y)))") +shen.eval("(define appl2 F X Y -> (F X Y))") +-- from shen.eval +local add = shen.eval("(/. X Y (+ X Y))") +local add3 = shen.eval("(/. X Y Z (+ X (+ Y Z)))") +local mk = shen.eval("(/. X (/. Y [X Y]))") +check("lambda of 2, curried", function() return add(1)(2) end, 3) +check("lambda of 2, whole", function() return add(1, 2) end, 3) +check("lambda of 3, curried", function() return add3(1)(2)(3) end, 6) +check("lambda of 3, 2 then 1", function() return add3(1, 2)(3) end, 6) +check("lambda of 3, 1 then 2", function() return add3(1)(2, 3) end, 6) +check("nested lambdas, one argument gives a function", function() return type(mk(1)) end, "function") +check("nested lambdas, curried", function() return pair(mk(1)(2)) end, true) +check("lambda of 1", function() return shen.eval("(/. X (+ X 1))")(1) end, 2) +-- through other routes +check("(fn f), curried", function() return shen.eval("(fn add2)")(1)(2) end, 3) +check("shen.call result, curried", function() return shen.call("retlam")(1)(2) end, 3) +shen.eval("(set stored (/. X Y (+ X Y)))") +check("shen.value, curried", function() return shen.value("stored")(1)(2) end, 3) +check("totable element, curried", function() return shen.totable(shen.eval("[(/. X Y (+ X Y))]"))[1](1)(2) end, 3) +check("nested in a list, curried", function() return shen.call("hd", shen.eval("[(/. X Y (+ X Y))]"))(1)(2) end, 3) +shen.eval("(define givecb F -> (F (/. A B (+ A B))))") +check("a lambda Shen passes to a host callback", function() + return shen.call("givecb", function(g) return g(1)(2) end) +end, 3) +check("lua.call style: given to Lua and called with one", function() + return shen.call("givecb", function(g) local h = g(1); return h(2) end) +end, 3) +-- identity and equality +local st = shen.eval("(value stored)") +check("the same value each time", function() return st == shen.eval("(value stored)") end, true) +local l = shen.list({ st, 5 }) +shen.eval("(define memberq X [] -> false X [Y | Z] -> (if (= X Y) true (memberq X Z)))") +check("a host-built list holds the same function", function() return shen.call("memberq", shen.eval("(value stored)"), l) end, true) +check("= across the boundary", function() return shen.call("=", shen.call("hd", l), shen.eval("(value stored)")) end, true) +check("passed back and called", function() return shen.call("appl2", add, 1, 2) end, 3) +-- thaw of a function that takes arguments gives a partial +check("thaw of a two-parameter lambda", function() return pair(shen.eval("(let F (thaw (/. X Y [X Y])) (F 1 2))")) end, true) +check("thaw of a one-parameter lambda", function() return shen.eval("(let F (thaw (/. X (+ X 1))) (F 2))") end, 3) +check("thaw of (fn f)", function() return shen.eval("(let F (thaw (fn add2)) (F 1 2))") end, 3) +check("thaw in a defun", function() + shen.eval("(define th-of F -> (thaw F))") + return pair(shen.eval("((th-of (/. X Y [X Y])) 1 2)")) +end, true) +check("thaw of a freeze", function() return shen.eval("(thaw (freeze (+ 1 2)))") end, 3) +-- a host's own function is left alone +check("a host function with one argument, called by Shen", function() + shen.eval("(define appl1 F X -> (F X))") + return shen.call("appl1", function(a) return a + 1 end, 1) +end, 2) +print(("embed_values_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/error_robustness_spec.lua b/test/error_robustness_spec.lua index bab645e..97ac8d1 100644 --- a/test/error_robustness_spec.lua +++ b/test/error_robustness_spec.lua @@ -80,11 +80,134 @@ do end -- --------------------------------------------------------------------------- --- DIVERGENCE: shen-lua's `if` does not require a boolean condition. A non-false --- value is truthy, so (if 42 1 2) returns 1 (shen-go raises "if requires a --- boolean"). Lock in the port's behavior. +-- A non-boolean test raises, for the test of if and cond and both operands of +-- and/or, wherever the form sits. Short-circuited operands are not evaluated. -- --------------------------------------------------------------------------- -check(evs("(if 42 1 2)") == "1", "if with non-boolean condition is truthy (port divergence)") +local function raises_boolean(src) + local ok, err = pcall(evs, src) + local msg = not ok and tostring(type(err) == "table" and (err.msg or err[1]) or err) or "" + check(not ok, src .. " raises") + if not ok then check(msg:find("is not a boolean", 1, true) ~= nil, src .. " says why, got: " .. msg) end +end +for _, src in ipairs({ + "(if 42 1 2)", '(if "a" 1 2)', "(if [] 1 2)", "(if a 1 2)", + "(and 1 true)", "(and true 1)", "(or 1 false)", "(or false 1)", + "(cond (1 a) (true b))", "(cond (false a) (2 b))", + "(let X 1 (if X a b))", "(let F (/. X (if X a b)) (F 1))", "(do 1 (if 3 a b))", + "(let X (and true 1) X)", "(if (and true 1) a b)", "(and true (if true 1 false))", + "(and true (let Y 1 Y))", "(and true (do 0 1))", "(or false (cond (true 1)))", +}) do raises_boolean(src) end +-- not is a test too, compiled or not +for _, src in ipairs({ "(not 5)", '(not "a")', "(not [])", "(let X 5 (not X))", "(eval-kl [not 5])" }) do + raises_boolean(src) +end +-- a predicate with too few arguments is a closure, not a boolean +for _, src in ipairs({ + "(if (cons?) a b)", "(if (number?) a b)", "(cond ((cons?) a) (true b))", "(and (number?) true)", + "(eval-kl [if [= 1] a b])", "(eval-kl [if [cons?] a b])", "(eval-kl [if [not] a b])", + "(eval-kl [if [element? 1] a b])", +}) do raises_boolean(src) end +evs("(define rob-id X -> X)") +raises_boolean("(not (rob-id 5))") +evs("(define rob-not X -> (not X))") +raises_boolean("(rob-not 5)") +check(evs("(rob-not true)") == "false", "not of a variable holding true") +check(evs("(rob-not false)") == "true", "not of a variable holding false") +check(evs("(not (rob-id false))") == "true", "not of a call giving false") +check(evs("(not (cons? 1))") == "true", "not of a predicate") +raises_boolean("(if (rob-id 1) a b)") +raises_boolean("(and (rob-id true) (rob-id 1))") +check(evs("(if (rob-id true) 1 2)") == "1", "a call that gives true passes") +check(evs("(if (rob-id false) 1 2)") == "2", "a call that gives false passes") +check(evs("(and false 1)") == "false", "and does not evaluate past false") +check(evs("(or true 1)") == "true", "or does not evaluate past true") +check(evs("(and true (rob-id false))") == "false", "and gives its last operand") +check(evs("(or false (rob-id true))") == "true", "or gives its last operand") +check(evs("(if (and true (and (rob-id true) (or false (rob-id true)))) 1 2)") == "1", "nested tests") +-- a loop through and/or stays a loop, and still reaches a bad element +evs("(define rob-all [] -> true [X | Xs] -> (and (rob-id X) (rob-all Xs)))") +evs("(define rob-none [] -> false [X | Xs] -> (or (rob-id X) (rob-none Xs)))") +evs("(define rob-iota 0 V A -> A N V A -> (rob-iota (- N 1) V [V | A]))") +check(evs("(rob-all (rob-iota 3000000 true []))") == "true", "and: a tail call to itself keeps its frame") +check(evs("(rob-none (rob-iota 3000000 false []))") == "false", "or: a tail call to itself keeps its frame") +raises_boolean("(rob-all [true 1 true])") +-- so does one through another function, however deep +evs("(define rob-ev? 0 -> true N -> (and (number? N) (rob-od? (- N 1))))") +evs("(define rob-od? 0 -> false N -> (and (number? N) (rob-ev? (- N 1))))") +evs("(define rob-ev2? 0 -> false N -> (or (not (number? N)) (rob-od2? (- N 1))))") +evs("(define rob-od2? 0 -> false N -> (or (not (number? N)) (rob-ev2? (- N 1))))") +local function deep(src, want, name) + local ok, got = pcall(evs, src) + check(ok and got == want, name .. ", got " .. tostring(ok and got or "an error")) +end +deep("(rob-ev? 5000000)", "true", "and: a tail call to another function keeps its frame") +deep("(rob-ev2? 5000000)", "false", "or: a tail call to another function keeps its frame") +for name, src in pairs({ + ["and"] = "(define rob-%s 0 -> true N -> (and (number? N) (rob-%s (- N 1))))", + ["and-if-then"] = "(define rob-%s 0 -> true N -> (and (number? N) (if (> N 0) (rob-%s (- N 1)) false)))", + ["and-if-else"] = "(define rob-%s 0 -> true N -> (and (number? N) (if (= N 0) false (rob-%s (- N 1)))))", + ["and-cond"] = "(define rob-%s 0 -> true N -> (and (number? N) (cond ((> N 0) (rob-%s (- N 1))) (true false))))", + ["and-let"] = "(define rob-%s 0 -> true N -> (and (number? N) (let M (- N 1) (rob-%s M))))", + ["and-do"] = "(define rob-%s 0 -> true N -> (and (number? N) (do 1 (rob-%s (- N 1)))))", + ["and-and"] = "(define rob-%s 0 -> true N -> (and (number? N) (and (integer? N) (rob-%s (- N 1)))))", + ["and-or"] = "(define rob-%s 0 -> true N -> (and (number? N) (or (string? N) (rob-%s (- N 1)))))", + ["if-and"] = "(define rob-%s 0 -> true N -> (if (> N 0) (and (number? N) (rob-%s (- N 1))) false))", + ["or"] = "(define rob-%s 0 -> true N -> (or (string? N) (rob-%s (- N 1))))", + ["or-if"] = "(define rob-%s 0 -> true N -> (or (string? N) (if (> N 0) (rob-%s (- N 1)) false)))", + ["or-cond"] = "(define rob-%s 0 -> true N -> (or (string? N) (cond ((> N 0) (rob-%s (- N 1))) (true false))))", + ["freeze-and"] = "(define rob-%s 0 -> true N -> (thaw (freeze (and (number? N) (rob-%s (- N 1))))))", + ["lambda-and"] = "(define rob-%s 0 -> true N -> ((/. M (and (number? M) (rob-%s M))) (- N 1)))", +}) do + local a, b = "a-" .. name, "b-" .. name + evs(src:format(a, b)); evs(src:format(b, a)) + local ok, got = pcall(evs, "(rob-" .. a .. " 5000000)") + check(ok and got == "true", "mutual tail recursion, depth 5M, through " .. name .. ", got " .. tostring(ok and got or "an error")) +end +-- with no enclosing defun: a top-level lambda's tail is a tail too +evs("(set rob-top (/. N (or (= N 0) ((value rob-top) (- N 1)))))") +deep("((value rob-top) 5000000)", "true", "top-level lambda, or") +evs("(set rob-top2 (/. N (and (number? N) (if (= N 0) true ((value rob-top2) (- N 1))))))") +deep("((value rob-top2) 5000000)", "true", "top-level lambda, and with if") +evs("(set rob-top3 (/. N (and (number? N) (cond ((= N 0) true) (true ((value rob-top3) (- N 1)))))))") +deep("((value rob-top3) 5000000)", "true", "top-level lambda, and with cond") +evs("(set rob-top4 (/. N (and (number? N) (let M (- N 1) (if (< M 0) true ((value rob-top4) M))))))") +deep("((value rob-top4) 5000000)", "true", "top-level lambda, and with let") +deep("(let F (/. G N (or (= N 0) (G G (- N 1)))) (F F 5000000))", "true", "top-level let-bound lambda") +evs("(set rob-top5 (/. N (thaw (freeze (and (number? N) (if (= N 0) true ((value rob-top5) (- N 1))))))))") +deep("((value rob-top5) 5000000)", "true", "top-level lambda with a freeze body") +-- checked as in a defun: value position, a primitive, and a form that is no function +raises_boolean("(let F (/. X (let Y (and true (rob-id X)) Y)) (F 1))") +raises_boolean("(let F (/. X (and true (+ X 1))) (F 1))") +raises_boolean("(let F (/. X (if (and true (rob-id X)) a b)) (F 1))") +raises_boolean("(and true (rob-id 1))") +check(evs("(let F (/. X (and true (rob-id X))) (F 1))") == "1", "unchecked: a top-level lambda's tail call") +evs("(define rob-va _ 0 -> true F N -> (and (number? N) (F F (- N 1))))") +evs("(define rob-vgo N -> (rob-va (/. F M (rob-va F M)) N))") +deep("(rob-vgo 5000000)", "true", "depth 5M through a variable under and") +evs("(define rob-apply F X -> (and true (F X)))") +check(evs("(rob-apply (/. X (rob-ev? X)) 2)") == "true", "and: a tail call through a variable") +-- the tail call is unchecked; a test of its result, here or in the caller, is not +evs("(define rob-last X -> (and true (rob-id X)))") +evs("(define rob-test X -> (if (rob-last X) yes no))") +evs("(define rob-inner X -> (if (and true (rob-id X)) yes no))") +evs("(define rob-let X -> (let Y (and true (rob-id X)) Y))") +evs("(define rob-prim X -> (and true (+ X 1)))") +-- the hole, on purpose: Shen/JIT and Shen/Scheme raise on these, shen-go gives 5 +evs("(define rob-lastif X -> (and true (if true (rob-id X) false)))") +evs("(define rob-lastor X -> (or false (rob-id X)))") +evs("(define rob-lastfz X -> (thaw (freeze (and true (rob-id X)))))") +evs("(define rob-lastlam X -> ((/. Y (and true (rob-id Y))) X))") +check(evs("(rob-lastif 1)") == "1", "unchecked: a tail call under if under and") +check(evs("(rob-lastor 1)") == "1", "unchecked: a tail call under or") +check(evs("(rob-lastfz 1)") == "1", "unchecked: a freeze body's tail call") +check(evs("(rob-lastlam 1)") == "1", "unchecked: a lambda body's tail call") +check(evs("(rob-last 1)") == "1", "a defun's tail call through and returns its value") +raises_boolean("(rob-test 1)") +raises_boolean("(rob-inner 1)") +raises_boolean("(rob-let 1)") +raises_boolean("(rob-prim 1)") +check(evs("(rob-test true)") == "yes", "a test of a boolean tail result passes") +check(evs("(rob-inner false)") == "no", "and in a test passes false") -- --------------------------------------------------------------------------- -- Adversarial sequence: drive several errors in a row through the SAME booted diff --git a/test/fixtures/codegen-fuzz-112.shen b/test/fixtures/codegen-fuzz-112.shen new file mode 100644 index 0000000..a985ace --- /dev/null +++ b/test/fixtures/codegen-fuzz-112.shen @@ -0,0 +1,661 @@ +(set trace []) +(define tr K V -> (do (set trace [K | (value trace)]) V)) +(define pl X -> (if (number? X) X 0)) +(define two A B -> [two A B]) +(define three A B C -> [three A B C]) +(define one A -> [one A]) +(define curry2 A -> (/. B [curry2 A B])) +(define nz V -> (if (= (shen.fnp V) true) fun (if (cons? V) (cons (nz (hd V)) (nz (tl V))) V))) +(define shen.fnp V -> (let S (str-any V) (and (>= (length (explode S)) 5) (= (hdn S 5) "funex")))) +(define hdn S 0 -> "" S N -> (cn (hdstr S) (hdn (tlstr S) (- N 1)))) +(define str-any V -> (trap-error (make-string "~S" V) (/. E "err"))) +(define go Id F -> (do (set trace []) (let R (trap-error (nz (thaw F)) (/. E (cn "ERR " (error-to-string E)))) (output "~A ~S ~S~%" Id R (value trace))))) + +(go t0_0 (freeze (let X [[[1]] | c] (let Y [a | b] (or (cons? (tl Y)) (cons? (tl Y))))))) +(go t0_1 (freeze (let X [[[1]] | c] (let Y [] (trap-error (if (cons? (tl (tl (tl X)))) (let X (or (cons? (hd (tl X))) true) (let V1 (hd (tl X)) (hd Y))) (if (number? (hd (hd (tl X)))) Y Y)) (/. E (cn "E:" (error-to-string E)))))))) +(go t0_2 (freeze (let X 0 (let Y true [(let V2 7 (V2 (let X (or false true) (hd (hd (tl X)))) (do (tr 6 0) (two "s" (hd Y))))) | ((/. V6 V7 V8 V9 X (or (or (cons? (hd (tl (hd X)))) true) (cons? (tl (hd Y))))) (let V3 (/. V4 (let V5 Y (tl (hd V4)))) (V3 (tr 7 (tl (hd (tl Y)))))) (cond ((number? (hd (tl Y))) (if true Y 1)) ((cons? (hd X)) Y) ((number? (tl (tl (tl X)))) (tl Y)) (true X)))])))) +(go t0_3 (freeze (let X a (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (tl X))))) +(go t0_4 (freeze (let X 2.5 (let Y 2.5 (hd (tl X)))))) +(define f1 N X Y -> (if (not (if (cons? (tl (tl X))) (not (cons? N)) (or (cons? N) (if true false false)))) (if (number? (hd (tl (hd N)))) (do (tr 2 0) (do (tr 9 0) (cond (false (tl (hd (hd X)))) (false (tl N))))) (let V1 undefinedf (V1 (tl (hd (hd X))) (trap-error (trap-error (hd (tl (tl N))) (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E)))) (do (tr 5 0) (or true (cons? X)))))) (if (> N 0) (f1 (- N 1) (if (cons? (tl (tl (hd Y)))) (tr 1 (tl (tl N))) (let V2 three (V2 (thaw (freeze [[[1 2] [3 4]] [[5] 6] 7 8])) (or true true) ((/. V3 V4 V5 N)) (if (> N 0) (f1 (- N 1) N X) (tl (tl Y)))))) (or (if (and (cons? (hd (hd (hd X)))) true) (= Y [[[1 2] [3 4]] [[5] 6] 7 8]) (= (tl (hd (hd X))) (hd (tl (tl X))))) (and (not (cons? (hd (tl N)))) (number? Y)))) (if (> N 0) (f1 (- N 1) (if (= N 1) (cond (true (tl (hd X))) (true X)) (trap-error (hd (tl (tl X))) (/. E (cn "E:" (error-to-string E))))) (/. Q (if (> (pl N) 0) (and false true) (if (cons? (tl (tl Y))) N (tl (hd (hd N))))))) (let V6 (/. V7 N (let V8 (fn two) (V8 (hd V7) [[[1 2] [3 4]] [[5] 6] 7 8] X [[[1 2] [3 4]] [[5] 6] 7 8]))) (V6 ((/. V9 N X V10 V11 (hd Y))) (trap-error N (/. E (cn "E:" (error-to-string E)))) (cond ((cons? (tl (tl Y))) (hd X)) (false N) ((cons? N) a) (true X)))))))) +(go t1_0 (freeze (f1 3 [[1] 2] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t1_1 (freeze (f1 3 [1 2] [1]))) +(go t1_2 (freeze (f1 1 0 [1]))) +(go t1_3 (freeze (f1 3 [[[1 2] [3 4]] [[5] 6] 7 8] false))) +(go t1_4 (freeze (f1 1 [[1 [2 [3 [4]]]] 5] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t2_0 (freeze (let X [[1] 2] (let Y [1 2] (hd Y))))) +(go t2_1 (freeze (let X [[[1]] | c] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (and (if (if true (cons? (tl Y)) true) (let V1 X (cons? X)) (cons? (tl (hd (hd X))))) (number? (do (tr 5 0) X))))))) +(go t2_2 (freeze (let X [[[1]] | c] (let Y [[1 2] [3]] Y)))) +(go t2_3 (freeze (let X 0 (let Y [a | b] X)))) +(go t2_4 (freeze (let X [a | b] (let Y [1 2] ((/. V2 V3 V4 (let V5 (/. V6 ((/. V7 V8 V9 V10 V11 (hd (tl V10))) V4 (hd (hd V6)) 1 V2 V6 V2)) (V5 (tr 8 (tl V2))))) (tr 3 (two Y X))))))) +(define f3 N [[A | B] C] Y -> (cond ((or (cons? (hd (hd B))) (if (= (tl N) A) (cons? (hd (tl (tl A)))) (cons? (hd A)))) (trap-error (let V1 (/. V2 V3 V4 (trap-error (if (cons? (hd (hd V3))) (hd V4) A) (/. E (cn "E:" (error-to-string E))))) (V1 (if (> N 0) (f3 (- N 1) (or false false) (cond ((cons? (tl (tl B))) C) ((cons? (hd (hd (hd Y)))) B) (true N))) [A | Y]) (cond ((> (pl (tl Y)) 0) ((/. C A (tl (hd (tl N)))) C [] (tl (tl (hd B)))))))) (/. E (cn "E:" (error-to-string E))))) (true (if (> N 0) (cons (if (> N 0) (f3 (- N 1) ((/. V6 N ((/. V7 V8 V9 V10 V11 1) (hd (hd N)) "s" (hd (tl (tl N))))) (let V5 three (V5 (tl (hd (tl N))) (tl Y) Y (hd (hd A))))) (/. Q (if (> N 0) (cons (if (> N 0) (cons A (f3 (- N 1) (tl N) 2.5)) C) (f3 (- N 1) (if false A C) (do (tr 8 0) C))) (do (tr 0 0) (hd (hd (tl A))))))) (if (cons? (tl A)) (let V12 two (V12 (tl N) (hd Y) 2.5 (hd A))) (thaw (freeze (tl Y))))) (f3 (- N 1) (thaw (freeze (two (thaw (freeze 1)) (if (> N 0) (f3 (- N 1) N (/. Q (tl C))) N)))) (if (let N (tl A) (cons? (tl (hd A)))) ((/. V13 V14 C V15 (if true 1 A)) (if (> N 0) (cons B (f3 (- N 1) (hd (tl A)) [[[1 2] [3 4]] [[5] 6] 7 8])) A)) (let V16 (/. V17 V18 (cond (true N) (true (hd (hd C))))) (V16 (and false true) C))))) [(if (let V19 A false) (two 1 (hd (hd C))) (if false (tl Y) C)) | ((/. V20 V21 V22 V23 B (if (cons? C) N "s")) (two [] (tl (hd N))) (if (> N 0) (f3 (- N 1) (tl (hd C)) (/. Q C)) A))]))) + N X [A | B] -> (if (and (cons? (tl (tl X))) (and (let N (trap-error (tl (tl B)) (/. E (cn "E:" (error-to-string E)))) (if false true (cons? (hd (tl N))))) (or (and (cons? B) (cons? (tl (hd (hd N))))) (cons? (hd (hd A)))))) (if (and (cons? (tl A)) (or (cons? (hd B)) (or true false))) (trap-error [(and (cons? B) (cons? (hd (tl N)))) | (trap-error N (/. E (cn "E:" (error-to-string E))))] (/. E (cn "E:" (error-to-string E)))) ((/. V2 V3 (hd X)) (let V1 (if false (hd (tl (tl B))) 2.5) (if (> N 0) (f3 (- N 1) B (hd (tl N))) N)))) (tr 6 (do (tr 1 0) (tl (hd (tl X)))))) + N X Y -> (if (> N 0) (f3 (- N 1) (do (tr 5 0) (hd (hd Y))) (/. Q (hd N))) (hd N))) +(go t3_0 (freeze (f3 1 false false))) +(go t3_1 (freeze (f3 0 [[[1]] | c] false))) +(go t3_2 (freeze (f3 2 2.5 true))) +(go t3_3 (freeze (f3 3 true a))) +(go t3_4 (freeze (f3 2 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f4 N [H | T] Y -> (if (cons? (tl (tl (hd Y)))) ((/. V4 V5 V6 V7 (tl V6)) (two (or (cons? H) (cons? (hd T))) (if true a (hd (tl T)))) (if (> N 0) (cons (tr 0 1) (f4 (- N 1) (and false true) (let V1 (/. V2 V3 Y) (V1 H)))) (if true [[[1 2] [3 4]] [[5] 6] 7 8] 1))) [(do (tr 9 0) [(hd (hd Y)) | [[1 [2 [3 [4]]]] 5]]) | (tl (hd N))]) + N X Y -> (or (cons? (hd (hd (tl N)))) (if (or (cons? (hd N)) (number? (hd (hd (hd X))))) (cons? N) (cons? X)))) +(go t4_0 (freeze (f4 3 [a | b] []))) +(go t4_1 (freeze (f4 0 [[[1 2] [3 4]] [[5] 6] 7 8] 0))) +(go t4_2 (freeze (f4 2 "s" [1]))) +(go t4_3 (freeze (f4 2 0 [[1 [2 [3 [4]]]] 5]))) +(go t4_4 (freeze (f4 2 2.5 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f5 N [H | T] Y -> (or (number? [[[1 2] [3 4]] [[5] 6] 7 8]) (and true true)) + N X Y -> (and (> (pl Y) 1) (cons? (hd X)))) +(go t5_0 (freeze (f5 1 [[[1 2] [3 4]] [[5] 6] 7 8] [[1 2] [3]]))) +(go t5_1 (freeze (f5 0 [[[1]] | c] 0))) +(go t5_2 (freeze (f5 2 [[[1]] | c] true))) +(go t5_3 (freeze (f5 1 [] [1 2]))) +(go t5_4 (freeze (f5 1 a a))) +(define f6 N [[A | B] C] Y -> (let V1 7 (V1 (tl (hd A)) (two (let V2 three (V2 (if (let V3 (tl (hd (hd N))) false) (let V4 0 N) (tl (tl (tl N)))) (if (or true true) (or true true) (if (> N 0) (f6 (- N 1) (hd (tl B)) A) (hd B))) (if (> N 0) (f6 (- N 1) ((/. V5 V6 V6) A B 1) (/. Q (cond (true [[[1 2] [3 4]] [[5] 6] 7 8]) (true C)))) (if (> N 0) (f6 (- N 1) (hd (hd (hd Y))) (/. Q Y)) B)))) (do (tr 5 0) (if (> N 0) (f6 (- N 1) (tr 4 A) (/. Q [(hd (hd (tl B))) | (tl (tl (tl Y)))])) (if (> N 0) (cons (tl (tl Y)) (f6 (- N 1) Y (hd (tl A)))) B)))))) + N X [A | B] -> (let V1 (/. V2 (thaw (freeze (let V3 (/. V4 V5 V6 (or (and false false) (not (cons? (tl N))))) (V3 (and (cons? (hd (hd A))) (and (cons? (hd (hd A))) (cons? (tl (hd N))))) (tr 1 (and (cons? B) (cons? (hd (hd (tl X)))))) (and (let V7 B true) (cons? (hd V2)))))))) (V1 (if (number? (if (= N []) A (if (> N 0) (f6 (- N 1) N (/. Q (tl (hd A)))) A))) (if (> N 0) (f6 (- N 1) (and (cons? (tl A)) (cons? (hd (hd A)))) (do (tr 0 0) (hd (tl A)))) (do (tr 4 0) (trap-error A (/. E (cn "E:" (error-to-string E)))))) (tr 6 (do (tr 0 0) (let V8 B X)))) ((/. V11 V12 X (if (or (or true true) (number? (tl (hd X)))) (let V13 (/. B V14 N (or true (cons? (tl V11)))) (V13 (hd (hd (tl A))) (cond (false 2.5) ((cons? (hd (tl N))) V11) (true (hd (hd (hd B))))))) (if (cons? (hd (tl V12))) [V11 | B] (if (> N 0) (cons (tl (tl (tl B))) (f6 (- N 1) A V12)) (hd (tl A)))))) (two [(tr 8 (hd (tl (tl N)))) | (let V9 one (V9 (tl N) B []))] (if (let V10 (tl (tl (hd A))) false) (tr 6 (hd (hd A))) (tr 0 0)))))) + N X Y -> (if (> N 0) (f6 (- N 1) (let V1 (three 1 2) (V1 (thaw (freeze (if (let V2 a (cons? V2)) (if (cons? (hd (hd (hd N)))) 0 0) (let X N (hd (tl (tl N))))))) (let V3 (let V4 (fn two) (V4 (if (> N 0) (f6 (- N 1) (tl X) (/. Q N)) N) (trap-error (tl (tl N)) (/. E (cn "E:" (error-to-string E)))) (if (> N 0) (f6 (- N 1) N Y) (tl (hd Y))) (do (tr 2 0) Y))) (if (not (cons? (tl X))) (two (hd (tl V3)) N) (tr 5 Y))))) (/. Q (if (> N 0) (cons (do (tr 2 0) (do (tr 0 0) (two N (hd (tl X))))) (f6 (- N 1) (hd (tl (tl N))) (or (if (> (pl (hd N)) 0) (number? 0) (not (cons? (hd N)))) (or (or true false) (if (cons? Y) true false))))) (if (cons? (tl (hd (tl N)))) Y [(if false (tl Y) "s") | (hd (tl (tl X)))])))) (let V5 (tl (tl N)) (do (tr 4 0) (if (> N 0) (f6 (- N 1) (do (tr 4 0) N) ((/. V6 V7 (hd (hd (hd V6)))))) (if (> N 0) (f6 (- N 1) 2.5 X) X)))))) +(go t6_0 (freeze (f6 0 2.5 a))) +(go t6_1 (freeze (f6 3 [a | b] [[1 2] [3]]))) +(go t6_2 (freeze (f6 2 a 2.5))) +(go t6_3 (freeze (f6 1 [[1 2] [3]] [[[1]] | c]))) +(go t6_4 (freeze (f6 2 1 [[1] 2]))) +(define f7 N [[A | B] C] Y -> (or (if false true false) (let V1 A true)) + N X [A | B] -> N + N X Y -> (hd Y)) +(go t7_0 (freeze (f7 0 [[[1 2] [3 4]] [[5] 6] 7 8] [1 2]))) +(go t7_1 (freeze (f7 3 a []))) +(go t7_2 (freeze (f7 0 [[1 2] [3]] "s"))) +(go t7_3 (freeze (f7 3 [1 2] [1]))) +(go t7_4 (freeze (f7 2 "s" 1))) +(go t8_0 (freeze (let X 1 (let Y a (let V1 curry2 (V1 (or (= (if true X X) (let V2 (/. V3 V4 (tl X)) (V2 (tl (tl (hd Y)))))) (= (or (cons? (hd (hd X))) (cons? (hd (tl X)))) (do (tr 1 0) X))) (if (let V5 (and (cons? (tl (tl X))) false) (= (tl (hd V5)) Y)) (if (cons? (hd X)) (cond ((cons? (hd (tl Y))) (tl Y)) ((cons? (hd (tl (hd Y)))) "s")) (tr 0 (tl X))) [(tl (hd Y)) | (tl X)]))))))) +(go t8_1 (freeze (let X a (let Y [a | b] (let V6 (/. V7 V8 V9 (tl V7)) (V6 [0 | Y] (let V10 (/. V11 V12 (if (cons? (tl X)) (do (tr 4 0) a) (or (cons? (hd (tl V12))) false))) (V10 (cond ((> (pl (tl (hd X))) 0) ((/. V13 X X V14 V15 [[[1 2] [3 4]] [[5] 6] 7 8]) Y)) ((cons? (hd (tl (tl X)))) (let V16 Y (tl (tl Y)))) ((= (hd (tl (hd X))) X) (let V17 three (V17 (hd (hd (hd X))) (tl (tl Y))))) (true Y)) 1)))))))) +(go t8_2 (freeze (let X a (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (two [(thaw (freeze (let V18 (/. V19 V19) (V18 (tl (tl X)))))) | (let Y (thaw (freeze X)) [Y | Y])] Y))))) +(go t8_3 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [[[1]] | c] (tr 0 (let V20 (/. V21 (thaw (freeze "s"))) (V20 1))))))) +(go t8_4 (freeze (let X [1 2] (let Y [[1 [2 [3 [4]]]] 5] (tr 2 (do (tr 3 0) (let V22 curry2 (V22 (hd (tl Y)) (trap-error Y (/. E (cn "E:" (error-to-string E)))))))))))) +(define f9 N [H | T] Y -> [(if (cons? (hd (hd H))) 0 T) | ((/. V1 V2 V3 a) (hd (hd N)) H)] + N X Y -> (and (let V1 N false) (= (hd (hd (tl N))) N))) +(go t9_0 (freeze (f9 0 [[1] 2] [a | b]))) +(go t9_1 (freeze (f9 1 [1] true))) +(go t9_2 (freeze (f9 0 [1] 1))) +(go t9_3 (freeze (f9 1 [a | b] [[1 [2 [3 [4]]]] 5]))) +(go t9_4 (freeze (f9 3 [[1 [2 [3 [4]]]] 5] [a | b]))) +(go t10_0 (freeze (let X a (let Y [[1 [2 [3 [4]]]] 5] (if (cons? (tl (hd (hd Y)))) (tl (hd X)) (two (if false a Y) (tl Y))))))) +(go t10_1 (freeze (let X 0 (let Y [[1] 2] (tl Y))))) +(go t10_2 (freeze (let X true (let Y [[1] 2] (trap-error (hd (tl (hd X))) (/. E (cn "E:" (error-to-string E)))))))) +(go t10_3 (freeze (let X [[1 2] [3]] (let Y [] (hd (tl (tl Y))))))) +(go t10_4 (freeze (let X 2.5 (let Y 0 (hd (hd (hd Y))))))) +(define f11 N [H | T] Y -> (trap-error (two [[[1 2] [3 4]] [[5] 6] 7 8] (hd (hd (tl T)))) (/. E (cn "E:" (error-to-string E)))) + N X Y -> (if (> N 0) (cons (do (tr 5 0) X) (f11 (- N 1) (and true true) (two (hd Y) [[[1 2] [3 4]] [[5] 6] 7 8]))) (if (> N 0) (f11 (- N 1) Y N) (tl (tl (tl N)))))) +(go t11_0 (freeze (f11 2 [[1] 2] 2.5))) +(go t11_1 (freeze (f11 2 false 1))) +(go t11_2 (freeze (f11 3 [[1] 2] "s"))) +(go t11_3 (freeze (f11 1 [[[1 2] [3 4]] [[5] 6] 7 8] 2.5))) +(go t11_4 (freeze (f11 1 0 true))) +(define f12 N [[A | B] C] Y -> (thaw (freeze N)) + N X [A | B] -> (thaw (freeze (if (> N 0) (cons (tl A) (f12 (- N 1) (let V1 (/. V2 V3 X) (V1 A N (hd (tl N)))) (let V4 7 (V4 (hd (hd X)) A)))) (if (> N 0) (cons N (f12 (- N 1) a X)) N)))) + N X Y -> (do (tr 9 0) (cond ((if (cons? X) false true) (let V1 N (hd V1))) ((= (hd (hd Y)) Y) (let V2 7 (V2 a Y))) (true (tl N))))) +(go t12_0 (freeze (f12 2 [a | b] [1 2]))) +(go t12_1 (freeze (f12 3 a "s"))) +(go t12_2 (freeze (f12 1 "s" 2.5))) +(go t12_3 (freeze (f12 2 [a | b] true))) +(go t12_4 (freeze (f12 3 [[1 2] [3]] [[1 2] [3]]))) +(define f13 N [H | T] Y -> T + N X Y -> (let X (do (tr 6 0) Y) (trap-error (if (> N 0) (cons 1 (f13 (- N 1) (tl (hd N)) (hd (tl (tl X))))) (tl (tl N))) (/. E (cn "E:" (error-to-string E)))))) +(go t13_0 (freeze (f13 0 [[[1 2] [3 4]] [[5] 6] 7 8] "s"))) +(go t13_1 (freeze (f13 0 [[1] 2] [1 2]))) +(go t13_2 (freeze (f13 0 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t13_3 (freeze (f13 3 "s" [[1 [2 [3 [4]]]] 5]))) +(go t13_4 (freeze (f13 0 [1 2] [1]))) +(define f14 N X Y -> (if (> N 0) (f14 (- N 1) (if (= (two (tl (tl X)) Y) (thaw (freeze a))) (if (and (cons? N) true) (if (> N 0) (f14 (- N 1) Y (hd (tl N))) a) (and (cons? (hd N)) false)) (two (thaw (freeze X)) (two (tl (tl Y)) (tl X)))) (if (cons? (hd (tl (tl N)))) ((/. V1 X V2 (trap-error (hd (tl V1)) (/. E (cn "E:" (error-to-string E))))) (if (> N 0) (f14 (- N 1) [[[1 2] [3 4]] [[5] 6] 7 8] [[1 [2 [3 [4]]]] 5]) (tl N)) (or false true)) (and (or false true) (and false false)))) (two X (let V3 three (V3 (do (tr 0 0) (tl (tl N))) (do (tr 4 0) N)))))) +(go t14_0 (freeze (f14 3 0 1))) +(go t14_1 (freeze (f14 1 0 []))) +(go t14_2 (freeze (f14 2 false 2.5))) +(go t14_3 (freeze (f14 3 [] [[1] 2]))) +(go t14_4 (freeze (f14 3 a 1))) +(define f15 N X Y -> (trap-error (trap-error (tr 9 (and true false)) (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E))))) +(go t15_0 (freeze (f15 3 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t15_1 (freeze (f15 3 [[[1 2] [3 4]] [[5] 6] 7 8] a))) +(go t15_2 (freeze (f15 0 [[[1 2] [3 4]] [[5] 6] 7 8] [[1 2] [3]]))) +(go t15_3 (freeze (f15 2 [[1 [2 [3 [4]]]] 5] 0))) +(go t15_4 (freeze (f15 3 "s" [a | b]))) +(go t16_0 (freeze (let X 2.5 (let Y 1 (if (> (pl (or (cons? (hd (tl (tl Y)))) (cons? (hd (tl X))))) 1) X (two (let V1 (three 1 2) (V1 X)) (if true (tl (hd Y)) Y))))))) +(go t16_1 (freeze (let X [[1] 2] (let Y 2.5 X)))) +(go t16_2 (freeze (let X [[1] 2] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (if (cons? (tl X)) (if (cons? Y) (if true Y Y) ((/. V2 V3 V4 (tl (tl (hd V4)))))) (trap-error (tr 5 (hd Y)) (/. E (cn "E:" (error-to-string E))))))))) +(go t16_3 (freeze (let X [1] (let Y true (if (cons? (hd X)) (or (> (pl (tl (hd (tl Y)))) 1) (cons? (tl (hd Y)))) ((/. V5 [(tl (tl (hd X))) | (hd (hd Y))]) (tr 8 1))))))) +(go t16_4 (freeze (let X [1] (let Y [[1] 2] (if (and (if false false true) (and false false)) (do (tr 9 0) 0) (thaw (freeze (tr 9 (hd (hd Y)))))))))) +(define f17 N X Y -> (tl (hd (hd Y)))) +(go t17_0 (freeze (f17 2 [[[1 2] [3 4]] [[5] 6] 7 8] [[1 [2 [3 [4]]]] 5]))) +(go t17_1 (freeze (f17 3 [] [1 2]))) +(go t17_2 (freeze (f17 0 true "s"))) +(go t17_3 (freeze (f17 1 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t17_4 (freeze (f17 3 [1 2] [[1 [2 [3 [4]]]] 5]))) +(define f18 N [H | T] Y -> (hd (tl (tl H))) + N X Y -> (let V1 (/. V2 V3 V4 (and true false)) (V1 (trap-error N (/. E (cn "E:" (error-to-string E)))) (and true false)))) +(go t18_0 (freeze (f18 1 [[1 [2 [3 [4]]]] 5] [[[1]] | c]))) +(go t18_1 (freeze (f18 3 [1 2] 0))) +(go t18_2 (freeze (f18 3 [1] 0))) +(go t18_3 (freeze (f18 2 [[[1 2] [3 4]] [[5] 6] 7 8] [1]))) +(go t18_4 (freeze (f18 0 [] []))) +(define f19 N X Y -> (tr 4 (if (> N 0) (f19 (- N 1) (let V1 (let V2 (/. V3 X (hd (hd (hd N)))) (V2 a)) ((/. V4 (tl (hd (hd Y)))) (hd (tl (hd N))) Y)) (/. Q (tr 7 (cond (false Y) (false X) (true X))))) (if (cons? (tl (hd (tl Y)))) (let V5 Y V5) (hd (tl (hd N))))))) +(go t19_0 (freeze (f19 0 1 true))) +(go t19_1 (freeze (f19 0 [[[1]] | c] [a | b]))) +(go t19_2 (freeze (f19 3 [1] [a | b]))) +(go t19_3 (freeze (f19 1 [[1 2] [3]] 1))) +(go t19_4 (freeze (f19 3 [1] [[[1]] | c]))) +(go t20_0 (freeze (let X [1] (let Y "s" [((/. V1 (let V2 (if (number? V1) (let V3 (two 1) (V3 X)) (let V4 (two 1) (V4 V1 (hd (hd Y))))) (if (number? [[[1 2] [3 4]] [[5] 6] 7 8]) (tr 2 (tl (tl V2))) (or false true))))) | (two Y [(if (and false true) (or true false) (or (cons? Y) (cons? (tl (hd Y))))) | (tr 1 X)])])))) +(go t20_1 (freeze (let X [] (let Y [a | b] ((/. V5 V6 (or (or (cons? (tl V6)) (= (thaw (freeze (tl V6))) (two a (hd X)))) (cons? (hd (tl X))))) [X | (tl Y)]))))) +(go t20_2 (freeze (let X [1 2] (let Y false (if (> (pl (thaw (freeze (let V7 (/. V8 X X (thaw (freeze (tl Y)))) (V7 ((/. Y V9 V10 V11 (hd (hd (tl V10)))) X (hd (hd X)) Y (tl (hd (hd X)))) (trap-error Y (/. E (cn "E:" (error-to-string E)))) (tr 7 (tl (hd Y)))))))) 0) X (let V12 one (V12 (if (cons? (hd (tl (hd X)))) Y (cond ((cons? (tl Y)) X) ((cons? (hd (hd (tl X)))) [[[1 2] [3 4]] [[5] 6] 7 8]) ((cons? (hd (tl (tl Y)))) [a | (hd (tl (tl Y)))]) (true [X | X])))))))))) +(go t20_3 (freeze (let X [a | b] (let Y [1] (thaw (freeze X)))))) +(go t20_4 (freeze (let X [[1] 2] (let Y [[[1]] | c] (if (cons? (hd (hd (hd Y)))) (if (cons? (tl (hd (hd X)))) (if (and (cons? (hd Y)) (cons? (tl (tl (tl Y))))) [(do (tr 8 0) (tl (hd (tl X)))) | (or true true)] (if (cons? (hd (hd (hd X)))) 1 (hd (tl (tl X))))) ((/. V13 (two (hd (hd (tl Y))) (and (cons? X) false))))) (tr 2 (tl X))))))) +(define f21 N [[A | B] C] Y -> (do (tr 7 0) (two (tr 7 (if (> N 0) (cons [C | C] (f21 (- N 1) (two a [[1 [2 [3 [4]]]] 5]) (if true [[1 [2 [3 [4]]]] 5] (tl (hd (hd A)))))) (if (> N 0) (f21 (- N 1) B (hd (hd N))) "s"))) (if (> N 0) (cons [(if (cons? (hd N)) Y N) | (hd (hd (tl A)))] (f21 (- N 1) (cond ((cons? (tl (tl (hd N)))) (hd N)) (true (if (> N 0) (cons [[[1 2] [3 4]] [[5] 6] 7 8] (f21 (- N 1) "s" C)) B))) (let V1 (/. V2 (let V3 (three 1 2) (V3 (hd V2) (tl (tl Y))))) (V1 (tl (hd A)))))) (do (tr 4 0) (cond ((cons? (tl C)) (tl (tl (tl A))))))))) + N X [A | B] -> (if (> N 0) (f21 (- N 1) ((/. N V4 V5 V6 V7 (if (if (not (cons? (tl (hd (tl B))))) (cons? (tl (hd (hd A)))) (cons? (hd V4))) (let V8 (/. V9 V10 (trap-error "s" (/. E (cn "E:" (error-to-string E))))) (V8 ((/. V11 B (tl V5))) ((/. V12 V13 V14 A V15 (tl (tl B))) (tl N) X))) (trap-error (thaw (freeze (hd (hd B)))) (/. E (cn "E:" (error-to-string E)))))) (let B (if (number? (tl (hd (hd N)))) (let V1 (fn two) (V1 X X (tl B))) (let V2 7 (V2 N))) (or (= X B) (number? B))) ((/. V3 (do (tr 0 0) (or true (cons? (hd (hd (tl V3))))))) (cond ((or true (cons? A)) (tl (hd X))) ((> (pl (tl X)) 0) (if false (hd X) (tl (tl (tl N))))) (true (if (cons? (tl (hd (hd N)))) (hd (hd (tl A))) a))))) (/. Q (or (and (number? (if (cons? (hd (tl (hd B)))) a (tl (tl B)))) (> (pl [(hd (tl A)) | (tl X)]) 0)) (cons? A)))) ((/. V16 V17 V18 B V19 (tr 9 (trap-error (tl (tl B)) (/. E (cn "E:" (error-to-string E)))))) (trap-error (cond ((not false) (or false true)) ((and true (cons? (tl (hd (hd A))))) (two (tl (hd (tl X))) X))) (/. E (cn "E:" (error-to-string E)))))) + N X Y -> (trap-error (do (tr 3 0) (if (> N 0) (f21 (- N 1) [(if true Y N) | (and (cons? (tl (hd X))) false)] (/. Q (do (tr 3 0) (hd (tl N))))) (let X (thaw (freeze [[[1 2] [3 4]] [[5] 6] 7 8])) (if (> N 0) (cons (hd (tl N)) (f21 (- N 1) (hd (tl X)) Y)) X)))) (/. E (cn "E:" (error-to-string E))))) +(go t21_0 (freeze (f21 1 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t21_1 (freeze (f21 3 [[1 2] [3]] 2.5))) +(go t21_2 (freeze (f21 2 false 2.5))) +(go t21_3 (freeze (f21 3 [[1] 2] [[1] 2]))) +(go t21_4 (freeze (f21 0 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f22 N X Y -> ((/. V8 N V9 V10 (and (= (do (tr 2 0) (let V9 V9 V8)) (let V11 (cond (true N) ((cons? (hd V9)) X) (false (tl (tl (hd V8)))) (true a)) [a | (hd (hd N))])) (if (cons? X) (> (pl (tr 4 V10)) 1) (> (pl (let V12 curry2 (V12 a))) 1)))) (tr 4 (do (tr 8 0) (let V1 (/. Y V2 (let V3 (/. V4 V5 V4) (V3 [[1 [2 [3 [4]]]] 5] (hd Y) (tl (hd (tl N)))))) (V1 (tl X) (let V6 (/. V7 (tl V7)) (V6 (hd (hd (hd N))))) (if (> N 0) (cons (tl (hd X)) (f22 (- N 1) (tl Y) a)) (tl N)))))) (or (> (pl (tr 1 (if false X (tl N)))) 1) (or (cons? (hd N)) (number? [X | (hd (hd N))]))))) +(go t22_0 (freeze (f22 3 [a | b] [[1] 2]))) +(go t22_1 (freeze (f22 0 true [[[1]] | c]))) +(go t22_2 (freeze (f22 0 0 1))) +(go t22_3 (freeze (f22 1 [[[1]] | c] []))) +(go t22_4 (freeze (f22 3 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f23 N [H | T] Y -> (if (> N 0) (f23 (- N 1) ((/. V1 V2 V3 (tl (tl H)))) (/. Q (hd (hd (hd H))))) (two T (trap-error (tl N) (/. E (cn "E:" (error-to-string E)))))) + N X Y -> ((/. V12 V13 V14 V15 X (tr 2 (if (> N 0) (f23 (- N 1) (tl (tl N)) (hd (tl (hd V12)))) V13))) (if (> N 0) (f23 (- N 1) (hd X) (let V1 (/. V2 X) (V1 Y))) (let V3 (/. V4 N V5 V6 V4) (V3 [[1 [2 [3 [4]]]] 5] (hd N) a (hd Y) (hd Y)))) (let V7 (two 1) (V7 (let V8 (/. X V9 V10 V11 Y) (V8 (hd (tl N)) (hd (hd (tl Y))))))))) +(go t23_0 (freeze (f23 3 [[1 2] [3]] true))) +(go t23_1 (freeze (f23 2 true 0))) +(go t23_2 (freeze (f23 0 [[[1 2] [3 4]] [[5] 6] 7 8] [[1] 2]))) +(go t23_3 (freeze (f23 2 2.5 [1 2]))) +(go t23_4 (freeze (f23 0 2.5 [[1] 2]))) +(define f24 N [[A | B] C] Y -> (two (let V1 (/. V2 (tr 0 (tr 8 B))) (V1 (tl Y))) (trap-error (if (> N 0) (cons (let V3 1 N) (f24 (- N 1) (or true true) (let V4 (/. V5 V6 V7 (hd V7)) (V4 (tl (tl C)))))) (cond (true B) (true B))) (/. E (cn "E:" (error-to-string E))))) + N X [A | B] -> (if (cons? A) (if (cons? (tl (hd (tl X)))) (thaw (freeze (two (hd (tl N)) a))) (trap-error (if (> N 0) (f24 (- N 1) B (/. Q N)) (tl X)) (/. E (cn "E:" (error-to-string E))))) ((/. A X A V2 (if (let V3 (tl (hd X)) (cons? (hd (tl (hd V3))))) (thaw (freeze X)) (if (> N 0) (f24 (- N 1) (tl X) (/. Q (tl N))) A))) (and (number? (hd (hd (hd A)))) (and (cons? (tl (tl B))) true)) (or (cons? (tl (hd N))) (let N (tl N) (cons? B))) (let V1 (cond ((cons? (tl (tl (hd A)))) B) ((cons? (hd N)) 1) (true (tl (hd B)))) (tl (tl A))) B)) + N X Y -> (if (> N 0) (cons (if (number? (do (tr 2 0) (tl (hd (tl N))))) (if (> N 0) (f24 (- N 1) (if true (tl (hd X)) (hd X)) (/. Q (if false N "s"))) (if true (tl (tl Y)) (tl (hd X)))) (thaw (freeze (if (> N 0) (cons a (f24 (- N 1) [] Y)) (hd (tl X)))))) (f24 (- N 1) (trap-error (if (or (cons? N) true) (and (cons? (tl (hd (hd N)))) false) (if (> N 0) (cons Y (f24 (- N 1) X [[[1 2] [3 4]] [[5] 6] 7 8])) (tl (hd (hd N))))) (/. E (cn "E:" (error-to-string E)))) (trap-error (thaw (freeze (let V1 (two 1) (V1 (hd (tl N)) [])))) (/. E (cn "E:" (error-to-string E)))))) (if (cons? (hd (hd (tl Y)))) (do (tr 7 0) (cond (true (hd (hd (tl N)))) (false N) ((cons? (hd N)) (tl (hd (tl Y)))))) (let V2 (let V3 (tl (hd Y)) X) (cond ((cons? Y) (tl (tl (tl Y)))) (true 2.5) (true N)))))) +(go t24_0 (freeze (f24 3 [[[1]] | c] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t24_1 (freeze (f24 3 true [a | b]))) +(go t24_2 (freeze (f24 3 [[[1 2] [3 4]] [[5] 6] 7 8] [1]))) +(go t24_3 (freeze (f24 1 1 a))) +(go t24_4 (freeze (f24 1 [[1 [2 [3 [4]]]] 5] [[1] 2]))) +(go t25_0 (freeze (let X [[1] 2] (let Y false (let V1 (two 1) (V1 (trap-error (if true Y (hd X)) (/. E (cn "E:" (error-to-string E)))) (if (> (pl (tl X)) 0) (cond ((cons? X) (hd (hd (hd X)))) ((cons? (tl X)) a) (true 1) (true X)) ((/. V2 V2))) (or (= (hd (hd (tl Y))) (tl (tl X))) (cons? (hd X))) (if (cons? (tl (hd X))) (two (hd X) (tl Y)) (do (tr 0 0) Y)))))))) +(go t25_1 (freeze (let X [[1 2] [3]] (let Y 0 (if (cons? (tl (tl X))) [[X | Y] | (and false false)] (or (cons? (tl (tl Y))) (and (cons? (hd Y)) true))))))) +(go t25_2 (freeze (let X [1 2] (let Y [] (hd X))))) +(go t25_3 (freeze (let X 0 (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (or (cons? X) (let V3 (two (tl (tl X)) X) (let X X (cons? (hd (hd (hd X))))))))))) +(go t25_4 (freeze (let X [a | b] (let Y [a | b] (or (cons? Y) (or (and false false) (cons? (tl X)))))))) +(go t26_0 (freeze (let X false (let Y 2.5 (if (cons? (tl (hd Y))) (let V1 (/. V2 (let V3 (hd X) (hd (hd (tl V3))))) (V1 (do (tr 6 0) (hd (hd Y))))) (and (cons? (hd X)) (if (cons? (tl X)) true true))))))) +(go t26_1 (freeze (let X 2.5 (let Y 2.5 Y)))) +(go t26_2 (freeze (let X [[1 [2 [3 [4]]]] 5] (let Y true [[1 [2 [3 [4]]]] 5])))) +(go t26_3 (freeze (let X false (let Y a (trap-error (let V4 (/. Y a) (V4 (tr 6 [[[1 2] [3 4]] [[5] 6] 7 8]))) (/. E (cn "E:" (error-to-string E)))))))) +(go t26_4 (freeze (let X 1 (let Y [[[1 2] [3 4]] [[5] 6] 7 8] 1)))) +(define f27 N [H | T] Y -> (tr 0 (if (number? (tl T)) (tr 6 (tl (tl (tl N)))) (tl (hd (tl H))))) + N X Y -> (and (not (let X (hd (tl (tl X))) false)) (cons? (hd (tl (tl X)))))) +(go t27_0 (freeze (f27 2 true 2.5))) +(go t27_1 (freeze (f27 1 0 a))) +(go t27_2 (freeze (f27 3 [] true))) +(go t27_3 (freeze (f27 1 2.5 [a | b]))) +(go t27_4 (freeze (f27 1 [a | b] [1]))) +(define f28 N [H | T] Y -> (let V1 (thaw (freeze Y)) (trap-error [[1 [2 [3 [4]]]] 5] (/. E (cn "E:" (error-to-string E))))) + N X Y -> (if (> (pl 1) 0) (tl N) (let V1 undefinedf (V1 [[[1 2] [3 4]] [[5] 6] 7 8] X)))) +(go t28_0 (freeze (f28 0 false true))) +(go t28_1 (freeze (f28 2 [] false))) +(go t28_2 (freeze (f28 1 [] a))) +(go t28_3 (freeze (f28 2 1 [[1] 2]))) +(go t28_4 (freeze (f28 0 [[1] 2] "s"))) +(define f29 N X Y -> (trap-error (or (> (pl (tr 2 (thaw (freeze X)))) 1) (= (cond ((cons? X) N)) (or (let V1 X (cons? (tl Y))) (and false (cons? X))))) (/. E (cn "E:" (error-to-string E))))) +(go t29_0 (freeze (f29 1 [a | b] [[[1]] | c]))) +(go t29_1 (freeze (f29 2 0 []))) +(go t29_2 (freeze (f29 3 [1] a))) +(go t29_3 (freeze (f29 1 0 [1]))) +(go t29_4 (freeze (f29 3 a [[1] 2]))) +(define f30 N X Y -> (hd (tl (tl N)))) +(go t30_0 (freeze (f30 0 [] [1]))) +(go t30_1 (freeze (f30 0 "s" a))) +(go t30_2 (freeze (f30 0 [[[1]] | c] [a | b]))) +(go t30_3 (freeze (f30 1 [[[1]] | c] [[1 2] [3]]))) +(go t30_4 (freeze (f30 3 [[1] 2] a))) +(define f31 N [H | T] Y -> (let V1 two (V1 (let V2 7 (V2 (tl (hd Y)))))) + N X Y -> (cond ((let V1 (let V2 (/. V3 V4 (if (> N 0) (f31 (- N 1) (if (> N 0) (f31 (- N 1) "s" (/. Q Y)) (hd (hd (tl Y)))) (/. Q X)) (if (cons? Y) [[[1 2] [3 4]] [[5] 6] 7 8] (hd Y)))) (V2 (if (and (cons? (tl (tl X))) false) (if false [] a) (if true (tl Y) a)) (if (> N 0) (f31 (- N 1) (hd (tl (hd Y))) (/. Q (if true N (tl N)))) (do (tr 7 0) (tl (hd Y)))) [(tl (hd X)) | ((/. V5 V6 (tl V6)) (tl (tl N)))])) (not (cons? (hd (hd (hd N)))))) (hd (hd (tl N)))) ((and (or (number? (trap-error N (/. E (cn "E:" (error-to-string E))))) (not (= (hd (hd X)) (hd X)))) (let V7 (let V8 (if (cons? (tl (hd X))) "s" [[1 [2 [3 [4]]]] 5]) [[[1 [2 [3 [4]]]] 5] | (tl (tl X))]) (and (> (pl V7) 1) (number? "s")))) (tr 8 (if (cons? N) (tr 6 [N | "s"]) (and (and true false) (or true false))))) (true (let V9 undefinedf (V9 (let V10 curry2 (V10 (let V11 (/. V12 ((/. V12 V13 N) (tl X) (tl N))) (V11 (if false Y 0) (hd X))) (if (> (pl "s") 1) (let V14 curry2 (V14 Y X)) (let V15 7 (V15 [[1 [2 [3 [4]]]] 5] (hd N) (tl (hd (tl N))) [[1 [2 [3 [4]]]] 5])))))))))) +(go t31_0 (freeze (f31 2 [] [a | b]))) +(go t31_1 (freeze (f31 1 [1] [a | b]))) +(go t31_2 (freeze (f31 2 [[[1 2] [3 4]] [[5] 6] 7 8] 1))) +(go t31_3 (freeze (f31 2 [[[1 2] [3 4]] [[5] 6] 7 8] 1))) +(go t31_4 (freeze (f31 2 [[1] 2] 0))) +(define f32 N [[A | B] C] Y -> (let V1 three (V1 (or (if (let V2 [[1 [2 [3 [4]]]] 5] true) (cons? A) (cons? (hd (tl B)))) (if (> (pl C) 1) (cons? Y) (> (pl (tl (tl A))) 1))))) + N X [A | B] -> (if (cons? (hd (hd (hd N)))) [(cond ((let V1 (hd (hd (tl N))) false) (hd N)) (true (hd N))) | (if (> N 0) (cons N (f32 (- N 1) (and true true) (tl (tl X)))) (hd (hd B)))] (let V2 (/. V3 V4 V5 ((/. V6 N (let V7 X a)))) (V2 (or (if (cons? (tl (hd (hd X)))) false (cons? (tl N))) (cons? (hd (tl N)))) ((/. B V8 V9 B (if false B "s")) (trap-error N (/. E (cn "E:" (error-to-string E)))))))) + N X Y -> (do (tr 1 0) ((/. X V2 V3 V4 (cond ((if true (cons? (tl (tl (hd Y)))) true) (cond (false a))) (true (two N "s")))) (tr 4 (if (> N 0) (cons (tl X) (f32 (- N 1) [[[1 2] [3 4]] [[5] 6] 7 8] Y)) [])) [(if (cons? (hd X)) (tl (tl (tl Y))) Y) | (two (tl Y) (hd Y))] (two (two (hd (hd (tl X))) N) (let V1 N N))))) +(go t32_0 (freeze (f32 3 2.5 [1]))) +(go t32_1 (freeze (f32 0 a [1 2]))) +(go t32_2 (freeze (f32 3 "s" false))) +(go t32_3 (freeze (f32 3 [[[1 2] [3 4]] [[5] 6] 7 8] [[1 2] [3]]))) +(go t32_4 (freeze (f32 2 [1] true))) +(define f33 N [[A | B] C] Y -> Y + N X [A | B] -> A + N X Y -> (if (> N 0) (cons [(do (tr 4 0) (hd (hd X))) | (tr 0 (hd (tl (hd X))))] (f33 (- N 1) (or (cons? N) (or false false)) [(cond ((cons? (hd Y)) N) (true (tl Y))) | [[[[1 2] [3 4]] [[5] 6] 7 8] | [[[1 2] [3 4]] [[5] 6] 7 8]]])) (if (> N 0) (f33 (- N 1) (cond (true (tl (hd Y))) (false 0) (true (hd (hd (tl X))))) (/. Q (if (> N 0) (cons 1 (f33 (- N 1) X X)) Y))) (let V1 (three 1 2) (V1 X (tl (hd (tl Y))) N))))) +(go t33_0 (freeze (f33 2 0 [1]))) +(go t33_1 (freeze (f33 0 [[1 2] [3]] [[1 [2 [3 [4]]]] 5]))) +(go t33_2 (freeze (f33 1 2.5 [[[1]] | c]))) +(go t33_3 (freeze (f33 1 a false))) +(go t33_4 (freeze (f33 2 [[[1 2] [3 4]] [[5] 6] 7 8] 1))) +(go t34_0 (freeze (let X [1] (let Y 1 (let V1 (do (tr 6 0) (if true (hd (hd (hd Y))) Y)) (let V2 (/. V3 V4 V5 V6 (trap-error (tl (tl V4)) (/. E (cn "E:" (error-to-string E))))) (V2 (let V7 three (V7 X (hd X) (hd (tl (tl V1))) (hd Y)))))))))) +(go t34_1 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y a (two (tl X) (if (cons? (hd (hd X))) (do (tr 3 0) 1) Y)))))) +(go t34_2 (freeze (let X 1 (let Y [a | b] (trap-error (trap-error Y (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E)))))))) +(go t34_3 (freeze (let X "s" (let Y [[1] 2] (two (do (tr 3 0) (cond (false X) (true [[[1 2] [3 4]] [[5] 6] 7 8]))) (and (= 2.5 Y) (or true false))))))) +(go t34_4 (freeze (let X "s" (let Y true 0)))) +(go t35_0 (freeze (let X 1 (let Y 2.5 (and (not (if (if (cons? (hd (hd (tl X)))) (cons? (tl X)) false) (= (hd (tl (hd Y))) X) (> (pl 0) 0))) (not (and (and true (cons? (hd (tl X)))) (> (pl (hd (hd (hd Y)))) 0)))))))) +(go t35_1 (freeze (let X [a | b] (let Y [[1 [2 [3 [4]]]] 5] (if (let V1 (trap-error (thaw (freeze (tl X))) (/. E (cn "E:" (error-to-string E)))) (cons? Y)) (if (= Y "s") (or (cons? (hd (hd (tl Y)))) (or true true)) (and (or (cons? (tl (tl (hd Y)))) true) (not true))) (cond ((cons? (hd X)) (tl (hd (hd Y)))) (true ((/. V6 V7 V8 V9 V10 (if true V7 V8)) (tl (hd (tl Y))) (if (cons? X) (hd (tl X)) X) (let V2 (/. V3 V4 V5 Y "s") (V2 (tl X) X X a (tl Y))))))))))) +(go t35_2 (freeze (let X [a | b] (let Y 0 (and (if (or (if true (cons? (hd (hd X))) true) (not false)) (> (pl (trap-error (hd (tl Y)) (/. E (cn "E:" (error-to-string E))))) 0) (number? 1)) (number? (let V11 (tl (hd Y)) (thaw (freeze V11))))))))) +(go t35_3 (freeze (let X "s" (let Y 2.5 (thaw (freeze (do (tr 1 0) Y))))))) +(go t35_4 (freeze (let X true (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (let V12 (/. Y V13 V14 V15 (if (cons? (hd V14)) (let V16 curry2 (V16 (do (tr 2 0) (hd V15)) ((/. V17 V18 V19 V20 V21 V15) (hd V13) 1 (hd (tl (hd X)))))) a)) (V12 [])))))) +(define f36 N [[A | B] C] Y -> (let C A (tr 0 (if (> N 0) (cons B (f36 (- N 1) (if (> N 0) (f36 (- N 1) (hd (tl (tl Y))) C) []) (tr 5 B))) (and (cons? (hd (hd (tl A)))) false)))) + N X [A | B] -> (trap-error (let N (trap-error (trap-error (tl (hd N)) (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E)))) ((/. V1 V2 B V3 [V3 | a]))) (/. E (cn "E:" (error-to-string E)))) + N X Y -> (and (cons? (tl (hd (tl X)))) (if (cons? (tl (hd (tl X)))) (number? (tl (tl Y))) (cons? (hd (hd Y)))))) +(go t36_0 (freeze (f36 0 [1] 0))) +(go t36_1 (freeze (f36 0 a a))) +(go t36_2 (freeze (f36 1 2.5 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t36_3 (freeze (f36 1 "s" "s"))) +(go t36_4 (freeze (f36 0 [1 2] [1]))) +(define f37 N [[A | B] C] Y -> (thaw (freeze (tl B))) + N X [A | B] -> (if (> N 0) (f37 (- N 1) (cond ((if (> (pl B) 1) (not false) (cons? B)) ((/. V1 V2 V3 X (if true a V2)) (if (cons? (hd (hd X))) 0 A))) ((> (pl (if (> N 0) (f37 (- N 1) A (hd N)) [[1 [2 [3 [4]]]] 5])) 1) (do (tr 1 0) [N | B]))) (if (> N 0) (f37 (- N 1) (thaw (freeze (cond (true N) ((cons? (tl (hd N))) "s") (false X) (true (tl (tl N)))))) (if (cons? (hd B)) (if (> N 0) (f37 (- N 1) (hd (hd B)) A) (tl B)) (two A (tl (hd X))))) (or (number? X) (cons? X)))) (or (cons? N) (cons? (tl (tl (hd B)))))) + N X Y -> (if (> (pl (two [Y | [[[1 2] [3 4]] [[5] 6] 7 8]] (or true true))) 0) (and (number? (if (> N 0) (f37 (- N 1) (tl (tl (hd Y))) (hd X)) N)) (cons? (hd X))) ((/. V2 ((/. V3 Y V4 V5 (cond (true (tl (hd (tl V5)))) ((cons? (hd (hd (hd V5)))) X) (false 1) (true (hd (hd (hd N)))))) (and true true) (tl V2))) ((/. V1 [Y | N]) (trap-error (hd Y) (/. E (cn "E:" (error-to-string E)))) (hd N))))) +(go t37_0 (freeze (f37 2 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t37_1 (freeze (f37 0 [[1 2] [3]] a))) +(go t37_2 (freeze (f37 3 [[1 [2 [3 [4]]]] 5] [[1] 2]))) +(go t37_3 (freeze (f37 2 [[[1 2] [3 4]] [[5] 6] 7 8] a))) +(go t37_4 (freeze (f37 2 [[1 2] [3]] [a | b]))) +(define f38 N X Y -> (do (tr 1 0) (tr 5 (tr 4 N)))) +(go t38_0 (freeze (f38 1 [[1] 2] 2.5))) +(go t38_1 (freeze (f38 0 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t38_2 (freeze (f38 3 "s" [[[1]] | c]))) +(go t38_3 (freeze (f38 1 [[[1 2] [3 4]] [[5] 6] 7 8] [a | b]))) +(go t38_4 (freeze (f38 3 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f39 N [H | T] Y -> (tr 4 [(if (> N 0) (f39 (- N 1) (let H 1 Y) (/. Q (do (tr 5 0) T))) ((/. V1 V2 V3 T V3) Y N (tl (tl Y)) N (tl (tl (tl T))))) | (if (> N 0) (f39 (- N 1) (if true T (hd (tl N))) (/. Q H)) (or true false))]) + N X Y -> (let V1 (/. V2 (cond ((cons? N) (tr 1 (let V3 (/. V4 X V5 "s") (V3 (hd (tl (tl Y))) N N)))) ((= (thaw (freeze (tl (hd (hd V2))))) (and (cons? (tl X)) false)) (if (= Y (tl Y)) (if (cons? X) X (hd V2)) (two (tl (tl N)) []))) (true (if (> N 0) (cons (if (> N 0) (cons 0 (f39 (- N 1) [[[1 2] [3 4]] [[5] 6] 7 8] a)) [[[1 2] [3 4]] [[5] 6] 7 8]) (f39 (- N 1) (if (> N 0) (f39 (- N 1) V2 0) []) (if (> N 0) (f39 (- N 1) Y (tl (hd Y))) X))) (if (> N 0) (f39 (- N 1) (hd (hd (hd N))) X) V2))))) (V1 (let V6 (/. V7 (let V8 (/. V9 V10 V11 V12 (cond ((cons? X) (hd (hd V12))) (false (hd (hd V10))) ((cons? (hd (hd X))) V11) (true (hd (tl V12))))) (V8 (thaw (freeze V7)) (if (> N 0) (f39 (- N 1) (tl (tl N)) (/. Q "s")) a)))) (V6 ((/. N V13 (cond (true (tl X)))) (let Y (hd Y) X)) (tr 4 Y))) (thaw (freeze (let V14 (/. V15 V16 (trap-error Y (/. E (cn "E:" (error-to-string E))))) (V14 (let V17 (tl (hd N)) Y) (tl (tl (tl X))) (if (> N 0) (f39 (- N 1) N (hd (tl Y))) (hd (tl (tl Y))))))))))) +(go t39_0 (freeze (f39 2 [[[1 2] [3 4]] [[5] 6] 7 8] [1]))) +(go t39_1 (freeze (f39 0 [] [1 2]))) +(go t39_2 (freeze (f39 3 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t39_3 (freeze (f39 1 a [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t39_4 (freeze (f39 2 [[1 2] [3]] []))) +(define f40 N [H | T] Y -> (thaw (freeze (tr 0 [(tl H) | (hd (tl Y))]))) + N X Y -> (or (not (if (> (pl (tl (hd X))) 0) (> (pl (tl (hd (tl Y)))) 1) (not true))) (and (> (pl (if false N N)) 0) (let V1 (hd X) (> (pl a) 1))))) +(go t40_0 (freeze (f40 3 false [[1] 2]))) +(go t40_1 (freeze (f40 2 [[[1 2] [3 4]] [[5] 6] 7 8] [[1 [2 [3 [4]]]] 5]))) +(go t40_2 (freeze (f40 2 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t40_3 (freeze (f40 1 [1] [1 2]))) +(go t40_4 (freeze (f40 3 1 2.5))) +(define f41 N X Y -> (let V1 (/. V2 V3 V4 V5 [(hd V3) | (if (cons? (tl (hd (tl V5)))) (thaw (freeze (cond (true (hd (hd V4))) (true a)))) (let V6 undefinedf (V6 ((/. V7 V7) 1 V4) (do (tr 1 0) V5) (tl (tl (hd V5))))))]) (V1 (hd (tl (tl X))) (let V8 (/. V9 (and (let Y (two N (hd (hd N))) (not (cons? (hd N)))) (> (pl (or (cons? (hd (hd (tl Y)))) true)) 0))) (V8 (two (tr 4 (and false true)) (tl N))))))) +(go t41_0 (freeze (f41 3 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t41_1 (freeze (f41 1 [[1] 2] [[1 2] [3]]))) +(go t41_2 (freeze (f41 1 [[1 [2 [3 [4]]]] 5] [[1] 2]))) +(go t41_3 (freeze (f41 1 2.5 [1]))) +(go t41_4 (freeze (f41 3 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t42_0 (freeze (let X [[[1]] | c] (let Y [1] [[1 [2 [3 [4]]]] 5])))) +(go t42_1 (freeze (let X [1 2] (let Y [[1 [2 [3 [4]]]] 5] ((/. Y V1 V2 V3 V4 (tr 2 (let V5 (/. V6 (tl Y)) (V5 (hd (hd (hd V3))))))) (cond ((cons? X) 2.5) (true (trap-error X (/. E (cn "E:" (error-to-string E)))))) (two (tl (hd Y)) a)))))) +(go t42_2 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y 0 (trap-error (or (number? (hd (tl Y))) (if true (cons? Y) false)) (/. E (cn "E:" (error-to-string E)))))))) +(go t42_3 (freeze (let X 0 (let Y [1] (cond ((or (let V7 X (cons? (tl (hd (tl X))))) (and false (cons? (tl (tl (tl X)))))) (let V8 (if (cons? (tl (hd X))) X Y) (let V9 (fn two) (V9 0 "s" 0)))) ((let V10 (tr 2 a) (and true true)) (let X a (if (cons? (tl (hd X))) (tl (hd (hd Y))) (hd (tl Y)))))))))) +(go t42_4 (freeze (let X [[1] 2] (let Y 1 (if (if (number? Y) (cons? (hd (hd (tl Y)))) (or (cons? (tl (tl (tl X)))) true)) (thaw (freeze (or false true))) (tr 7 (do (tr 4 0) [[1 [2 [3 [4]]]] 5]))))))) +(define f43 N X Y -> (hd (hd Y))) +(go t43_0 (freeze (f43 0 [1 2] [[1 [2 [3 [4]]]] 5]))) +(go t43_1 (freeze (f43 0 [[[1 2] [3 4]] [[5] 6] 7 8] 0))) +(go t43_2 (freeze (f43 1 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t43_3 (freeze (f43 3 [] [1]))) +(go t43_4 (freeze (f43 1 [] false))) +(define f44 N X Y -> (if (and (cons? (tl X)) (cons? Y)) (hd (tl X)) (if false (tl N) Y))) +(go t44_0 (freeze (f44 0 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t44_1 (freeze (f44 2 false false))) +(go t44_2 (freeze (f44 1 [1] a))) +(go t44_3 (freeze (f44 3 2.5 true))) +(go t44_4 (freeze (f44 3 [] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t45_0 (freeze (let X 0 (let Y a (two (and (if false (cons? (tl (tl X))) false) (let X (hd (hd X)) false)) [(let V1 [[1 [2 [3 [4]]]] 5] (tl (tl (tl V1)))) | (trap-error (tl X) (/. E (cn "E:" (error-to-string E))))]))))) +(go t45_1 (freeze (let X true (let Y [[1] 2] [X | (trap-error (if (cons? (hd Y)) (hd (hd (tl X))) Y) (/. E (cn "E:" (error-to-string E))))])))) +(go t45_2 (freeze (let X [1 2] (let Y [[1 2] [3]] (thaw (freeze (do (tr 7 0) (do (tr 4 0) X)))))))) +(go t45_3 (freeze (let X [1] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (if (let V2 (hd (hd Y)) (or (cons? (tl X)) true)) (let V3 (/. V4 V5 V6 (trap-error (tl Y) (/. E (cn "E:" (error-to-string E))))) (V3 Y (and true true) [[1 [2 [3 [4]]]] 5])) Y))))) +(go t45_4 (freeze (let X [a | b] (let Y [[1] 2] (let V7 (let V8 (/. V9 (trap-error (hd (tl Y)) (/. E (cn "E:" (error-to-string E))))) (V8 (two [[[1 2] [3 4]] [[5] 6] 7 8] (hd (hd (hd Y)))))) (if (number? (hd Y)) (cond (true Y) ((cons? (hd (tl X))) (hd (hd X)))) (and true true))))))) +(define f46 N X Y -> Y) +(go t46_0 (freeze (f46 3 "s" [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t46_1 (freeze (f46 2 [[[1]] | c] "s"))) +(go t46_2 (freeze (f46 1 false []))) +(go t46_3 (freeze (f46 1 true [a | b]))) +(go t46_4 (freeze (f46 0 [[1 2] [3]] []))) +(define f47 N [[A | B] C] Y -> B + N X [A | B] -> (if (> (pl (let V1 (/. A (let V2 one (V2 A "s"))) (V1 (tr 3 A) (if (> N 0) (f47 (- N 1) (hd (tl (hd X))) (/. Q 0)) X)))) 1) (cond ((if (number? B) (= X (hd (tl X))) (cons? (tl (hd N)))) (if (> N 0) (f47 (- N 1) (tl A) (/. Q [B | a])) (let V3 N 2.5))) ((not (number? (hd (tl X)))) (or (or (cons? (tl (tl (hd A)))) (cons? (hd (tl (tl B))))) (cons? (tl X)))) (true (let V4 (tl (tl (hd N))) (let V5 (/. V6 B) (V5 [[1 [2 [3 [4]]]] 5] 1))))) (if (> N 0) (cons (let V7 (three 1 2) (V7 (or true false) (tl (tl (tl A))))) (f47 (- N 1) [(if false N A) | (if (cons? (tl B)) 1 N)] (if (> N 0) (cons (if (> N 0) (f47 (- N 1) X B) B) (f47 (- N 1) (hd (tl (tl A))) (trap-error [[1 [2 [3 [4]]]] 5] (/. E (cn "E:" (error-to-string E)))))) (if false A B)))) (if (> N 0) (cons (if (> N 0) (f47 (- N 1) A [[1 [2 [3 [4]]]] 5]) []) (f47 (- N 1) [A | X] (cond ((cons? X) (tl (hd X))) (false (hd (hd (tl A)))) ((cons? (hd (tl X))) N)))) (and (cons? (tl N)) false)))) + N X Y -> (hd (tl (hd Y)))) +(go t47_0 (freeze (f47 3 1 a))) +(go t47_1 (freeze (f47 1 "s" 2.5))) +(go t47_2 (freeze (f47 3 [1] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t47_3 (freeze (f47 0 true [1]))) +(go t47_4 (freeze (f47 0 a false))) +(go t48_0 (freeze (let X 0 (let Y 2.5 X)))) +(go t48_1 (freeze (let X 0 (let Y false (let V1 1 Y))))) +(go t48_2 (freeze (let X [] (let Y [[1] 2] (if (cons? (hd (hd X))) (if (not (not (if true true (cons? (hd X))))) Y (trap-error X (/. E (cn "E:" (error-to-string E))))) (or (or (cons? (hd Y)) (cons? (hd X))) (cons? Y))))))) +(go t48_3 (freeze (let X "s" (let Y [1] (let V2 curry2 (V2 (do (tr 1 0) (two (two (thaw (freeze Y)) (or true true)) (tr 6 (let V3 undefinedf (V3 [[[1 2] [3 4]] [[5] 6] 7 8] (tl (tl (tl X))) (hd (tl (hd Y))) [[[1 2] [3 4]] [[5] 6] 7 8]))))) (let V4 (fn two) (V4 (tl X))))))))) +(go t48_4 (freeze (let X [[1] 2] (let Y false (trap-error X (/. E (cn "E:" (error-to-string E)))))))) +(define f49 N [H | T] Y -> (or (= (if (cons? (tl (tl H))) (tl H) (hd (hd (hd Y)))) (if (> N 0) (f49 (- N 1) [[1 [2 [3 [4]]]] 5] (/. Q (hd (tl (hd H))))) H)) (cons? T)) + N X Y -> (if (> N 0) (f49 (- N 1) (if (not true) ((/. V1 N [])) (if (> N 0) (f49 (- N 1) (hd (hd N)) Y) N)) (hd (tl (tl X)))) (thaw (freeze (thaw (freeze Y)))))) +(go t49_0 (freeze (f49 1 true [[1] 2]))) +(go t49_1 (freeze (f49 0 1 true))) +(go t49_2 (freeze (f49 1 "s" [a | b]))) +(go t49_3 (freeze (f49 3 [[[1]] | c] "s"))) +(go t49_4 (freeze (f49 3 2.5 [[1] 2]))) +(define f50 N X Y -> (tr 0 (let V1 two (V1 (if (or (not false) (if false true (cons? Y))) (let V2 (/. V3 V4 [Y | N]) (V2 (trap-error (hd (tl (hd N))) (/. E (cn "E:" (error-to-string E)))) [(tl (hd (hd X))) | (hd (tl (tl Y)))])) (cond ((cons? (hd X)) (if (> N 0) (f50 (- N 1) (tl (hd (tl Y))) (/. Q Y)) (tl (tl Y)))) ((cons? (tl X)) (if (cons? (tl Y)) N (hd (tl X)))) ((not false) (let V5 (/. V6 "s") (V5 X Y))) (true (cond ((cons? (hd (tl Y))) (hd (hd X))) ((cons? (hd (tl (tl X)))) (hd (tl N))) (true (tl (hd X))) (true X))))) (tl (hd (tl X))) (if (> N 0) (f50 (- N 1) (let V7 (thaw (freeze X)) (if (> N 0) (f50 (- N 1) [[[1 2] [3 4]] [[5] 6] 7 8] (/. Q X)) Y)) (/. Q X)) (hd (tl Y))))))) +(go t50_0 (freeze (f50 3 [[1 2] [3]] 2.5))) +(go t50_1 (freeze (f50 0 [[1] 2] "s"))) +(go t50_2 (freeze (f50 2 [1] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t50_3 (freeze (f50 3 [[[1]] | c] true))) +(go t50_4 (freeze (f50 3 [[1 2] [3]] []))) +(define f51 N [[A | B] C] Y -> (and (= (if (or (cons? (tl (hd N))) (cons? N)) N (and (cons? A) true)) A) (> (pl (if (cons? (tl Y)) ((/. Y C A) (tl B) 2.5) [Y | Y])) 0)) + N X [A | B] -> (cond ((not (not (or false false))) (cond ((cons? (tl (tl N))) (trap-error (do (tr 4 0) "s") (/. E (cn "E:" (error-to-string E))))) ((cons? (hd X)) (thaw (freeze (let V1 (/. V2 V3 V4 (tl A)) (V1 (hd A) (hd A) X))))) (true (if (> N 0) (cons (let V5 "s" N) (f51 (- N 1) (let V6 X N) (if (cons? (tl X)) (hd N) A))) (if (cons? (hd (hd X))) B (hd (hd A))))))) (true (if (> N 0) (cons (if (let V7 B true) (if (> N 0) (f51 (- N 1) B (/. Q A)) 2.5) (if (> N 0) (f51 (- N 1) B B) N)) (f51 (- N 1) (let V8 (thaw (freeze (hd (tl (tl X))))) (tr 6 [[1 [2 [3 [4]]]] 5])) (if (not false) (thaw (freeze (hd (tl N)))) (hd (hd A))))) (let V9 one (V9 (if (> N 0) (f51 (- N 1) N (/. Q [[1 [2 [3 [4]]]] 5])) [[[1 2] [3 4]] [[5] 6] 7 8]) (if true A (hd (hd (hd B)))) (if (> N 0) (f51 (- N 1) B (/. Q (hd (tl (tl A))))) A)))))) + N X Y -> (or (cons? (hd (tl (tl Y)))) (if (cons? (tl (tl (hd Y)))) (let V1 (if false (tl (hd N)) (tl (tl (tl Y)))) (cons? V1)) (= (if (cons? (hd (hd (tl X)))) X [[[1 2] [3 4]] [[5] 6] 7 8]) (cond (false (tl N)) (false Y) ((cons? Y) X)))))) +(go t51_0 (freeze (f51 1 2.5 [[[1]] | c]))) +(go t51_1 (freeze (f51 1 [[1 [2 [3 [4]]]] 5] "s"))) +(go t51_2 (freeze (f51 2 [[1] 2] 1))) +(go t51_3 (freeze (f51 3 [[1 2] [3]] 2.5))) +(go t51_4 (freeze (f51 2 [] "s"))) +(define f52 N [H | T] Y -> (cond ((cons? (tl N)) [(let V1 (three 1 2) (V1 (hd (hd Y)) Y (hd N) N)) | (tl H)]) ((if (cons? (tl H)) (> (pl H) 1) (cons? (hd N))) H) ((if (cons? (hd T)) (number? T) (> (pl N) 1)) (let V2 (/. V3 H V4 (if (> N 0) (f52 (- N 1) N (/. Q (hd (tl (tl H))))) a)) (V2 (if (> N 0) (f52 (- N 1) H (tl H)) H) (do (tr 7 0) N) (do (tr 7 0) 2.5))))) + N X Y -> (let V1 (if (let V2 (hd (tl (tl N))) false) (cond (false N) (true a)) (thaw (freeze []))) (and (if true false true) (cons? (tl (hd (hd V1))))))) +(go t52_0 (freeze (f52 0 [[1 2] [3]] 1))) +(go t52_1 (freeze (f52 1 "s" false))) +(go t52_2 (freeze (f52 2 2.5 [1]))) +(go t52_3 (freeze (f52 1 [[[1]] | c] true))) +(go t52_4 (freeze (f52 3 [[1] 2] [1 2]))) +(go t53_0 (freeze (let X 1 (let Y 2.5 (hd (hd X)))))) +(go t53_1 (freeze (let X [[1 [2 [3 [4]]]] 5] (let Y 1 (if (if (cons? (tl (tl Y))) (> (pl (or (or true true) (not false))) 0) (cons? (tl Y))) (trap-error (if (cons? (hd (hd (hd Y)))) (hd X) (let V1 (/. Y (let V2 (/. Y V3 V4 V5 [[[1 2] [3 4]] [[5] 6] 7 8]) (V2 X (hd Y)))) (V1 (tr 6 X)))) (/. E (cn "E:" (error-to-string E)))) (cond ((not (number? (hd (hd (tl Y))))) (if (and (not true) (or true false)) [(if (cons? (tl (hd (tl Y)))) Y (tl (tl (tl Y)))) | (if false Y Y)] Y)) ((not (if (number? X) (cons? (hd X)) (cons? (hd (hd (hd Y)))))) (if (cons? (hd X)) "s" (and (cons? (hd Y)) (= (hd Y) (hd (tl Y)))))) (true (or (or (number? Y) (= 2.5 1)) (cons? Y))))))))) +(go t53_2 (freeze (let X 1 (let Y false (cond ((not (let Y (let V6 (/. Y V7 V8 [(tl (tl (hd X))) | Y]) (V6 (hd X))) (if (> (pl X) 0) (= (hd (tl (hd X))) "s") (not (cons? Y))))) ((/. V17 Y V18 X V19 V19) ((/. Y V9 V10 V11 V12 (two ((/. V13 V14 V15 V16 (tl V13)) (hd (hd V10)) (tl (tl (hd Y))) [[[1 2] [3 4]] [[5] 6] 7 8] a [[[1 2] [3 4]] [[5] 6] 7 8]) (trap-error V10 (/. E (cn "E:" (error-to-string E))))))) (do (tr 1 0) (if (cons? (tl (tl X))) (tl Y) (tl X))))) ((> (pl [[(hd (hd (hd X))) | (tr 6 a)] | (let V20 (and (cons? (hd Y)) true) (if true X X))]) 0) X) ((cons? (tl (tl X))) a)))))) +(go t53_3 (freeze (let X a (let Y false (let V21 undefinedf (V21 Y [[(let V22 (/. V23 V24 V25 V26 (if (cons? (tl (hd (hd V26)))) V23 (tl (tl V24)))) (V22 (if (cons? (tl (hd X))) (hd (hd Y)) (hd (tl X))))) | (let V27 two (V27 ((/. V28 Y V29 V30 1)) (if false X (hd (hd Y)))))] | X])))))) +(go t53_4 (freeze (let X 2.5 (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (do (tr 8 0) (let V31 (/. V32 V33 Y (thaw (freeze (hd (hd (hd X)))))) (V31 (hd Y) (trap-error (tr 9 Y) (/. E (cn "E:" (error-to-string E))))))))))) +(define f54 N [H | T] Y -> ((/. V1 N V2 (if true N H)) (hd (hd T)) (if true (hd Y) (tl (tl H))) (if false Y T) (tr 5 (tl H))) + N X Y -> (if (cons? (tl X)) (hd (tl N)) (if (> N 0) (f54 (- N 1) (hd Y) (/. Q N)) (tl Y)))) +(go t54_0 (freeze (f54 1 [[[1]] | c] [[1] 2]))) +(go t54_1 (freeze (f54 1 [[1 [2 [3 [4]]]] 5] [[[1]] | c]))) +(go t54_2 (freeze (f54 1 2.5 0))) +(go t54_3 (freeze (f54 3 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t54_4 (freeze (f54 1 false []))) +(define f55 N X Y -> (if (if (cons? (tl (hd (hd X)))) false (cons? (hd (tl Y)))) (hd (tl N)) [(tl Y) | 0])) +(go t55_0 (freeze (f55 3 true 1))) +(go t55_1 (freeze (f55 2 [1] 0))) +(go t55_2 (freeze (f55 1 [[1 [2 [3 [4]]]] 5] "s"))) +(go t55_3 (freeze (f55 3 1 [[[1]] | c]))) +(go t55_4 (freeze (f55 1 [a | b] [1 2]))) +(define f56 N [H | T] Y -> (if (cons? (tl (tl H))) (thaw (freeze (tl (hd N)))) (cond ((cons? (hd (tl T))) (tl (tl Y))) (true (hd (hd (tl N)))) (true (tl T)))) + N X Y -> (tl (hd N))) +(go t56_0 (freeze (f56 1 0 0))) +(go t56_1 (freeze (f56 2 a []))) +(go t56_2 (freeze (f56 3 [[[1 2] [3 4]] [[5] 6] 7 8] 2.5))) +(go t56_3 (freeze (f56 3 [1] [1 2]))) +(go t56_4 (freeze (f56 1 [[[1]] | c] 1))) +(define f57 N [[A | B] C] Y -> (if (> N 0) (cons A (f57 (- N 1) (let V1 three (V1 (if (= (if (> N 0) (f57 (- N 1) Y (/. Q (hd (tl B)))) (hd B)) (tr 1 (tl (tl Y)))) (tl (hd (tl Y))) (let V2 (two 1) (V2 A))))) (if (number? (do (tr 4 0) [a | B])) B (if (or (cons? (hd (hd (tl A)))) (cons? B)) (cond ((and false false) (if (> N 0) (cons [[[1 2] [3 4]] [[5] 6] 7 8] (f57 (- N 1) A (hd (tl (hd Y))))) C)) ((> (pl Y) 1) (hd (tl C))) ((number? Y) (let V3 curry2 (V3 B [[[1 2] [3 4]] [[5] 6] 7 8] C))) (true (two (hd (tl B)) C))) (let V4 undefinedf (V4 (let V5 one (V5 (hd (tl (hd C))) (tl N))))))))) (tr 7 [(and (and false (cons? (hd (tl (tl Y))))) (> (pl B) 1)) | (trap-error (if (> N 0) (cons C (f57 (- N 1) A Y)) a) (/. E (cn "E:" (error-to-string E))))])) + N X [A | B] -> (if (> N 0) (f57 (- N 1) ((/. V3 V4 V5 (let V6 (/. V3 V7 (trap-error (if true 1 B) (/. E (cn "E:" (error-to-string E))))) (V6 (let B (two a [[[1 2] [3 4]] [[5] 6] 7 8]) (if (cons? (tl (hd X))) (tl B) N)) ((/. V8 (let V9 (/. V10 (hd (hd (hd B)))) (V9 a))))))) (tl B) (tl (hd B)) (do (tr 1 0) (if (> N 0) (f57 (- N 1) (tr 8 A) (/. Q (let V1 (/. V2 "s") (V1 (tl (tl (hd X))) X)))) [X | A]))) (if (= (hd N) N) (if (> N 0) (cons (do (tr 8 0) (cond (false 0) (true 0))) (f57 (- N 1) (do (tr 0 0) (let V11 (/. V12 V13 V14 (tl (tl V14))) (V11 0))) (and (cons? (tl N)) (number? (tl A))))) [(if (> N 0) (f57 (- N 1) B (/. Q (tl B))) B) | (let V15 (/. V16 N) (V15 X (tl N)))]) (or (or (let V17 a false) (not true)) (not (if false false (cons? (hd (tl A)))))))) (and (= N (let V18 two (V18 (hd X) [A | (hd (tl (hd A)))]))) (= (if (cons? (tl X)) (if (> N 0) (cons 1 (f57 (- N 1) A [[[1 2] [3 4]] [[5] 6] 7 8])) X) (cond (false a))) ((/. V19 V20 V21 V22 N (trap-error (tl (tl A)) (/. E (cn "E:" (error-to-string E))))) (if (> N 0) (f57 (- N 1) A 1) X) (let A a A) (or (cons? (tl (hd A))) (cons? (hd A))) (two A A))))) + N X Y -> [[(thaw (freeze (hd Y))) | (if (> N 0) (f57 (- N 1) (tl Y) (/. Q (let V1 (/. V2 N V3 (two 0 1)) (V1 ((/. N X V4 (hd N))) [(hd N) | [[[1 2] [3 4]] [[5] 6] 7 8]] (let V5 curry2 (V5 (hd (hd X)))))))) (trap-error (let V6 curry2 (V6 [[[1 2] [3 4]] [[5] 6] 7 8])) (/. E (cn "E:" (error-to-string E)))))] | (if (> N 0) (f57 (- N 1) (let N (if (cons? (hd N)) (tr 6 Y) (tl (tl Y))) (tr 6 (if (> N 0) (f57 (- N 1) N (/. Q (tl X))) Y))) (two (if (> N 0) (f57 (- N 1) (tl (tl (tl X))) (tr 6 (hd (tl Y)))) (let V7 (/. V8 []) (V7 [] N))) (tl (hd X)))) (thaw (freeze (let V9 (let V10 (/. V11 V12 V13 V14 0) (V10 (hd (hd N)))) (tl X)))))]) +(go t57_0 (freeze (f57 2 [[1] 2] a))) +(go t57_1 (freeze (f57 2 0 a))) +(go t57_2 (freeze (f57 1 true "s"))) +(go t57_3 (freeze (f57 0 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t57_4 (freeze (f57 1 [[[1]] | c] "s"))) +(define f58 N [[A | B] C] Y -> (cond ((cons? (hd (hd A))) [(tl (tl Y)) | C]) ((= (hd (hd (tl C))) 1) (trap-error B (/. E (cn "E:" (error-to-string E))))) ((cons? (hd (tl (hd B)))) (tr 1 (tl Y))) (true (hd C))) + N X [A | B] -> (two (trap-error (tl (tl (tl A))) (/. E (cn "E:" (error-to-string E)))) (tr 2 0)) + N X Y -> (hd (hd X))) +(go t58_0 (freeze (f58 2 0 []))) +(go t58_1 (freeze (f58 3 [[1 2] [3]] 0))) +(go t58_2 (freeze (f58 1 false 0))) +(go t58_3 (freeze (f58 2 1 [[1 [2 [3 [4]]]] 5]))) +(go t58_4 (freeze (f58 0 [1 2] [1 2]))) +(define f59 N X Y -> (cond ((not (cons? (tl (tl Y)))) (tl X)) ((cons? (tl X)) ((/. V1 V2 V3 (hd (tl (tl V3)))) X (tl (hd (tl N))))) ((cons? (hd (hd N))) [(tl (hd (tl N))) | X]) (true (let V4 (three 1 2) (V4 Y (hd (tl (tl Y)))))))) +(go t59_0 (freeze (f59 1 [] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t59_1 (freeze (f59 1 [1 2] false))) +(go t59_2 (freeze (f59 0 true []))) +(go t59_3 (freeze (f59 3 [[[1 2] [3 4]] [[5] 6] 7 8] false))) +(go t59_4 (freeze (f59 3 true [[[1]] | c]))) +(go t60_0 (freeze (let X 0 (let Y [] (do (tr 4 0) (tl (hd (tl X)))))))) +(go t60_1 (freeze (let X true (let Y [1] 1)))) +(go t60_2 (freeze (let X true (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (two [(or (or (= (tl (tl (hd Y))) (tl (tl X))) (cons? Y)) (= (hd (hd Y)) (cond (true Y) (false Y) (true X)))) | ((/. V1 V2 (let V3 (two 1) (V3 0 (two 1 V2) (hd (hd Y)) (let V4 [[1 [2 [3 [4]]]] 5] (tl (hd V2)))))))] (or (if (or (if false false (cons? (hd (hd Y)))) (> (pl X) 1)) (if (or (cons? (hd (hd (hd Y)))) false) (cons? (tl (tl X))) (or true false)) (cons? (hd (tl (tl X))))) (let Y (do (tr 6 0) [[[1 [2 [3 [4]]]] 5] | X]) (if (and (cons? X) true) (cons? (hd X)) (cons? (hd Y)))))))))) +(go t60_3 (freeze (let X [1] (let Y a [(if (= (let V5 (/. V6 X X X (trap-error (hd (tl X)) (/. E (cn "E:" (error-to-string E))))) (V5 (if true (tl X) Y) (trap-error X (/. E (cn "E:" (error-to-string E)))) Y ((/. V7 X V8 X) 0 Y Y Y) Y)) (thaw (freeze (tr 6 (tl X))))) [] (two (or (cons? (hd Y)) (> (pl Y) 0)) (tl X))) | ((/. V9 Y (thaw (freeze (let V10 (thaw (freeze (tl (tl Y)))) (if true (hd (hd (hd X))) (hd (tl X))))))))])))) +(go t60_4 (freeze (let X 2.5 (let Y [[1 2] [3]] Y)))) +(define f61 N [[A | B] C] Y -> (let V1 (tr 7 (if (> N 0) (cons (let V2 (/. V3 V4 V5 (tl Y)) (V2 C)) (f61 (- N 1) (if (> N 0) (f61 (- N 1) (tl Y) N) Y) (or false false))) (if true (hd C) (tl (tl (tl Y)))))) (if (> N 0) (f61 (- N 1) (if (and false false) (if (> N 0) (cons (tl (tl (tl N))) (f61 (- N 1) (tl (tl (hd V1))) (hd (hd Y)))) C) (tr 7 (hd (hd (hd B))))) (/. Q (cond ((cons? (tl A)) (do (tr 5 0) (hd Y))) (true (or false true))))) (if (> (pl Y) 1) (if true N A) (cond ((cons? C) A) (false 1) (false (hd (tl (tl A)))))))) + N X [A | B] -> (hd B) + N X Y -> (cond ((= (tr 4 (or (cons? (tl (tl X))) true)) (hd (hd Y))) (cond ((> (pl (if true a (hd X))) 0) (if (> N 0) (f61 (- N 1) (tr 1 X) (two (tl X) N)) (tl (hd (tl N))))) ((cons? (hd (tl Y))) (or (cons? (hd X)) (cons? (tl N)))) ((not (= (tl (tl X)) X)) (if (number? N) (if (> N 0) (f61 (- N 1) (hd (tl X)) (/. Q N)) Y) (tr 2 a))))))) +(go t61_0 (freeze (f61 1 "s" "s"))) +(go t61_1 (freeze (f61 3 true "s"))) +(go t61_2 (freeze (f61 2 [[1 [2 [3 [4]]]] 5] []))) +(go t61_3 (freeze (f61 2 [] 0))) +(go t61_4 (freeze (f61 2 [[[1 2] [3 4]] [[5] 6] 7 8] 2.5))) +(go t62_0 (freeze (let X [[[1]] | c] (let Y false (thaw (freeze (let V1 three (V1 ((/. X V2 Y (and (cons? (hd (hd (hd V2)))) true))) ((/. X (thaw (freeze Y))) (two (hd (tl (tl Y))) (tl (hd Y)))))))))))) +(go t62_1 (freeze (let X 2.5 (let Y [[1 2] [3]] (two (if (or (if false false (cons? (hd (hd (tl Y))))) (or false true)) [Y | (and false true)] X) (if (or (cons? (hd (hd Y))) (or true false)) (tr 8 (if false [[[1 2] [3 4]] [[5] 6] 7 8] Y)) (thaw (freeze [[[1 2] [3 4]] [[5] 6] 7 8])))))))) +(go t62_2 (freeze (let X a (let Y [[1] 2] (trap-error (if (cons? (hd (hd Y))) ((/. V3 Y V4 (let V5 Y Y)) [(tl (tl X)) | Y]) (let V6 X (trap-error V6 (/. E (cn "E:" (error-to-string E)))))) (/. E (cn "E:" (error-to-string E)))))))) +(go t62_3 (freeze (let X [] (let Y [a | b] (thaw (freeze (hd X))))))) +(go t62_4 (freeze (let X [[1 [2 [3 [4]]]] 5] (let Y 1 (cond ((number? (if (and true false) (thaw (freeze (hd (hd (tl Y))))) (or false false))) (cond ((if (let V7 X true) (= (hd (tl X)) Y) (or (cons? (hd (hd Y))) false)) (if (let X "s" (cons? (tl Y))) (tr 2 1) (tr 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) ((and (> (pl (hd Y)) 1) (number? X)) (if (cons? (tl Y)) (cond ((cons? (hd (tl (hd X)))) (tl (tl Y))) ((cons? (tl (tl (hd Y)))) X)) (do (tr 8 0) Y))) ((cons? (hd (hd X))) (if (cons? X) (if true (hd Y) (tl Y)) (if false X (hd (tl Y))))) (true (trap-error (hd (hd (hd Y))) (/. E (cn "E:" (error-to-string E))))))) ((cons? X) (hd (hd Y))) ((number? X) (if (cons? (hd (hd Y))) X (hd (tl (hd X))))) (true (cond ((cons? Y) (trap-error (let V8 (hd X) (tl (hd V8))) (/. E (cn "E:" (error-to-string E))))) (true [((/. V9 V10 V11 (hd (tl V9))) Y) | (tr 4 Y)])))))))) +(define f63 N X Y -> (let V1 (/. V2 V3 V4 (let V5 (two 1) (V5 (two (cond (false V3) (false (hd (tl X))) (true (hd (tl (tl V3))))) (if false (tl (hd V4)) (tl V3)))))) (V1 (hd N) (if (not (cons? (tl N))) (let V6 (two 1) (V6 (or false (cons? (tl X))) (do (tr 9 0) Y) [(hd X) | (hd (hd Y))])) [(if (> N 0) (cons (hd (tl (hd N))) (f63 (- N 1) "s" X)) (tl (tl (tl N)))) | (and (cons? (tl (hd (tl X)))) true)]) (if (cons? Y) (trap-error (do (tr 2 0) (hd N)) (/. E (cn "E:" (error-to-string E)))) (if (and false true) (tl (hd Y)) N))))) +(go t63_0 (freeze (f63 2 false [a | b]))) +(go t63_1 (freeze (f63 3 "s" [1]))) +(go t63_2 (freeze (f63 2 [[1 2] [3]] [a | b]))) +(go t63_3 (freeze (f63 2 [[1 [2 [3 [4]]]] 5] []))) +(go t63_4 (freeze (f63 2 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f64 N X Y -> (and (> (pl (let V1 (/. V2 ((/. V3 V4 V5 (or true true)) (do (tr 1 0) "s") (let X "s" V2) (tr 3 (tl X)))) (V1 (do (tr 3 0) (do (tr 2 0) (tl Y))) (if (> N 0) (f64 (- N 1) (let V6 one (V6 (tl (tl (tl N))) Y)) (/. Q (if false 0 X))) (if (> N 0) (cons (hd (hd Y)) (f64 (- N 1) (hd Y) (tl (hd (hd Y))))) (tl (hd (hd X)))))))) 0) (> (pl (two (do (tr 6 0) (let V7 (/. V8 N) (V7 (hd (hd (tl N))) (tl (hd N))))) (and (= N Y) (or (cons? Y) true)))) 0))) +(go t64_0 (freeze (f64 3 true [[[1]] | c]))) +(go t64_1 (freeze (f64 2 [] [a | b]))) +(go t64_2 (freeze (f64 1 a [[1 [2 [3 [4]]]] 5]))) +(go t64_3 (freeze (f64 1 [[1 [2 [3 [4]]]] 5] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t64_4 (freeze (f64 3 "s" 1))) +(define f65 N [H | T] Y -> (if (cons? H) (if (> N 0) (f65 (- N 1) [(do (tr 4 0) (tl Y)) | (if (> N 0) (f65 (- N 1) (tl (tl N)) (hd T)) a)] [(if true (hd T) 0) | (if true (tl H) H)]) (and (cons? (tl Y)) (= H Y))) (if (> N 0) (cons (two (two (hd (hd (hd N))) 0) (hd (tl (hd T)))) (f65 (- N 1) (or (cons? (tl N)) (> (pl (tl (hd Y))) 0)) (hd (tl N)))) [(if false Y T) | (let V1 (two 1) (V1 N T))])) + N X Y -> ((/. V6 V7 N Y V8 (and (cons? V6) (and (cons? (hd (hd N))) (cons? (tl (tl (tl N))))))) (or (and (cons? N) (and false true)) (cons? (tl N))) (let V1 (/. V2 (if (> N 0) (f65 (- N 1) (tr 7 a) (if (cons? (tl (tl (tl N)))) 0 (hd Y))) (or false false))) (V1 (do (tr 5 0) (let V3 (fn two) (V3 (hd N) (hd (tl N)) (hd (tl (tl N)))))))) (two (let V4 (two 1) (V4 (tl (tl X)) (if (> N 0) (cons Y (f65 (- N 1) 1 X)) a) (tr 6 X))) (thaw (freeze (cond (true 1) (false 0))))) (thaw (freeze (let V5 (if (> N 0) (f65 (- N 1) (tl (tl (hd N))) a) N) (thaw (freeze (hd X)))))) (two (if (or false (cons? (tl (tl (tl N))))) [(hd (tl (tl X))) | [[[1 2] [3 4]] [[5] 6] 7 8]] (if (> N 0) (f65 (- N 1) (tl (hd X)) (tl (tl Y))) N)) (if (> N 0) (f65 (- N 1) (do (tr 5 0) (hd (tl (hd Y)))) (/. Q (hd (tl (tl X))))) (or (cons? (tl Y)) (cons? (tl (tl (tl N))))))))) +(go t65_0 (freeze (f65 2 [[[1 2] [3 4]] [[5] 6] 7 8] "s"))) +(go t65_1 (freeze (f65 1 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t65_2 (freeze (f65 1 [] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t65_3 (freeze (f65 2 [[1 [2 [3 [4]]]] 5] "s"))) +(go t65_4 (freeze (f65 2 1 false))) +(go t66_0 (freeze (let X [] (let Y 1 (thaw (freeze (thaw (freeze (do (tr 7 0) (tl (tl Y))))))))))) +(go t66_1 (freeze (let X [[1 2] [3]] (let Y false Y)))) +(go t66_2 (freeze (let X a (let Y [[1 [2 [3 [4]]]] 5] 0)))) +(go t66_3 (freeze (let X [1] (let Y [[1] 2] (or (> (pl (thaw (freeze (two Y (hd (tl Y)))))) 1) (cons? (hd (tl (tl Y))))))))) +(go t66_4 (freeze (let X [[1] 2] (let Y [[1 2] [3]] (or (let V1 (tr 1 (tl X)) (cons? (hd (tl (tl V1))))) (= (two (let V2 (tl (tl X)) []) (tl (hd X))) (let Y (tr 5 Y) [[[1 2] [3 4]] [[5] 6] 7 8]))))))) +(define f67 N [H | T] Y -> (if (> N 0) (f67 (- N 1) (and (cons? Y) (cons? (tl T))) (if (= (thaw (freeze H)) (trap-error 0 (/. E (cn "E:" (error-to-string E))))) N (tl (tl (tl Y))))) (let V1 one (V1 (if (> N 0) (f67 (- N 1) [Y | 0] [Y | N]) (or false true)) [(thaw (freeze T)) | (cond (false a) (true [[[1 2] [3 4]] [[5] 6] 7 8]))] (tl (hd (tl T))) (if (> N 0) (f67 (- N 1) ((/. Y V2 2.5)) (/. Q (two [[[1 2] [3 4]] [[5] 6] 7 8] []))) (or true false))))) + N X Y -> (if (> N 0) (f67 (- N 1) (tr 1 (if (cons? (hd N)) (tr 5 X) (tr 4 "s"))) (if (= (tr 2 (tl N)) (trap-error (tl N) (/. E (cn "E:" (error-to-string E))))) (tr 3 (hd (hd Y))) (if (cons? (tl N)) (if false (tl (hd Y)) X) (if (cons? (hd (hd (hd N)))) N N)))) (let V1 (if (> N 0) (cons (let V2 (/. V3 (hd (tl V3))) (V2 X 1)) (f67 (- N 1) (tr 8 (hd (tl (tl Y)))) (if (> N 0) (cons [[[1 2] [3 4]] [[5] 6] 7 8] (f67 (- N 1) a (hd (hd Y)))) (tl Y)))) (tr 2 N)) (hd (tl N))))) +(go t67_0 (freeze (f67 1 2.5 [1]))) +(go t67_1 (freeze (f67 3 [[1] 2] [[[1]] | c]))) +(go t67_2 (freeze (f67 1 [[1 [2 [3 [4]]]] 5] 2.5))) +(go t67_3 (freeze (f67 0 "s" false))) +(go t67_4 (freeze (f67 1 [[1 2] [3]] [[1 [2 [3 [4]]]] 5]))) +(define f68 N X Y -> ((/. V1 N (or (cons? (hd N)) (not (cons? (hd N))))))) +(go t68_0 (freeze (f68 3 0 2.5))) +(go t68_1 (freeze (f68 1 [1 2] false))) +(go t68_2 (freeze (f68 2 [] 1))) +(go t68_3 (freeze (f68 3 [[[1 2] [3 4]] [[5] 6] 7 8] true))) +(go t68_4 (freeze (f68 2 [[[1 2] [3 4]] [[5] 6] 7 8] a))) +(define f69 N [H | T] Y -> Y + N X Y -> [(if (> N 0) (f69 (- N 1) [(two X a) | ((/. V1 V2 V3 X V4 2.5) [[[1 2] [3 4]] [[5] 6] 7 8] N Y X N)] (/. Q X)) ((/. V9 V10 V11 (if (> N 0) (f69 (- N 1) (hd (hd Y)) (/. Q (tl (hd (tl X))))) 2.5)) (let V5 (/. V6 V7 V8 (tl (hd V7))) (V5 1 X Y (tl (tl Y)))) (do (tr 1 0) X) (cond (true (tl Y)) (true X) (true (hd (hd Y)))) (if (> N 0) (cons N (f69 (- N 1) (tl (tl (tl N))) X)) []))) | (two (tr 1 (tr 7 Y)) ((/. V12 V13 V14 V15 V16 (tr 2 Y))))]) +(go t69_0 (freeze (f69 3 true 1))) +(go t69_1 (freeze (f69 0 [[[1 2] [3 4]] [[5] 6] 7 8] [a | b]))) +(go t69_2 (freeze (f69 0 "s" 1))) +(go t69_3 (freeze (f69 2 [[1] 2] [[[1]] | c]))) +(go t69_4 (freeze (f69 1 [[1] 2] [a | b]))) +(define f70 N [[A | B] C] Y -> (trap-error (if true A C) (/. E (cn "E:" (error-to-string E)))) + N X [A | B] -> (thaw (freeze (if false B A))) + N X Y -> (do (tr 1 0) (two Y Y))) +(go t70_0 (freeze (f70 3 false a))) +(go t70_1 (freeze (f70 3 0 [[1 [2 [3 [4]]]] 5]))) +(go t70_2 (freeze (f70 3 0 0))) +(go t70_3 (freeze (f70 1 true [[1 2] [3]]))) +(go t70_4 (freeze (f70 0 [1 2] [a | b]))) +(go t71_0 (freeze (let X 1 (let Y "s" Y)))) +(go t71_1 (freeze (let X [[1 [2 [3 [4]]]] 5] (let Y [[1 [2 [3 [4]]]] 5] (two (let V1 (tl (tl (tl X))) V1) (trap-error (hd X) (/. E (cn "E:" (error-to-string E))))))))) +(go t71_2 (freeze (let X [a | b] (let Y [] (if (not true) (let V2 undefinedf (V2 X Y Y (tl (hd Y)))) (thaw (freeze (hd X)))))))) +(go t71_3 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [1] (and (cons? (hd (hd (hd X)))) (cons? (tl (hd Y)))))))) +(go t71_4 (freeze (let X [[1] 2] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (let V3 ((/. V4 V5 V6 1) (tl X) Y) (let Y V3 Y)))))) +(go t72_0 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [[[1]] | c] (cond ((let V1 (hd (hd (hd X))) (or (= (if false V1 [[1 [2 [3 [4]]]] 5]) (do (tr 5 0) (hd (hd (tl Y))))) (let V2 (let V3 (/. V4 X) (V3 (tl (hd (hd V1))))) (cons? (tl (hd V1)))))) [(if (if (cons? (hd (tl X))) (and true (cons? (tl (tl Y)))) (and false (cons? (hd (tl X))))) (let V5 (and true false) Y) (two (tl X) (or (cons? Y) true))) | (and (let X (thaw (freeze X)) (= (hd (tl X)) Y)) (= (trap-error (tl (hd (tl X))) (/. E (cn "E:" (error-to-string E)))) (tl (hd (hd X)))))])))))) +(go t72_1 (freeze (let X 0 (let Y false X)))) +(go t72_2 (freeze (let X 2.5 (let Y [] (let V6 (tl X) (do (tr 7 0) (trap-error (thaw (freeze (if true X (tl (hd (tl Y)))))) (/. E (cn "E:" (error-to-string E)))))))))) +(go t72_3 (freeze (let X 1 (let Y [1 2] (trap-error Y (/. E (cn "E:" (error-to-string E)))))))) +(go t72_4 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (thaw (freeze [(if (or (cons? (tl (tl X))) (and false (cons? (hd (tl (hd Y)))))) (thaw (freeze [])) (trap-error (if (cons? (tl (tl (hd X)))) (tl (tl Y)) (tl (tl (hd Y)))) (/. E (cn "E:" (error-to-string E))))) | (do (tr 1 0) 1)])))))) +(define f73 N [H | T] Y -> (trap-error (if (> N 0) (f73 (- N 1) (if (> N 0) (cons (if (> N 0) (f73 (- N 1) H (/. Q T)) H) (f73 (- N 1) (tl T) [(hd T) | H])) (let V1 (two 1) (V1 (tl (tl (hd T))) (hd Y)))) (let V2 (/. V3 V4 H T (let V5 one (V5 (tl (hd (hd H)))))) (V2 (do (tr 8 0) H) (if false (tl (hd (hd H))) N) (if false (tl N) (hd (tl (hd Y)))) (thaw (freeze T))))) T) (/. E (cn "E:" (error-to-string E)))) + N X Y -> (do (tr 2 0) [(let V1 (/. V2 V3 V4 V5 (if false 1 (tl (hd (hd V4))))) (V1 (if (> N 0) (f73 (- N 1) X 2.5) (hd (tl X))) (let V6 (/. V7 (hd (tl X))) (V6 (tl (hd X)))) ((/. V8 V9 V10 V8) N X) (trap-error Y (/. E (cn "E:" (error-to-string E)))))) | (two (cond (false X) ((cons? (hd (tl (tl N)))) (tl (hd (tl N)))) (true Y)) (tl (hd X)))])) +(go t73_0 (freeze (f73 3 a [[1 [2 [3 [4]]]] 5]))) +(go t73_1 (freeze (f73 3 [[[1 2] [3 4]] [[5] 6] 7 8] [a | b]))) +(go t73_2 (freeze (f73 3 1 a))) +(go t73_3 (freeze (f73 3 [a | b] [1]))) +(go t73_4 (freeze (f73 0 false [1 2]))) +(go t74_0 (freeze (let X true (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (hd Y))))) +(go t74_1 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [[1 [2 [3 [4]]]] 5] Y)))) +(go t74_2 (freeze (let X [[[1]] | c] (let Y "s" a)))) +(go t74_3 (freeze (let X a (let Y "s" (or (> (pl a) 1) (> (pl (hd Y)) 1)))))) +(go t74_4 (freeze (let X [a | b] (let Y [[1] 2] (do (tr 9 0) (if false (hd (tl (tl X))) [[1 [2 [3 [4]]]] 5])))))) +(define f75 N X Y -> (if (> N 0) (cons [(let V1 (two 1) (V1 Y (if true N (hd (tl Y))) (if (> N 0) (f75 (- N 1) Y (/. Q N)) [[[1 2] [3 4]] [[5] 6] 7 8]) (if (> N 0) (f75 (- N 1) (hd Y) (/. Q (tl (hd X)))) (tl (hd (hd X)))))) | (or (> (pl a) 0) (cons? (tl (tl Y))))] (f75 (- N 1) (hd Y) (tl (hd Y)))) (if (cons? (hd (tl (tl N)))) (let V2 (/. V3 V4 V5 N (do (tr 3 0) [])) (V2 [X | (hd Y)] (if (> N 0) (cons (hd (hd (hd Y))) (f75 (- N 1) X X)) N))) (cond ((cons? (tl (tl (tl N)))) (if (> N 0) (f75 (- N 1) (hd (tl Y)) 1) X)) ((> (pl N) 0) (thaw (freeze (hd N)))) ((cons? (tl (hd Y))) (if false Y (tl (tl (tl X))))) (true (if (> N 0) (cons (hd (hd X)) (f75 (- N 1) [[1 [2 [3 [4]]]] 5] N)) (hd (tl (tl Y))))))))) +(go t75_0 (freeze (f75 1 [[1 [2 [3 [4]]]] 5] [[1 [2 [3 [4]]]] 5]))) +(go t75_1 (freeze (f75 1 "s" "s"))) +(go t75_2 (freeze (f75 3 1 0))) +(go t75_3 (freeze (f75 0 [[1 2] [3]] 2.5))) +(go t75_4 (freeze (f75 2 [[1] 2] "s"))) +(define f76 N [H | T] Y -> (or (and (let H (trap-error (if (> N 0) (cons (hd N) (f76 (- N 1) a (tl Y))) a) (/. E (cn "E:" (error-to-string E)))) (cons? Y)) (or (cons? N) (cons? T))) (> (pl (if (cons? N) (if (> N 0) (f76 (- N 1) (if (cons? (hd H)) (tl (hd Y)) (tl (hd (hd H)))) (cond (false 0) ((cons? (tl (tl (tl Y)))) (hd Y)))) (if (> N 0) (f76 (- N 1) (hd (tl Y)) (/. Q T)) N)) T)) 0)) + N X Y -> (hd Y)) +(go t76_0 (freeze (f76 3 [[1 [2 [3 [4]]]] 5] [1]))) +(go t76_1 (freeze (f76 3 a true))) +(go t76_2 (freeze (f76 1 [[1 2] [3]] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t76_3 (freeze (f76 1 a [a | b]))) +(go t76_4 (freeze (f76 1 [[1 [2 [3 [4]]]] 5] [a | b]))) +(define f77 N [H | T] Y -> (tl H) + N X Y -> (let V1 two (V1 (if (= (if (cons? X) (trap-error (tl (tl (hd X))) (/. E (cn "E:" (error-to-string E)))) (trap-error Y (/. E (cn "E:" (error-to-string E))))) (and (cons? (tl (hd (hd X)))) (number? 1))) (if (> N 0) (f77 (- N 1) (or (= (tl N) (tl (hd (tl N)))) (= (hd (tl N)) N)) (cond ((cons? (hd (hd (hd X)))) (if (> N 0) (f77 (- N 1) [[[1 2] [3 4]] [[5] 6] 7 8] (tl (hd (hd N)))) [])) (true (if (cons? X) N (hd (tl (tl N))))))) (trap-error (if false (tl Y) [[[1 2] [3 4]] [[5] 6] 7 8]) (/. E (cn "E:" (error-to-string E))))) (if (> N 0) (f77 (- N 1) (let V2 (two a (tl (tl X))) (trap-error (tl (tl (hd N))) (/. E (cn "E:" (error-to-string E))))) (/. Q (let V3 (if (> N 0) (cons [[1 [2 [3 [4]]]] 5] (f77 (- N 1) Y Y)) (hd (tl (tl X)))) (tr 1 (hd V3))))) (if (if (cons? N) (cons? N) (cons? (tl (hd (hd X))))) (tl Y) X))) (if (> N 0) (f77 (- N 1) (if (> (pl [(hd (hd X)) | Y]) 0) (if (> N 0) (f77 (- N 1) (cond (false [[1 [2 [3 [4]]]] 5]) (false (tl (hd (hd X)))) ((cons? (hd (tl (tl Y)))) X)) (/. Q Y)) (tl (hd (tl N)))) (if (> N 0) (f77 (- N 1) (trap-error Y (/. E (cn "E:" (error-to-string E)))) (/. Q (if (cons? X) a (tl (hd (hd Y)))))) (let V4 (/. V5 V6 V7 V8 V8) (V4 Y N X N (hd (tl (hd X))))))) (tr 7 (let V9 three (V9 (do (tr 5 0) (hd X)) (thaw (freeze Y)))))) ((/. V14 V15 V16 (let V17 (if (cons? V16) (tl (hd (hd X))) (hd (tl (tl V16)))) (if (> N 0) (f77 (- N 1) [] V17) (hd V16)))) (if (> N 0) (cons (tr 0 Y) (f77 (- N 1) (cond (true X) ((cons? Y) (hd N)) (true N)) ((/. V10 V11 V12 X V13 (tl (hd (hd N)))) (tl Y) Y))) N) (do (tr 5 0) (tl (tl X)))))))) +(go t77_0 (freeze (f77 1 false [[1] 2]))) +(go t77_1 (freeze (f77 2 [1 2] a))) +(go t77_2 (freeze (f77 2 [[1 [2 [3 [4]]]] 5] a))) +(go t77_3 (freeze (f77 3 false [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t77_4 (freeze (f77 2 [[[1 2] [3 4]] [[5] 6] 7 8] 0))) +(define f78 N [H | T] Y -> (two (and false (cons? (hd (tl Y)))) (let V1 (two 1) (V1 Y (tl N)))) + N X Y -> (let Y (if (cons? (tl N)) N (hd (hd (hd X)))) [N | N])) +(go t78_0 (freeze (f78 3 a [a | b]))) +(go t78_1 (freeze (f78 1 [a | b] a))) +(go t78_2 (freeze (f78 1 "s" a))) +(go t78_3 (freeze (f78 1 [[[1 2] [3 4]] [[5] 6] 7 8] false))) +(go t78_4 (freeze (f78 2 1 true))) +(go t79_0 (freeze (let X [[[1]] | c] (let Y 0 (and (cons? Y) (not (or (cons? X) (cons? (hd (hd X)))))))))) +(go t79_1 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y a (do (tr 6 0) Y))))) +(go t79_2 (freeze (let X 1 (let Y [[[1 2] [3 4]] [[5] 6] 7 8] [(and (and (cons? (hd (hd (tl X)))) false) (cons? X)) | (let V1 undefinedf (V1 (two (hd (tl (hd X))) Y)))])))) +(go t79_3 (freeze (let X 0 (let Y 2.5 X)))) +(go t79_4 (freeze (let X 2.5 (let Y false (two (if (cons? Y) X (let V2 (/. V3 (tl Y)) (V2 (tl Y) Y))) X))))) +(define f80 N X Y -> (two ((/. V9 N V10 V11 V12 (tl (hd V12))) (if (or (cons? (tl (tl Y))) (cons? X)) (if (> N 0) (cons (if (> N 0) (f80 (- N 1) X (/. Q Y)) (hd (tl Y))) (f80 (- N 1) (trap-error (hd (hd Y)) (/. E (cn "E:" (error-to-string E)))) (cond (true (tl X)) (false X) (true a)))) (if (> N 0) (cons Y (f80 (- N 1) "s" (tl (hd (tl Y))))) (tl Y))) (hd (tl (tl Y)))) (or (cons? N) (= [N | X] (let X (tl (hd (tl N))) (hd (hd (hd X)))))) (do (tr 9 0) (if (> N 0) (cons (tr 5 X) (f80 (- N 1) (let V1 (/. V2 X X) (V1 (tl (hd N)))) (two 1 Y))) (let V3 (/. V4 V5 V6 [[[1 2] [3 4]] [[5] 6] 7 8]) (V3 N (hd (hd X)) (tl Y) (hd X))))) (if (> N 0) (f80 (- N 1) (if (> N 0) (f80 (- N 1) (two (hd N) Y) (and (cons? N) (cons? (tl Y)))) (if (> N 0) (f80 (- N 1) (hd (hd (tl X))) "s") (hd (tl (hd Y))))) (if (> N 0) (f80 (- N 1) ((/. V7 Y) (tl (tl (hd Y)))) (two N (tl Y))) (tl X))) (tl Y)) (if (or (number? (hd (tl Y))) (number? (hd (hd (tl X))))) (do (tr 6 0) (let V8 one (V8 N (hd (hd Y)) a))) (or (and true (cons? (tl (hd (hd N))))) (cons? (tl (hd X)))))) (trap-error (if (> N 0) (f80 (- N 1) (thaw (freeze (hd (hd (hd Y))))) (thaw (freeze (tl (hd (tl N)))))) (let V13 (/. V14 V15 V16 V17 (if false V17 1)) (V13 (or true true)))) (/. E (cn "E:" (error-to-string E)))))) +(go t80_0 (freeze (f80 3 true [1 2]))) +(go t80_1 (freeze (f80 0 a "s"))) +(go t80_2 (freeze (f80 0 [[1] 2] 1))) +(go t80_3 (freeze (f80 3 [[[1 2] [3 4]] [[5] 6] 7 8] a))) +(go t80_4 (freeze (f80 0 [[1 2] [3]] false))) +(define f81 N [[A | B] C] Y -> (trap-error (if false N (hd (tl N))) (/. E (cn "E:" (error-to-string E)))) + N X [A | B] -> (tr 8 (cond (true (hd (tl A))) (false (hd N)) (false (tl (tl B))) (true 1))) + N X Y -> (let V1 (/. V2 V3 (tl (tl N))) (V1 (if (cons? (hd (hd (hd X)))) X Y) (if (> N 0) (cons (hd Y) (f81 (- N 1) N X)) 1) (if false [[1 [2 [3 [4]]]] 5] [])))) +(go t81_0 (freeze (f81 0 [[[1 2] [3 4]] [[5] 6] 7 8] 1))) +(go t81_1 (freeze (f81 1 2.5 [[1 2] [3]]))) +(go t81_2 (freeze (f81 1 [[1 2] [3]] 0))) +(go t81_3 (freeze (f81 3 [] a))) +(go t81_4 (freeze (f81 0 [[[1 2] [3 4]] [[5] 6] 7 8] []))) +(define f82 N [[A | B] C] Y -> (if (and (cons? (hd B)) true) (or false true) (if false 2.5 Y)) + N X [A | B] -> (let V1 (/. V2 V3 V4 (if false N V3)) (V1 (and (cons? B) false) (tr 8 "s") (cond ((cons? B) (hd (hd X))) (true B)) (if (> N 0) (cons A (f82 (- N 1) X [[[1 2] [3 4]] [[5] 6] 7 8])) B))) + N X Y -> (and (number? (hd Y)) (and true (cons? (hd N))))) +(go t82_0 (freeze (f82 1 0 0))) +(go t82_1 (freeze (f82 1 [1 2] true))) +(go t82_2 (freeze (f82 1 true false))) +(go t82_3 (freeze (f82 2 false a))) +(go t82_4 (freeze (f82 2 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f83 N X Y -> (thaw (freeze (tr 7 N)))) +(go t83_0 (freeze (f83 3 [1 2] [1]))) +(go t83_1 (freeze (f83 3 true "s"))) +(go t83_2 (freeze (f83 1 [[1] 2] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t83_3 (freeze (f83 1 [a | b] [[1] 2]))) +(go t83_4 (freeze (f83 3 1 [1]))) +(go t84_0 (freeze (let X 0 (let Y [[1 [2 [3 [4]]]] 5] (if (let V1 (let V2 (/. V3 (if true [[[1 2] [3 4]] [[5] 6] 7 8] (tl X))) (V2 (cond (false (tl (hd (tl X))))) (and false (cons? (hd (tl (tl Y))))))) (= V1 (hd (tl (hd X))))) (tr 3 (if (if false false true) (cond (false Y) (true Y)) (or false false))) (cond ((number? (or (cons? X) false)) (trap-error (or false false) (/. E (cn "E:" (error-to-string E))))) (true (thaw (freeze (if false (tl X) X)))))))))) +(go t84_1 (freeze (let X [[1 2] [3]] (let Y [a | b] (if (number? X) ((/. V11 Y V12 V13 (tr 2 Y)) (do (tr 2 0) [0 | (hd (hd (tl X)))]) (let V4 (/. V5 Y V6 V7 (trap-error [[1 [2 [3 [4]]]] 5] (/. E (cn "E:" (error-to-string E))))) (V4 Y ((/. V8 V9 a) (tl (tl (tl Y))) (tl Y) (tl (hd (tl X)))) (let V10 Y V10) (and false (cons? (tl Y))))) (do (tr 8 0) (hd (tl X))) 1) (do (tr 3 0) (let V14 one (V14 Y)))))))) +(go t84_2 (freeze (let X [1] (let Y [a | b] (let V15 (tr 7 (two X (do (tr 7 0) [[1 [2 [3 [4]]]] 5]))) (cond ((> (pl (and true false)) 1) (let V16 (tl (tl (hd Y))) (tl X))) ((cons? (tl X)) V15) (true (or (if false false true) (and (cons? X) false))))))))) +(go t84_3 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (let V17 7 (V17 [[1 [2 [3 [4]]]] 5] (two [(do (tr 4 0) Y) | (do (tr 7 0) (tl (tl Y)))] (if (not (cons? (hd (hd X)))) (if false Y Y) (hd (hd (hd X))))))))))) +(go t84_4 (freeze (let X [[1] 2] (let Y true (if (cons? (hd X)) (cond ((> (pl ((/. V18 Y (hd X)))) 1) (thaw (freeze (cond (true Y) (true Y) (true Y))))) ((cons? (hd (tl Y))) "s") ((or (cons? (tl Y)) (if false false true)) (do (tr 4 0) (if (cons? (hd (hd (hd Y)))) X (hd Y)))) (true (if (or false (cons? (tl (tl Y)))) ((/. V19 V20 V21 Y)) (trap-error X (/. E (cn "E:" (error-to-string E))))))) (thaw (freeze (and (and (cons? Y) false) (not true))))))))) +(define f85 N X Y -> (let V1 (/. X V2 V3 V4 (let V5 (two 1) (V5 [((/. V6 V7 V8 V9 V10 (let V11 three (V11 "s" (tl (hd (hd N))) Y)))) | (let V12 (hd (hd V3)) V3)] (do (tr 9 0) (hd (hd (hd V3))))))) (V1 (if (let V13 Y (> (pl (let V14 curry2 (V14 []))) 1)) (if (number? (let V15 (/. N (hd (hd (tl X)))) (V15 (tl (hd Y))))) ((/. V18 V19 V20 Y ((/. V21 (tl (hd (tl X)))) V20 Y)) ((/. V16 V17 N) N Y)) (let V22 (if false (tl (tl (hd N))) 1) (let V22 a Y))) (trap-error (trap-error (cond (true a) (true Y) (true (tl (tl (hd X))))) (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E))))) (hd Y)))) +(go t85_0 (freeze (f85 0 [[1 2] [3]] 2.5))) +(go t85_1 (freeze (f85 1 [1] a))) +(go t85_2 (freeze (f85 1 1 [1]))) +(go t85_3 (freeze (f85 2 1 [[[1]] | c]))) +(go t85_4 (freeze (f85 3 [[1] 2] true))) +(go t86_0 (freeze (let X 2.5 (let Y false (let V1 (/. Y V2 V3 (and (let V2 X (cons? (tl X))) (let X Y true))) (V1 (let V4 (if false (tl (tl X)) X) (or (cons? (tl (tl (tl V4)))) (cons? (hd (hd V4))))))))))) +(go t86_1 (freeze (let X "s" (let Y [[[1 2] [3 4]] [[5] 6] 7 8] (trap-error (two a (tl X)) (/. E (cn "E:" (error-to-string E)))))))) +(go t86_2 (freeze (let X true (let Y 0 (tr 9 (if (not false) X (if false (tl (hd (hd Y))) 2.5))))))) +(go t86_3 (freeze (let X [[[1]] | c] (let Y false (trap-error (thaw (freeze (tl X))) (/. E (cn "E:" (error-to-string E)))))))) +(go t86_4 (freeze (let X [1 2] (let Y [a | b] (let V5 two (V5 (let X (let V6 (/. V7 V8 V8) (V6 (hd (hd X)) Y 2.5)) (tl (hd Y))) (hd (hd Y)))))))) +(define f87 N [H | T] Y -> (let V1 (fn two) (V1 [(thaw (freeze H)) | (tl (hd T))])) + N X Y -> (let V1 three (V1 (cond ((> (pl (hd (tl (hd Y)))) 1) (if false (tl X) 1)) (true (and true false))) (let V2 (/. V3 V4 V5 V6 (if false (hd (hd X)) N)) (V2 (hd (hd X))))))) +(go t87_0 (freeze (f87 2 [[[1]] | c] 1))) +(go t87_1 (freeze (f87 2 0 []))) +(go t87_2 (freeze (f87 2 true a))) +(go t87_3 (freeze (f87 0 "s" "s"))) +(go t87_4 (freeze (f87 1 "s" []))) +(define f88 N [[A | B] C] Y -> (or (cons? (tl (tl (tl N)))) (cons? B)) + N X [A | B] -> (or (and true false) (number? X)) + N X Y -> (thaw (freeze (thaw (freeze 0))))) +(go t88_0 (freeze (f88 0 "s" [[1 2] [3]]))) +(go t88_1 (freeze (f88 0 2.5 "s"))) +(go t88_2 (freeze (f88 3 [[[1]] | c] [a | b]))) +(go t88_3 (freeze (f88 3 [[1 [2 [3 [4]]]] 5] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t88_4 (freeze (f88 1 [] true))) +(define f89 N [[A | B] C] Y -> (let V1 (hd (tl (tl A))) (and (cons? (hd (hd V1))) (cons? (tl (hd (tl V1)))))) + N X [A | B] -> (if (cons? (hd (tl (hd B)))) (two (tr 3 [[1 [2 [3 [4]]]] 5]) (if (> N 0) (f89 (- N 1) N (/. Q (hd (hd N)))) (hd (tl (hd A))))) (trap-error (cond (false (tl A)) ((cons? X) [[1 [2 [3 [4]]]] 5]) (false A) (true (tl (tl N)))) (/. E (cn "E:" (error-to-string E))))) + N X Y -> (hd N)) +(go t89_0 (freeze (f89 1 1 true))) +(go t89_1 (freeze (f89 0 [[[1]] | c] 1))) +(go t89_2 (freeze (f89 3 [[[1 2] [3 4]] [[5] 6] 7 8] [1 2]))) +(go t89_3 (freeze (f89 1 [] [1]))) +(go t89_4 (freeze (f89 3 [a | b] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f90 N [H | T] Y -> ((/. V7 H (if (> N 0) (cons (let V8 (do (tr 3 0) ((/. V9 V10 V10))) (tl (tl (hd V7)))) (f90 (- N 1) (if (> N 0) (f90 (- N 1) ((/. V11 (and false false))) (and (> (pl Y) 1) (= [[1 [2 [3 [4]]]] 5] T))) (thaw (freeze (thaw (freeze H))))) (hd (hd V7)))) (trap-error (trap-error (trap-error Y (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E)))) (/. E (cn "E:" (error-to-string E)))))) (let V1 (two 1) (V1 (if (> N 0) (f90 (- N 1) (tr 8 (hd (tl (hd T)))) (/. Q (let H (if (> N 0) (f90 (- N 1) "s" (tl (tl T))) (tl H)) (let V2 (/. V3 V4 V5 (hd (tl V3))) (V2 a H))))) (thaw (freeze (hd (hd (tl T)))))) (if (if (not true) (or false true) (if false false (cons? (tl (hd H))))) (if (> N 0) (f90 (- N 1) (thaw (freeze (hd (tl (hd N))))) (/. Q (if true [] T))) (let V6 (hd H) V6)) (hd N))))) + N X Y -> (and (cons? (hd N)) (and (cons? (hd (tl (hd Y)))) (or (= (and (cons? (hd (tl (hd Y)))) false) (let V1 (/. V2 V3 V4 X (tl Y)) (V1 (hd (tl Y)) a N X))) (or (or false (cons? (tl Y))) (let V5 N false)))))) +(go t90_0 (freeze (f90 3 [a | b] [[1] 2]))) +(go t90_1 (freeze (f90 3 1 [[1 2] [3]]))) +(go t90_2 (freeze (f90 2 [[[1 2] [3 4]] [[5] 6] 7 8] "s"))) +(go t90_3 (freeze (f90 1 1 [1 2]))) +(go t90_4 (freeze (f90 1 [[1 [2 [3 [4]]]] 5] true))) +(define f91 N [[A | B] C] Y -> (if (> N 0) (cons (do (tr 8 0) (cond ((or false false) (if (> N 0) (cons (hd (tl (hd B))) (f91 (- N 1) [[1 [2 [3 [4]]]] 5] (hd (hd N)))) a)))) (f91 (- N 1) (two (if (number? (tl (hd (hd Y)))) (do (tr 0 0) 1) (if (> N 0) (cons 1 (f91 (- N 1) (hd (hd (hd C))) (hd (hd (tl B))))) (tl A))) (let V1 (three 1 2) (V1 (if (> N 0) (f91 (- N 1) (tl (hd (hd B))) (/. Q (hd (tl B)))) a)))) (if (> N 0) (f91 (- N 1) (trap-error ((/. V2 V3 V4 V5 (hd V3)) (tl A) N C) (/. E (cn "E:" (error-to-string E)))) (/. Q (if (and false true) (thaw (freeze Y)) (if (cons? (hd (tl (hd Y)))) 1 (tl (hd (tl B))))))) (thaw (freeze ((/. A A A V6 V7 B) (hd B) (hd A))))))) (let V8 (two 1) (V8 (if (> N 0) (f91 (- N 1) (two 1 1) (and false true)) (two 0 0)) (if (and (cons? (hd (tl A))) false) (cond (false "s") (false (hd (hd (hd B)))) (false [[[1 2] [3 4]] [[5] 6] 7 8]) (true B)) (if (cons? (tl Y)) [[[1 2] [3 4]] [[5] 6] 7 8] 1)) (if (not false) Y (do (tr 5 0) B)) (do (tr 9 0) (trap-error (tl (tl C)) (/. E (cn "E:" (error-to-string E)))))))) + N X [A | B] -> (two N (if (number? (trap-error (hd (tl X)) (/. E (cn "E:" (error-to-string E))))) (do (tr 1 0) (if (> N 0) (f91 (- N 1) N (/. Q (hd (hd A)))) (hd (tl (hd N))))) (if (not true) (two 1 N) (do (tr 7 0) N)))) + N X Y -> (thaw (freeze (cond ((not (cons? (hd (hd (tl N))))) (thaw (freeze (if (> N 0) (f91 (- N 1) 1 Y) Y)))) (true (let V1 (/. Y V2 (if true (hd (tl (hd N))) (hd (hd (tl N))))) (V1 (hd (hd (hd N)))))))))) +(go t91_0 (freeze (f91 1 [[1] 2] [a | b]))) +(go t91_1 (freeze (f91 3 "s" "s"))) +(go t91_2 (freeze (f91 0 [1 2] [[1 [2 [3 [4]]]] 5]))) +(go t91_3 (freeze (f91 3 true [[1 [2 [3 [4]]]] 5]))) +(go t91_4 (freeze (f91 1 0 [1]))) +(define f92 N [H | T] Y -> (if (> N 0) (f92 (- N 1) (tr 3 N) (thaw (freeze (tl (hd Y))))) (and (cons? (hd (tl Y))) (cons? (tl T)))) + N X Y -> (two (do (tr 2 0) (hd (hd Y))) (two N Y))) +(go t92_0 (freeze (f92 0 [a | b] 2.5))) +(go t92_1 (freeze (f92 3 [] [[1 2] [3]]))) +(go t92_2 (freeze (f92 1 1 [[[1]] | c]))) +(go t92_3 (freeze (f92 0 [[[1]] | c] [a | b]))) +(go t92_4 (freeze (f92 3 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(define f93 N X Y -> (or (and (or false false) (cons? (hd (tl N)))) (or (cons? (hd (tl X))) (and false false)))) +(go t93_0 (freeze (f93 2 2.5 false))) +(go t93_1 (freeze (f93 1 [1 2] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t93_2 (freeze (f93 3 [[[1]] | c] 0))) +(go t93_3 (freeze (f93 0 0 0))) +(go t93_4 (freeze (f93 0 0 "s"))) +(define f94 N X Y -> (cond ((cons? (tl (tl (tl X)))) (tl (hd (tl X)))))) +(go t94_0 (freeze (f94 1 false [1]))) +(go t94_1 (freeze (f94 3 [[1 2] [3]] "s"))) +(go t94_2 (freeze (f94 0 true [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t94_3 (freeze (f94 0 [1 2] [[1 [2 [3 [4]]]] 5]))) +(go t94_4 (freeze (f94 1 false [[1 [2 [3 [4]]]] 5]))) +(define f95 N [[A | B] C] Y -> (two (and (cons? (hd N)) (and true false)) (if (= N (hd C)) A (thaw (freeze N)))) + N X [A | B] -> X + N X Y -> (tr 6 (hd (tl (tl X))))) +(go t95_0 (freeze (f95 3 [[[1]] | c] []))) +(go t95_1 (freeze (f95 0 [[[1]] | c] [[1 2] [3]]))) +(go t95_2 (freeze (f95 1 0 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t95_3 (freeze (f95 1 0 a))) +(go t95_4 (freeze (f95 3 true [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t96_0 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y [1] (let V1 (/. V2 [(if (cons? (tl (tl (tl V2)))) [1 | V2] (and true (cons? Y))) | V2]) (V1 0)))))) +(go t96_1 (freeze (let X [] (let Y [a | b] (let V3 (fn two) (V3 (do (tr 8 0) (let V4 (two 1) (V4 (let V5 (hd X) [[1 [2 [3 [4]]]] 5]) (cond ((cons? (tl (hd (hd Y)))) Y) ((cons? (hd Y)) Y) (true (tl (hd (hd Y))))) 0 (hd Y)))) (cond ((or (= Y (hd (hd (hd X)))) (> (pl 2.5) 0)) (tl (hd (tl X)))) ((if (and false (cons? (tl X))) (cons? Y) (> (pl 0) 0)) (or (cons? (tl Y)) (cons? (hd X)))) ((or (not false) (> (pl 1) 1)) (let X X ((/. V6 V7 Y V8 V9 (tl (hd (tl V9)))) 1 Y X Y))) (true (two (thaw (freeze X)) X))) (let V10 (and (and true false) (let V11 Y false)) [(hd (tl V10)) | V10]))))))) +(go t96_2 (freeze (let X 1 (let Y [[[1]] | c] (if (cons? (tl (tl Y))) (if (let V12 (tr 9 Y) (let V13 V12 false)) [(or (cons? (hd (tl (hd X)))) (cons? Y)) | (let V14 two (V14 [[[1 2] [3 4]] [[5] 6] 7 8]))] (if (or (cons? (tl (hd Y))) false) (and false (cons? (hd (tl (hd Y))))) [X | X])) ((/. V15 V16 V17 V18 V19 [(or true (cons? (tl (tl V16)))) | (thaw (freeze (tl (tl (hd V16)))))]))))))) +(go t96_3 (freeze (let X [[1 [2 [3 [4]]]] 5] (let Y [1 2] (tl (hd X)))))) +(go t96_4 (freeze (let X [[[1 2] [3 4]] [[5] 6] 7 8] (let Y 1 [[[1 2] [3 4]] [[5] 6] 7 8])))) +(define f97 N X Y -> (let V1 (/. V2 V3 V4 V5 (hd (hd V2))) (V1 (do (tr 3 0) N) (if (cons? Y) (tl (hd (tl Y))) (hd (hd N))) (if true (hd (hd (hd X))) (hd (hd N))) (do (tr 2 0) 1)))) +(go t97_0 (freeze (f97 2 [[1 [2 [3 [4]]]] 5] [[1 2] [3]]))) +(go t97_1 (freeze (f97 1 2.5 0))) +(go t97_2 (freeze (f97 0 1 [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t97_3 (freeze (f97 3 [[[1 2] [3 4]] [[5] 6] 7 8] [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t97_4 (freeze (f97 1 [a | b] [[1 [2 [3 [4]]]] 5]))) +(define f98 N [[A | B] C] Y -> (if (and (cons? (tl (tl A))) (let V1 (if (> N 0) (f98 (- N 1) (tl (hd N)) (if (cons? (hd N)) (tl N) (hd N))) ((/. Y V2 N V3 V4 B) B 1 B)) (or (if (cons? (hd B)) true (cons? (hd (hd N)))) (not (cons? (hd N)))))) (if (or (cons? (tl (hd (hd N)))) (let C (tr 4 (hd B)) (= 0 1))) (if (> N 0) (f98 (- N 1) (or (not true) (= N B)) (tl C)) (thaw (freeze (if true A C)))) ((/. V8 V9 V10 V11 A) ((/. V5 ((/. A V6 (hd (tl Y))) C)) (two A (tl (hd (tl B)))) (tr 2 [[[1 2] [3 4]] [[5] 6] 7 8])) (hd (hd (tl N))) (let V7 (if (cons? (tl (hd (tl A)))) (tl A) (tl C)) (if false 1 (hd N))))) (if (> N 0) (f98 (- N 1) (trap-error (thaw (freeze (if (> N 0) (f98 (- N 1) [] (/. Q (hd N))) (tl (hd B))))) (/. E (cn "E:" (error-to-string E)))) (if (> N 0) (f98 (- N 1) [(two N [[[1 2] [3 4]] [[5] 6] 7 8]) | (if (cons? (tl (hd (hd B)))) a (tl B))] Y) (tl (tl (tl C))))) (let V12 (/. V13 [(tl B) | ((/. V14 (hd (hd V13))) (hd (hd Y)) (hd V13))]) (V12 (hd Y) (or (cons? (hd C)) (cons? A)))))) + N X [A | B] -> (thaw (freeze (or (= (tl (hd (hd B))) ((/. V1 V1))) (if (and (if true false false) (not (cons? (tl (tl (tl A)))))) (let V2 (if true N (hd (tl (hd N)))) (number? N)) (> (pl (if true (tl (tl A)) B)) 0))))) + N X Y -> (let V1 (/. Y Y V2 (and (cons? Y) (let V3 ((/. V4 (let V5 (/. V2 V6 1) (V5 "s" a))) (trap-error V2 (/. E (cn "E:" (error-to-string E))))) (number? ((/. V7 V8 V9 V10 V9) (hd (tl (hd X))) (hd (hd Y))))))) (V1 ((/. V21 N (if (> N 0) (f98 (- N 1) (if (> N 0) (f98 (- N 1) (two V21 a) (/. Q (if true Y (tl V21)))) ((/. X V22 V23 X V24 []))) (/. Q (if (> N 0) (f98 (- N 1) (if (> N 0) (f98 (- N 1) (hd (tl (hd N))) Y) (hd (tl (hd X)))) (hd N)) (or false false)))) (if (> N 0) (f98 (- N 1) (if true Y (tl (tl V21))) (do (tr 0 0) (hd N))) (if (> N 0) (f98 (- N 1) (tl (tl (hd N))) (/. Q (tl (hd (hd Y))))) V21)))) (trap-error (tl (tl (tl X))) (/. E (cn "E:" (error-to-string E)))) (let V11 (/. V12 V13 V14 V15 (if (> N 0) (f98 (- N 1) (let V16 undefinedf (V16 (tl (hd Y)) (tl X) V12)) ((/. V17 V13))) (if (cons? (hd V12)) Y 0))) (V11 (if (cons? (hd Y)) (or true (cons? Y)) (let V18 (fn two) (V18 [[[1 2] [3 4]] [[5] 6] 7 8]))) (if (> (pl [[[1 2] [3 4]] [[5] 6] 7 8]) 0) (let V19 2.5 (hd (hd X))) (tl (hd N))) (thaw (freeze (let V20 [[[1 2] [3 4]] [[5] 6] 7 8] 1)))))) (if (> N 0) (cons (do (tr 0 0) (trap-error (hd (hd X)) (/. E (cn "E:" (error-to-string E))))) (f98 (- N 1) (tl Y) (if (if (cons? (tl X)) (not (cons? (hd (hd (hd X))))) (or true false)) (let V25 (/. V26 V27 V28 (do (tr 8 0) (hd (hd Y)))) (V25 X [Y | (tl X)] (let V29 (/. V30 V31 V32 (tl V31)) (V29 N)))) (tr 1 (if (> N 0) (f98 (- N 1) (tl (tl Y)) (/. Q Y)) X))))) X)))) +(go t98_0 (freeze (f98 0 true [[1 2] [3]]))) +(go t98_1 (freeze (f98 2 [] 1))) +(go t98_2 (freeze (f98 0 a [[[1 2] [3 4]] [[5] 6] 7 8]))) +(go t98_3 (freeze (f98 3 [] true))) +(go t98_4 (freeze (f98 2 true false))) +(define f99 N X Y -> (if (> N 0) (cons (two ((/. V5 V6 (trap-error (if (> N 0) (f99 (- N 1) (hd (hd (hd X))) (hd (tl X))) (tl (hd V5))) (/. E (cn "E:" (error-to-string E))))) (if (cons? (tl (tl (hd Y)))) (if (> N 0) (f99 (- N 1) N (/. Q (hd (hd (hd Y))))) X) (tr 8 (hd (hd X)))) (let V1 (let V2 (hd (hd X)) (tl N)) (let V3 one (V3 X [[1 [2 [3 [4]]]] 5] (hd X)))) (tr 4 (let V4 curry2 (V4 (tl (tl N)))))) (let X (let V7 (/. V8 V9 (let V10 (/. V11 X V12 Y V11) (V10 N (tl (hd X)) (hd X)))) (V7 (let V13 two (V13 Y N X (hd (hd Y)))) (tr 0 (tl N)) (cond (true (tl Y)) (true (tl (tl (tl X)))) (true N) (true a)))) (cond ((= (hd (hd N)) 2.5) (if (> N 0) (cons [] (f99 (- N 1) (hd (hd (tl Y))) (tl N))) Y)) ((not (cons? (tl Y))) (let X N [])) ((let V14 N false) (if false N X)) (true (and false (cons? (tl (hd Y)))))))) (f99 (- N 1) ((/. V15 V16 X V17 V18 [(hd (hd N)) | (if (> N 0) (f99 (- N 1) (if false 1 (hd (tl N))) (tr 4 "s")) (if (> N 0) (cons V18 (f99 (- N 1) (hd X) (tl (tl V17)))) Y))])) (and (and (number? (trap-error [[[1 2] [3 4]] [[5] 6] 7 8] (/. E (cn "E:" (error-to-string E))))) (cons? (hd (tl N)))) (and (not (let X (tl (hd (hd N))) (cons? (tl (hd (hd X)))))) (not (if (cons? N) (cons? (tl X)) true)))))) ((/. N (cond ((if (cons? (tl Y)) (not true) (cons? X)) ((/. V26 (let V27 (/. V28 (tl V26)) (V27 Y))) ((/. V25 1)) (thaw (freeze N)))))) (tl (tl N)) [(if (number? N) (let V19 (/. V20 V21 N a) (V19 X)) (if (> N 0) (f99 (- N 1) Y Y) (hd (hd (tl Y))))) | (if (> N 0) (f99 (- N 1) (let V22 two (V22 2.5)) (if true X (tl (tl N)))) (let V23 (/. V24 N) (V23 X)))]))) +(go t99_0 (freeze (f99 0 [[[1 2] [3 4]] [[5] 6] 7 8] [1 2]))) +(go t99_1 (freeze (f99 2 a a))) +(go t99_2 (freeze (f99 2 1 true))) +(go t99_3 (freeze (f99 3 1 "s"))) +(go t99_4 (freeze (f99 2 false [1]))) diff --git a/test/gc_pause_spec.lua b/test/gc_pause_spec.lua new file mode 100644 index 0000000..28ffca8 --- /dev/null +++ b/test/gc_pause_spec.lua @@ -0,0 +1,46 @@ +-- gc_pause_spec.lua : boot's adaptive GC pause follows the live heap, and leaves +-- alone a pause the host set. Boot's GC block runs when boot.lua is required. +package.path = (arg[0]:gsub("test/[^/]*$", "")) .. "?.lua;" .. package.path +local npass, nfail = 0, 0 +local function check(cond, name) + if cond then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name) end +end +local function pause() + local p = collectgarbage("setpause", 0) + collectgarbage("setpause", p) + return p +end +local function cycles(n) for _ = 1, n do collectgarbage("collect") end end + +if os.getenv("SHEN_GC") or not newproxy then + print("gc_pause_spec: skipped (SHEN_GC set, or no newproxy)") + os.exit(0) +end +require("boot") + +cycles(3) +check(pause() == 400, "a small live heap stays at 400, got " .. pause()) + +-- 128 MB or so live: the pause falls, and more than once +local hold = {} +for i = 1, 1000000 do hold[i] = { i, i } end +cycles(3) +local big = pause() +check(big < 400 and big >= 150, "a big live heap lowers the pause, got " .. big) +hold = nil +cycles(3) +check(pause() == 400, "the pause comes back when the heap shrinks, got " .. pause()) + +-- the host sets its own: it stays through cycles of any heap size +collectgarbage("setpause", 275) +cycles(3) +check(pause() == 275, "the host's pause stands, got " .. pause()) +hold = {} +for i = 1, 1000000 do hold[i] = { i, i } end +cycles(3) +check(pause() == 275, "the host's pause stands on a big heap, got " .. pause()) +hold = nil + +print(("gc_pause_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/primitives_spec.lua b/test/primitives_spec.lua index 45e7558..e157b26 100644 --- a/test/primitives_spec.lua +++ b/test/primitives_spec.lua @@ -128,6 +128,13 @@ checkeq("(symbol? hello)", "true") checkeq("(symbol? 1)", "false") checkeq("(variable? X)", "true") -- uppercase first char => variable checkeq("(variable? x)", "false") +checkeq("(variable? [X])", "false") -- a non-symbol is false, without throwing +checkeq("(variable? [])", "false") +checkeq("(variable? (vector 1))", "false") +checkeq("(variable? (lambda X X))", "false") +checkeq("(variable? 1)", "false") +checkeq("(variable? \"X\")", "false") +checkeq("(variable? true)", "false") checkeq("(cons? (cons 1 ()))", "true") checkeq("(cons? 1)", "false") checkeq("(absvector? (absvector 3))", "true") diff --git a/test/reader_native_spec.lua b/test/reader_native_spec.lua index bebfa65..8be2a7e 100644 --- a/test/reader_native_spec.lua +++ b/test/reader_native_spec.lua @@ -112,6 +112,17 @@ local EDGE = { "1.5e3 -2 +3 - +", "-1.5 1e-2 .5 5.", "<> [<>]", "true false", "Xy _ x_1", "[1 [2 (3 4] 5)]", "(a [b) c]", "[[[]]]", "((()))", "[] ()", "(define f X -> [X | X])", "(a b) junk ) (c)", "a\r\nb", "é \"ü\" ß", + -- the first-code fast paths: each must stop where the kernel's rules do + "->", "<-", "-", "+", ".", "-a", "+a", ".a", "-.", "+.", "--", "++", "-+", "..", "...", + "-5", "--5", "-+5", "+-.5", "-.5", "+.5", ".5", ".5.5", "-.a", "--a", "-+a", + "12abc", "12-3", "12+3", "1.", "1.a", "1e", "1e5", "1e-5", "1ex", "1E5", "e5", "E5", + "007", "0", "123456789012345", "1234567890123456", "12345678901234567890", "999999999999999", + "12\"s\"", "12(a)", "12]", "a12", "a.b", "a-1", "a+1", "a1.5", "<>a", "a<>", "<><>", "(<>)", + "\"c\"", "\"cat\"", "\"c#\"", "\"c#x\"", "\"c#65\"", "\"c#65;c\"", "\"cc#65;\"", "\"é\"", "\"a\nb\"", + "\"\\\"", "\"unterminated c", "\"c", "a\"b\"c", "\t\ra", "a\t", "\0", "a\0b", "\"\0\"", + "=", ":==", "a=b", "@p", "@", "~", "!", "$", "?", "_", "`", "%", "&", "'", "#", "*", "/", "<", ">", + "[-> . -]", "(- 1 2)", "(-1 2)", "[1|2]", "[a|-1]", "(a)(b)", "[a][b]", "a]b", "a)b", + "true", "false", "truex", "($ abc def)", "($)", "($ 12)", "($ \"s\")", "[($ abc)]", "(a ($ bc) d)", } for i, s in ipairs(EDGE) do same(s, "edge case " .. i .. " " .. string.format("%q", s)) end diff --git a/test/seg_pool_spec.lua b/test/seg_pool_spec.lua new file mode 100644 index 0000000..3bef866 --- /dev/null +++ b/test/seg_pool_spec.lua @@ -0,0 +1,56 @@ +-- seg_pool_spec.lua : what a stack segment's reuse must not change. Needs only +-- runtime.lua, so it also runs on PUC Lua: lua test/seg_pool_spec.lua +package.path = (arg[0]:gsub("test/[^/]*$", "")) .. "?.lua;" .. package.path +local R = require("runtime") +local run = R.seg.run +local npass, nfail = 0, 0 +local function check(cond, name) + if cond then npass = npass + 1 + else nfail = nfail + 1; print("FAIL " .. name) end +end +local pack = function(...) return { n = select("#", ...), ... } end + +-- a yield through a segment keeps its exact values, each way. PUC 5.1 cannot +-- yield across the pcall a segment runs in, before these branches or after. +if jit then + local function through(...) + local args = pack(...) + local co = coroutine.create(function() + return "done", run(function() + return coroutine.yield(unpack(args, 1, args.n)) + end) + end) + return pack(coroutine.resume(co)) + end + local r = through() + check(r.n == 1 and r[1] == true, "yield of nothing gives nothing, got " .. r.n) + r = through("a") + check(r.n == 2 and r[2] == "a", "yield of one value gives one, got " .. r.n) + r = through("a", "b") + check(r.n == 3 and r[3] == "b", "yield of two gives two, got " .. r.n) + r = through("a", nil) + check(r.n == 3 and r[2] == "a", "a trailing nil that was yielded stays, got " .. r.n) + r = through(1, 2, 3, 4, 5) + check(r.n == 6 and r[6] == 5, "yield of five gives five, got " .. r.n) + local co = coroutine.create(function() + return run(function() local a, b = coroutine.yield("y"); return a + b end) + end) + coroutine.resume(co) + local ok, v = coroutine.resume(co, 3, 4) + check(ok and v == 7, "a resume's values reach the yield") + check(run(function() return 5 end) == 5, "a job after the yields runs on the pool") +end + +-- a hook set on a segment is not seen by the next job. On LuaJIT hooks are global, +-- so only PUC Lua can tell. +if not jit then + run(function() debug.sethook(function() end, "", 100000); return 1 end) + check(debug.gethook() == nil, "the main thread has no hook after the job") + local hooked = run(function() return debug.gethook() ~= nil end) + check(not hooked, "the next job does not inherit a hook") + local again = run(function() return debug.gethook() ~= nil end) + check(not again, "nor the one after") +end + +print(("seg_pool_spec: %d pass, %d fail"):format(npass, nfail)) +os.exit(nfail == 0 and 0 or 1) diff --git a/test/stack_segments_spec.lua b/test/stack_segments_spec.lua index 3b7ddd7..76dc924 100644 --- a/test/stack_segments_spec.lua +++ b/test/stack_segments_spec.lua @@ -148,5 +148,43 @@ do check(ok and v == DEEP, "deep recursion works again after the cap") end +-- segments are reused once finished: arguments, errors and a host's yield +-- all behave as on a fresh coroutine, and a segment reused after each of them +-- starts clean +do + local run1 = R.seg.run + check(run1(function(a, b) return a + b end, 2, 3) == 5, "reuse: arguments and value") + check(select("#", run1(function() return 1, 2 end)) == 1, "reuse: one value, as before") + local obj = {} + for _ = 1, 3 do + local ok, e = pcall(run1, function() error(obj, 0) end) + check(not ok and e == obj, "reuse: error object unchanged") + check(run1(function() return 7 end) == 7, "reuse: runs after an error") + end + -- a host's yield passes through, in both directions, and repeatedly + for _ = 1, 3 do + local co = coroutine.wrap(function() + return run1(function(a) + local b = coroutine.yield(a + 1) + local c = coroutine.yield(b + 1) + return c * 2 + end, 1) + end) + check(co() == 2 and co(10) == 11 and co(21) == 42, "reuse: host yield through a segment") + end + -- a yield with no coroutine above it is an error, and a segment left + -- suspended by it is not reused + local ok = pcall(run1, function() coroutine.yield(1) end) + check(not ok, "reuse: yield outside any coroutine is an error") + check(run1(function() return 8 end) == 8, "reuse: runs after a stray yield") + -- a segment abandoned by its host never comes back + local co = coroutine.wrap(function() return run1(function() coroutine.yield(1); return 2 end) end) + check(co() == 1, "reuse: suspended by its host") + check(run1(function() return 9 end) == 9, "reuse: runs with another segment suspended") + check(co() == 2, "reuse: suspended segment resumes later") + local _, n = R.seg.stats() + check(n == 0 and R.seg.SD[1] == 0, "reuse: state restored") +end + io.write(string.format("stack_segments_spec: %d pass, %d fail\n", npass, nfail)) os.exit(nfail == 0 and 0 or 1) diff --git a/test/tailcall_spec.lua b/test/tailcall_spec.lua index 2544d57..26447be 100644 --- a/test/tailcall_spec.lua +++ b/test/tailcall_spec.lua @@ -169,6 +169,33 @@ do check(ev("(tc-redef 5)") == R.intern("new"), "redefinition observed by new calls") end +-- --------------------------------------------------------------------------- +-- proven conses: hd/tl dominated by (cons? E) read the cell directly +-- --------------------------------------------------------------------------- +do + local src = gensrc("(defun pc-a (L) (if (cons? L) (hd L) 0))") + check(not src:find("HD(", 1, true), "hd under (cons? V) needs no check") + src = gensrc("(defun pc-b (L) (cond ((and (cons? L) (cons? (tl L))) (hd (tl L))) (true 0)))") + check(src:find("[1]", 1, true) ~= nil, "(and ...) proves a chain for its result") + src = gensrc("(defun pc-c (L) (if (cons? L) 0 (hd L)))") + check(src:find("HD(", 1, true) ~= nil, "the else branch proves nothing") + src = gensrc("(defun pc-d (L M) (if (cons? L) (hd M) 0))") + check(src:find("HD(", 1, true) ~= nil, "another variable is not proven") + src = gensrc("(defun pc-e (L) (if (cons? L) (let L (tl L) (hd L)) 0))") + check(src:find("HD(", 1, true) ~= nil, "a shadowing let is not proven") + src = gensrc("(defun pc-f (L) (do (if (cons? L) 1 2) (hd L)))") + check(src:find("HD(", 1, true) ~= nil, "a proof ends with its branch") + + ev("(defun pc-g (L) (if (cons? L) (hd L) (trap-error (hd L) (lambda E wrong))))") + check(ev("(pc-g (cons 1 ()))") == 1, "proven hd reads the car") + check(ev("(pc-g 7)").name == "wrong", "hd of a non-cons still errors") + ev("(defun pc-h (L) (if (cons? L) (pc-h (tl L)) done))") + check(ev("(pc-h (cons 1 (cons 2 ())))").name == "done", "a self-tail loop re-tests its param") + ev("(defun pc-i (L) (if (cons? L) (hd (tl L)) 0))") + check(ev("(trap-error (pc-i (cons 1 2)) (lambda E caught))").name == "caught", + "only the tested chain is proven, not its tail") +end + -- --------------------------------------------------------------------------- -- tail recursion modulo cons (TRMC, #58): (cons H (self ...)) builders -- --------------------------------------------------------------------------- diff --git a/test/typecheck_lazy_spec.lua b/test/typecheck_lazy_spec.lua index 9ac959a..cb21f07 100644 --- a/test/typecheck_lazy_spec.lua +++ b/test/typecheck_lazy_spec.lua @@ -29,6 +29,14 @@ local function form(s) return P.F["read-from-string"](s)[1] end check("drivers deferred at boot (n_ok == 0)", tn.n_ok == 0) check("no failures recorded at boot (n_fail == 0)", tn.n_fail == 0) +-- The kernel signatures wait for the first typecheck too; what a program did to +-- a signature before then must still stand after. +local function sym(n) return R.intern(n) end +check("signatures deferred at boot", tn.NativeSig[sym("append")] == nil) +P.F["destroy"](sym("reverse")) +shen.eval("(declare lazy-sig-fn [number --> number])") +shen.eval("(declare hd [number --> number])") + -- 2) the exported native_typecheck is guarded: a direct call (no dispatch has -- run yet) must translate drivers first, not crash on nil NP predicates. local ok, res = pcall(tn.native_typecheck, form("1"), form("number")) @@ -37,6 +45,13 @@ check("direct native_typecheck returns a result", ok and res ~= false) check("direct call triggered driver translation (n_ok > 0)", tn.n_ok > 0) check("driver translation was clean (n_fail == 0)", tn.n_fail == 0) +check("kernel signatures are there after the first typecheck", tn.NativeSig[sym("append")] ~= nil) +check("complete signature harvest", tn.sigs_complete == true) +check("a signature destroyed before it stays destroyed", tn.NativeSig[sym("reverse")] == nil) +check("a signature declared before it stays", tn.NativeSig[sym("lazy-sig-fn")] ~= nil) +check("a kernel signature redeclared before it keeps the new type", + P.F["="](tn.NativeSig[sym("hd")], R.from_table({ sym("number"), sym("-->"), sym("number") }))) + -- 3) translated exactly once: a normal typecheck afterwards does not re-run it. local n = tn.n_ok check("a normal typecheck still succeeds", diff --git a/test/utf8_strings_spec.lua b/test/utf8_strings_spec.lua index 1e4c1ac..89bca37 100644 --- a/test/utf8_strings_spec.lua +++ b/test/utf8_strings_spec.lua @@ -126,7 +126,7 @@ checkeq('(cn (hdstr "Grüße") (tlstr "Grüße"))', '"Grüße"') checkeq('(shen.string->bytes "aü€")', "(97 252 8364)") checkeq('(shen.str->bytes "aü€")', "(97 252 8364)") checkeq('(@s "Gr" "ü" "ße")', '"Grüße"') -checkeq('(str "Grüße")', '"Grüße"') +checkeq('(str "Grüße")', '""Grüße""') -- @s pattern matching in a define walks characters (hdstr / tlstr) shen.eval([[(define u-count "" -> 0 (@s _ S) -> (+ 1 (u-count S)))]]) checkeq('(u-count "Zürich😀")', "7") diff --git a/typecheck_native.lua b/typecheck_native.lua index 994f345..488d179 100644 --- a/typecheck_native.lua +++ b/typecheck_native.lua @@ -583,8 +583,21 @@ function M.install(Pmod, Emod) -- then init.kl. An incomplete harvest (missing source -- or an unrecognised form) leaves sigs_complete false, which forces the -- legacy in-kernel typecheck path — slower, but identical verdicts. - M.sigs_complete = harvest_types_sigs(read_kl("types")) - or harvest_init_sigs(read_kl("init")) + -- Parsing those files is most of a boot's engine cost (two read_alls, 65M of + -- 480M instructions) and only a typecheck wants the result, so it waits for + -- the first one. A signature declared or destroyed before then must win. + local types_src, init_src = read_kl("types"), read_kl("init") + local sig_touched = {} + local function ensure_sigs() + if not types_src and not init_src then return end + local src1, src2 = types_src, init_src + types_src, init_src = nil, nil + local live = {} + for k, v in pairs(NativeSig) do live[k] = v end + M.sigs_complete = harvest_types_sigs(src1) or harvest_init_sigs(src2) + for k in pairs(sig_touched) do NativeSig[k] = live[k] end + for k, v in pairs(live) do NativeSig[k] = v end + end -- Driver TRANSLATION is DEFERRED to the first typecheck; the t-star.kl READ -- stays eager. Walking the 16 driver defuns through the KL->Lua prolog @@ -618,6 +631,7 @@ function M.install(Pmod, Emod) local native_problems = nil local function ensure_drivers() if drivers_ready then return end + ensure_sigs() -- Translate into FRESH accumulators and commit `drivers_ready` (plus the -- n_fail the dispatch consults) only AFTER the whole pass and the -- hand-ports succeed. If R.read_all or an unexpected translator error @@ -722,6 +736,7 @@ function M.install(Pmod, Emod) if orig_destroy then F["destroy"] = function(name) NativeSig[name] = nil + sig_touched[name] = true return orig_destroy(name) end P.FA[F["destroy"]] = 1