From 9edce46bec73d6f67b7e8ae9feb51f608ae455e2 Mon Sep 17 00:00:00 2001 From: Jon Ruttan Date: Wed, 4 Nov 2026 20:29:39 -0500 Subject: [PATCH] fix(env): a program's and a module's frame is a tree; pin x-lang v0.46.0 x-lang v0.46.0 (x-lang#1154, x-engine-c v0.2.26) keeps a tree behind an environment whose spine the language made with pair, as a program's and a module's are here, where it kept a list of cells; law 10 says so. The runtime walked those frames as lists, so a lookup ran off a node into `first: () has no first`, and python-module-new handed pair a list of cells the engine then read as a tree. %py-env-tree? tells the two apart as law 10 does (the root, or pair?), and %py-env-cell and %py-env-rows take the environment and walk either. A cell found in a tree is the live one, so an assignment through it still rebinds the name. A module the runtime writes is made empty and bound with def. Spec: tests/specs/C0-env-frames.spec.md reads and rebinds a program's and a module's frame; on v0.46.0 its three cases fail before this change and pass after. The other 127 spec files pass on v0.46.0 too. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 4 +- lang.xon | 2 +- python/runtime-flow.x | 107 ++++++++++++++++++++---------- tests/specs/C0-env-frames.spec.md | 49 ++++++++++++++ 4 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 tests/specs/C0-env-frames.spec.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a51e9b8..743734d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: # allowed to fail. Keep the pinned row equal to lang.xon's # (requires-release ...) VERBATIM -- they are one fact, and the check # below fails the build if they drift. - x-lang: ['v0.44.0', 'main'] + x-lang: ['v0.46.0', 'main'] # FOUR SHARDS, BECAUSE THE BOOT IS THE COST. Each spec file boots the # whole tower in its own process (tests/spec-runner.sh says why), and # measured on these runners that boot is ~26s of a ~38s file: a @@ -96,7 +96,7 @@ jobs: strategy: fail-fast: false matrix: - x-lang: ['v0.44.0', 'main'] + x-lang: ['v0.46.0', 'main'] steps: - uses: actions/checkout@v4 - name: Check out x-lang diff --git a/lang.xon b/lang.xon index 84f3c4d..c756a99 100644 --- a/lang.xon +++ b/lang.xon @@ -60,6 +60,6 @@ ; v0.35.0 is what this bundle and its siblings are green against, and the row ; tracks that rather than the floor. This bundle has no claim to anything ; older. -(requires-release "v0.44.0") +(requires-release "v0.46.0") (entry "run.x") diff --git a/python/runtime-flow.x b/python/runtime-flow.x index 7e5428b..ea92974 100644 --- a/python/runtime-flow.x +++ b/python/runtime-flow.x @@ -83,22 +83,44 @@ (def %py-env-def! (fn (_ env sym v) (eval (list (lit def) sym (list (lit lit) v)) env))) -; ENV's own (SYM . VALUE) cell, or (), from the list of cells that is a -; module's bindings; a parent's cell is never an answer. +; Whether ENV keeps its own bindings as a tree of (entry . (left . right)) +; nodes, each entry a (SYM . VALUE) cell, rather than a list of cells. The +; root does, and so does an environment whose spine the language made with +; pair, a program's or a module's (x-lang docs/engine-laws.md, law 10); a +; call's frame, which the engine makes, keeps a list. pair? tells the two +; apart, and the root is the one with no parent. +(def %py-env-tree? (fn (_ env) (if (null? (rest env)) #t (pair? env)))) + +; ENV's own (SYM . VALUE) cell, or (); a parent's cell is never an answer. +; The cell is the live one, so setting its rest rebinds SYM. (def %py-env-cell + (fn (_ env sym) + (if (%py-env-tree? env) + (%py-tree-cell (first env) sym) + (%py-list-cell (first env) sym)))) +(def %py-list-cell (fn (self cells sym) (match ((null? cells) ()) ((eq? (first (first cells)) sym) (first cells)) (#t (self (rest cells) sym))))) +; A tree is walked whole: its order is the engine's, not one a walk can use. +(def %py-tree-cell + (fn (self node sym) + (match + ((null? node) ()) + ((eq? (first (first node)) sym) (first node)) + (#t + (let ((c (self (first (rest node)) sym))) + (if (null? c) (self (rest (rest node)) sym) c)))))) -; Whether ENV itself binds SYM. The root's bindings are a tree rather than a -; list, and the root has no parent, so there a lookup is the answer. +; Whether ENV itself binds SYM. The root has no parent, so there a lookup +; is the answer, and one is quicker than a walk of the root's tree. (def %py-env-has? (fn (_ env sym) (if (null? (rest env)) (guard (_ #f) (%seq (eval sym env) #t)) - (not (null? (%py-env-cell (first env) sym)))))) + (not (null? (%py-env-cell env sym)))))) ; A program's names are bound before its first statement runs, each to ; DEFAULT (python/parse.x, %py-decls and %py-shims) -- unless the @@ -195,7 +217,7 @@ (def %py-mod-own (fn (_ m name) (let ((sym (%py-name->sym name))) - (let ((c (if (symbol? sym) (%py-env-cell (first (%py-mod-env m)) sym) ()))) + (let ((c (if (symbol? sym) (%py-env-cell (%py-mod-env m) sym) ()))) (if (if (null? c) #t (same? (rest c) %py-deleted)) %py-mod-missing (rest c)))))) (def %py-mod-no-attr @@ -239,14 +261,14 @@ (#t (%seq (%py-env-def! (%py-mod-env m) (%py-name->sym name) %py-deleted) ()))))) (python-sweep!) -; The module's names with their values, ("name" . value) rows in the order -; the module bound them, the deleted ones left out; the builtins module's are +; The module's names with their values, ("name" . value) rows in its +; frame's order, the deleted ones left out; the builtins module's are ; the builtins'. (def %py-mod-rows (fn (_ m) (if (%py-mod-builtins? m) (%py-builtins-rows m python-builtins) - (%py-env-rows (first (%py-mod-env m)) ())))) + (%py-env-rows (%py-mod-env m))))) ; --- the builtins module ------------------------------------------------------ ; @@ -278,32 +300,49 @@ () (pair (pair (first (first rows)) (%py-builtins-attr m (first (first rows)))) (self m (rest rows)))))) +; ENV's own names with their values, ("name" . value) rows, the deleted and +; the runtime's own left out. (def %py-env-rows + (fn (_ env) + (if (%py-env-tree? env) + (%py-tree-rows (first env) ()) + (%py-list-rows (first env) ())))) +(def %py-cell-row + (fn (_ c acc) + (let ((n (%py-sym->name (first c)))) + (if (if (null? n) #t (same? (rest c) %py-deleted)) + acc + (pair (pair n (rest c)) acc))))) +(def %py-list-rows (fn (self cells acc) (if (null? cells) acc - (self (rest cells) - (let ((n (%py-sym->name (first (first cells))))) - (if (if (null? n) #t (same? (rest (first cells)) %py-deleted)) - acc - (pair (pair n (rest (first cells))) acc))))))) - -; A module this runtime writes: an environment made from its rows, its -; __name__ bound first, as a module's is before its first statement runs, and -; a scope that holds its namespace dict, as a program's does (python/base.x). + (self (rest cells) (%py-cell-row (first cells) acc))))) +(def %py-tree-rows + (fn (self node acc) + (if (null? node) + acc + (self (rest (rest node)) + (self (first (rest node)) (%py-cell-row (first node) acc)))))) + +; A module this runtime writes: an environment its rows are bound in, its +; __name__ first, as a module's is before its first statement runs, and a +; scope that holds its namespace dict, as a program's does (python/base.x). +; The environment is made empty and bound with def: one made with pair keeps +; a tree (%py-env-tree?), which a list of cells handed to pair is not. (def python-module-new (fn (_ name rows) - (def env - (pair (%py-rows->cells (pair (pair "__name__" (python-str-of-x name)) rows) ()) - (pair () %py-base-env))) + (def env (pair () (pair () %py-base-env))) + (%py-rows-def! env (pair (pair "__name__" (python-str-of-x name)) rows)) (%py-env-def! (rest env) (lit %py-ns) (%py-ns-new env)) (%py-mod-new env))) -(def %py-rows->cells - (fn (self rows acc) +(def %py-rows-def! + (fn (self env rows) (if (null? rows) - acc - (self (rest rows) - (pair (pair (%py-name->sym (first (first rows))) (rest (first rows))) acc))))) + () + (%seq + (%py-env-def! env (%py-name->sym (first (first rows))) (rest (first rows))) + (self env (rest rows)))))) ; A module prints as its name and where it came from. (set! %py-module @@ -359,10 +398,10 @@ (fn (_ env) (if (python-obj-is env) (%py-attr-entries (python-obj-attrs env)) - (%py-attr-entries (%py-env-rows (first env) ()))))) + (%py-attr-entries (%py-env-rows env))))) (def %py-env-ns (fn (_ env) - (let ((c (%py-env-cell (first (rest env)) (lit %py-ns)))) + (let ((c (%py-env-cell (rest env) (lit %py-ns)))) (if (null? c) (%py-ns-new env) (rest c))))) ; K, a str, bound to V in ENV. @@ -386,7 +425,7 @@ (%py-append (%py-own-rows (python-obj-attrs env)) (%py-entry-rows entries))) (%seq (%py-ns-put-all! env entries) - (%py-ns-drop-others! env (%py-env-rows (first env) ()) entries))))) + (%py-ns-drop-others! env (%py-env-rows env) entries))))) ; an instance's rows the runtime keeps for itself, keyed with a % (def %py-own-rows (fn (self rows) @@ -492,7 +531,7 @@ (fn (self env top) (match ((same? env top) #f) - ((null? (%py-env-cell (first env) (lit %py-fn))) (self (rest env) top)) + ((null? (%py-env-cell env (lit %py-fn))) (self (rest env) top)) (#t #t)))) (def %py-here-rows @@ -507,9 +546,9 @@ (def %py-frame-rows (fn (self env top acc) (if (same? env top) - (%py-env-rows (first env) ()) - (let ((rows (%py-append (%py-rows-not-in (%py-env-rows (first env) ()) acc) acc))) - (if (null? (%py-env-cell (first env) (lit %py-fn))) + (%py-env-rows env) + (let ((rows (%py-append (%py-rows-not-in (%py-env-rows env) acc) acc))) + (if (null? (%py-env-cell env (lit %py-fn))) (self (rest env) top rows) rows))))) (def %py-rows-not-in @@ -1293,7 +1332,7 @@ (if (null? c) (%py-env-def! env sym v) (%seq (%set-rest! c v) ()))))))) (def %py-cell-out (fn (self env top sym) - (let ((c (%py-env-cell (first env) sym))) + (let ((c (%py-env-cell env sym))) (match ((not (null? c)) c) ((same? env top) ()) diff --git a/tests/specs/C0-env-frames.spec.md b/tests/specs/C0-env-frames.spec.md new file mode 100644 index 0000000..ed19f37 --- /dev/null +++ b/tests/specs/C0-env-frames.spec.md @@ -0,0 +1,49 @@ +# a program's and a module's environment + +A program's globals and a module's names live in an environment the runtime +makes with `pair`, and x-lang keeps a tree behind one of those (x-lang +`docs/engine-laws.md`, law 10, from v0.46.0); a call's frame keeps a list. +The runtime reads a name's own binding in either: a `global` or `nonlocal` +assignment rebinds the cell it finds, `globals()` and `locals()` list a +frame's names, and a module the runtime writes answers its attributes. +The expectations are CPython 3.14's. + +## a program's frame + +### a global and a nonlocal rebound, and globals() read + +```python +(python-run "x = 1\ndef f():\n global x\n x = 2\nf()\nprint(x)\ndef g():\n y = 1\n def h():\n nonlocal y\n y = 5\n h()\n return y\nprint(g())\nprint('x' in globals(), globals()['x'], 'nope' in globals())") +``` +--- +```output +2 +5 +True 2 False +``` + +### a function's locals() and a name bound after a hoist + +```python +(python-run "def f(a):\n b = 2\n return sorted(locals().items())\nprint(f(1))\nprint(len([1, 2]))\nlen = 3\nprint(len)") +``` +--- +```output +[('a', 1), ('b', 2)] +2 +3 +``` + +## a module the runtime writes + +### its attributes read, one stored, and its names listed + +```python +(python-run "import math\nprint(math.floor(2.5), math.__name__)\nmath.answer = 42\nprint(math.answer)\nprint('pi' in dir(math), 'answer' in dir(math))") +``` +--- +```output +2 math +42 +True True +```