Fix System S rejecting kernel signatures: evaluate hoisted declare typeforms - #63
Merged
Merged
Conversation
The trailing (declare Name Typeform) block of types.kl is hoisted out of the kernel chunk by boot.lua's hoist_tail so its type-theory cost can be cached. Typeform is a KL *expression* — an rcons constructor tree like (cons number (cons --> (cons number ()))) — which the inline form would evaluate before `declare` sees it. The hoist passed the unevaluated AST straight to `declare`, so every hoisted signature closure in shen.*sigf* unified goal types against the raw (cons ...) s-expression and never matched a real type. The native typecheck engine masked this on the shen.typecheck path (it harvests signatures from types.kl itself), but anything consulting the legacy closures — prolog?-driven shen.system-S queries, the legacy typecheck path (SHEN_TYPECHECK_NATIVE=off / SHEN_PROLOG_ENGINE=legacy) — rejected every kernel signature, visibly the arithmetic and comparison primitives (issue #62). Fix: hoist_tail now evaluates the typeform with a small pure evaluator (cons trees, intern, self-evaluating atoms) and refuses the hoist for anything else, so non-literal declares would run inline with full KL semantics. CACHE_FORMAT is bumped to SHENKC4: the kernel cache key does not cover boot.lua, and pre-fix caches hold declare dumps compiled from the unevaluated typeforms. Regression spec test/system_s_sigf_spec.lua covers the four issue #62 judgments plus the sig closure itself; verdicts cross-checked against shen-go. Fixes #62 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #62
Root cause
boot.lua'shoist_tailpulls the trailing(declare Name Typeform)block oftypes.klout of the kernel chunk so the type-theory cost of the 161 kernel signatures can be cached. ButTypeformis a KL expression — an rcons constructor tree such as(cons number (cons --> (cons number ())))— which the inline form would evaluate beforedeclaresees it. The hoist passed the unevaluated AST straight todeclare, so every hoisted signature closure registered inshen.*sigf*unified goal types against the literal(cons ...)s-expression instead of the type(number --> (number --> number)), and never matched anything.The native typecheck engine masked the bug on the
shen.typecheckpath — it harvests signatures fromtypes.klindependently (typecheck_native.lua) — which is whytcmode looked fine. Anything consulting the real*sigf*closures was broken:prolog?-drivenshen.system-Squeries (the issue's repro): everyshen.lookupsig-based judgment failed, visibly+ - * / > < >= <=(consworked only because it has a dedicated System S rule);SHEN_TYPECHECK_NATIVE=off,SHEN_PROLOG_ENGINE=legacy):(shen.typecheck [* 2 3] A)returnedfalse.Fix
hoist_tailnow evaluates each declare's typeform with a small pure evaluator (cons trees,(intern "..."), self-evaluating atoms) before handing it todeclare, matching inline KL semantics exactly. A typeform it cannot prove literal refuses the hoist, so such a form would run inline with full KL semantics instead of being mis-cached.CACHE_FORMATbumpedSHENKC3->SHENKC4: the kernel cache key does not coverboot.lua, and pre-fix caches contain declare dumps compiled from the unevaluated typeforms; the bump invalidates them.Verification
true/true/true/true(wasfalse/false/false/true), matching shen-go and shen-cl; verdicts for partial application and negative judgments also cross-checked against shen-go.test/system_s_sigf_spec.lua(runs undermake test): the four issue judgments, partial application, negative cases, and a direct ground-unify of the+closure stored in*sigf*.make test: 899 pass, 0 fail across 20 specs.make certify(canonical ShenOSKernel suite): 100% pass.~/.cache/shen-lua-faslcleared,SHEN_FASL=off) and warm (kernel cache + fasl hit) — identical results.tests/oracle-arith.shenunder the fixedbin/shenprintsSHENLOGIC|ORACLE ARITH PASS.🤖 Generated with Claude Code