9339279181
Terminal iteration of the kernel-extension-mechanics milestone. Ships
the four language-level mechanisms named in the spec's § Goal:
Module.kernel + TypeDef.param-in schema, their Form-A surface,
flag-driven kernel-tier auto-injection, and generic param-in checker
enforcement with a new diagnostic.
Schema (Tasks 1+2). Module gains a `kernel: bool` field
(skip_serializing_if = is_false), TypeDef gains a
`param_in: BTreeMap<String, BTreeSet<String>>` field
(skip-if-empty, kebab-renamed to "param-in"). Both fields are
strictly additive — every pre-existing fixture's canonical-JSON hash
is bit-stable except `prelude.ail`, which intentionally gains
`(kernel)`. The struct-literal sweep covered ~104 Module sites and
~35 TypeDef sites across the workspace; the additive serde-default
covers JSON deserialise paths, only Rust struct literals broke.
Form-A surface (Tasks 3+4). `(kernel)` is a bare module-header
attribute; `(param-in (a Int Float) (b Str))` is one outer
TypeDef-body clause carrying one or more inner var-lists (OQ1
decision — mirrors `(ctors …)`, one parser arm, deterministic
BTreeMap iteration). Both round-trip Form-A → JSON → Form-A
bit-identical.
Workspace-load migration (Task 5). The hardcoded `&["prelude"]`
literal at loader.rs:108 became a `modules.values().filter(|m|
m.kernel)` derivation; `parse_prelude()` injection stays because
the prelude has no on-disk manifest in user workspaces. Prelude
now carries `(kernel)` in its source, so the new filter picks it
up automatically. Code-path migration only — observable behaviour
is identical (prelude_free_fns.rs stays green). prelude hash
re-pinned (af372f28c726f29f) with Honesty-Rule provenance comment.
WorkspaceLoadError::ReservedModuleName diagnostic prose
repurposed: any built-in kernel module name is reserved
(currently prelude + kernel_stub), not specifically prelude. CLI
mapping at main.rs updated in lockstep.
Stub crate (Task 6). New `crates/ailang-kernel-stub/` is a
zero-dependency leaf crate carrying only `pub const STUB_AIL:
&str` with the Form-A source of the kernel_stub module (one
parametric TypeDef with param-in, one ctor). The parse hop —
`parse_kernel_stub()` — lives in ailang-surface next to
parse_prelude, keeping the crate-dependency graph acyclic
(`ailang-surface → ailang-kernel-stub → ailang-core`, no
back-edge). The stub is injected unconditionally in all builds as
the ratifying fixture for the kernel-extension mechanism; future
base extensions may add more or retire the stub. Drift-pinned by
`kernel_stub_module_round_trips`.
Checker (Task 7). New `CheckError::ParamNotInRestrictedSet`
variant + code() + ctx() arms + enforcement in
`check_type_well_formed`'s Type::Con arm — generic, data-driven
from the TypeDef, mentions no specific extension type. Two
in-source tests pin both the rejection (`Str` outside `{Int,
Float}`) and the acceptance (`Int` inside) paths.
Workspace-load integration tests (Task 8). New
`workspace_kernel.rs` integration-test crate with three tests:
auto-import without explicit `(import …)` declaration, two
kernel-tier modules co-load, explicit-import-overrides-auto-
import precedence preserved. Loader is import-tree-only so the
auto-import tests use a bridge module that brings the kernel
module into the workspace via the import graph — docstring
captures the reachability nuance for future readers.
Doc-state transitions (Task 9). INDEX.md kernel-extensions row
annotation transitions from "design accepted 2026-05-28; impl in
progress" to "mechanisms milestone closed 2026-05-28; raw-buf and
series milestones pending". Whitepaper STATUS + auto-import +
param-in sections transitioned forward→present for shipped
mechanisms; forward-tense survives only in sections describing
the still-pending raw-buf/series milestones (per Honesty-Rule).
data-model contract gains anchor blocks for both new schema
fields.
Side-effect: every binary's IR snapshot now contains ~52 lines
for `drop_kernel_stub_StubT` because the stub is auto-injected
into every workspace load. Snapshots refreshed; e2e expects 4
modules per workspace (prelude + kernel_stub + entry + zero or
more user modules) instead of the previous 3.
Plan defects scrubbed in the implementation (folded back into
the planner template via the planner's self-review checklist
next time): Task 4 sample test src used fictional
`(ctors (MkT a))` list form (project grammar is per-`(ctor MkT
a)`); Task 6 original wiring would have created a cycle
ailang-surface → ailang-kernel-stub → ailang-surface (inverted —
stub crate is zero-dep, parse hop lives in surface); Task 7 in-
source tests referenced a fictional `check_type_in_module`
helper (used the existing Workspace + check_workspace
convention); Task 8 first integration test expected loader to
auto-load kernel modules from disk (loader is import-tree-only;
tests use a bridge module).
Concern-5 fix folded in pre-commit: workspace.rs ReservedModuleName
doc-prose initially said "in test/dev builds" for kernel_stub —
but stub is unconditionally injected in all builds. Doc copy
tightened to present-state per Honesty-Rule.
Stats: 0 spec-review-loops, 0 quality-review-loops, 2 sweep-script
retries on Task 2 (brace-depth bug on nested vec![Ctor{…}],
recovered via per-file checkout + rewritten anchor-on-existing-
field sweep), 1 e2e-snapshot refresh on Task 6.
248 lines
11 KiB
Rust
248 lines
11 KiB
Rust
//! Roundtrip Invariant gate (in-process) for the form-(A) projection.
|
|
//!
|
|
//! Two complementary checks over `examples/*.ail`, each gathering
|
|
//! fixtures dynamically via `read_dir` (no hardcoded lists). The
|
|
//! tests are pure readers — they do not write into the working
|
|
//! tree.
|
|
//!
|
|
//! 1. `parse_is_deterministic_over_every_ail_fixture` — pins
|
|
//! **parse-determinism** (property 1 of the post-form-a Roundtrip
|
|
//! Invariant, design/contracts/0009-roundtrip-invariant.md). For every
|
|
//! well-formed `.ail` text `t`, `parse(t)` produces the same
|
|
//! canonical bytes every invocation. The parser is a pure
|
|
//! function of input — no randomness, no time dependence, no
|
|
//! environment leak. Hashing `canonical::to_bytes(parse(t))` is
|
|
//! therefore well-defined for any `.ail` source.
|
|
//!
|
|
//! 2. `parse_then_print_then_parse_is_idempotent_on_every_ail_fixture`
|
|
//! — pins **idempotency-under-print** (property 2). For every
|
|
//! well-formed `.ail` text `t`, asserts
|
|
//! `canonical_bytes(parse(t))` equals
|
|
//! `canonical_bytes(parse(print(parse(t))))`. The printer is a
|
|
//! left-inverse of the parser modulo canonical form.
|
|
//!
|
|
//! The other two properties of the post-form-a Roundtrip Invariant
|
|
//! live in sibling test crates: **CLI-pipeline-idempotency** is
|
|
//! pinned by `crates/ail/tests/roundtrip_cli.rs::cli_parse_then_render_then_parse_is_idempotent`,
|
|
//! and **carve-out-anchor** is pinned by
|
|
//! `crates/ailang-core/tests/carve_out_inventory.rs::examples_ail_json_inventory_matches_carve_outs`.
|
|
//!
|
|
//! Retired iter form-a.1 T9: `print_then_parse_round_trips_every_fixture`
|
|
//! (corpus shrunk to 8 carve-outs post-iter) and
|
|
//! `every_ail_fixture_matches_its_json_counterpart` (no longer
|
|
//! expressible: only one form is hand-authored post-iter).
|
|
|
|
use std::path::PathBuf;
|
|
|
|
fn examples_dir() -> PathBuf {
|
|
// `CARGO_MANIFEST_DIR` is the surface crate root; examples live two
|
|
// levels up.
|
|
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
crate_dir.parent().unwrap().parent().unwrap().join("examples")
|
|
}
|
|
|
|
// Retired iter form-a.1 T9:
|
|
// - `list_json_fixtures` helper (walked `.ail.json` for the two retired tests below).
|
|
// - `print_then_parse_round_trips_every_fixture`: corpus shrunk to 8 carve-outs
|
|
// post-iter; property subsumed by `parse_is_deterministic_over_every_ail_fixture`
|
|
// plus the surviving `parse_then_print_then_parse_is_idempotent_on_every_ail_fixture`.
|
|
// - `round_trip_one` helper (only consumer was the retired test above).
|
|
// - `every_ail_fixture_matches_its_json_counterpart`: no longer expressible —
|
|
// only one form is hand-authored post-iter; counterparts are derived in-process
|
|
// via `ail parse`.
|
|
|
|
fn list_ail_fixtures() -> Vec<PathBuf> {
|
|
let dir = examples_dir();
|
|
let mut paths: Vec<PathBuf> = std::fs::read_dir(&dir)
|
|
.unwrap_or_else(|e| panic!("read_dir({}): {e}", dir.display()))
|
|
.filter_map(|entry| entry.ok())
|
|
.map(|e| e.path())
|
|
.filter(|p| {
|
|
p.file_name()
|
|
.and_then(|n| n.to_str())
|
|
.map(|n| n.ends_with(".ail"))
|
|
.unwrap_or(false)
|
|
})
|
|
.collect();
|
|
paths.sort();
|
|
paths
|
|
}
|
|
|
|
/// Pins **idempotency-under-print** (property 2 of the post-form-a
|
|
/// Roundtrip Invariant, design/contracts/0009-roundtrip-invariant.md): for every
|
|
/// well-formed `.ail` text `t`, the composition `parse → print → parse`
|
|
/// is idempotent on the AST.
|
|
///
|
|
/// Direct enforcement complements the parse-determinism gate above
|
|
/// (which alone does not constrain the printer). Together they pin
|
|
/// `parse → print` as a left-inverse modulo canonical form for every
|
|
/// `.ail` fixture in the corpus.
|
|
#[test]
|
|
fn parse_then_print_then_parse_is_idempotent_on_every_ail_fixture() {
|
|
let fixtures = list_ail_fixtures();
|
|
assert!(
|
|
!fixtures.is_empty(),
|
|
"no .ail fixtures found under {}",
|
|
examples_dir().display()
|
|
);
|
|
|
|
let mut failures = Vec::<String>::new();
|
|
let mut passed = 0usize;
|
|
for path in &fixtures {
|
|
let text = match std::fs::read_to_string(path) {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
failures.push(format!("{}: read failed: {e}", path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
let parsed_once = match ailang_surface::parse(&text) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!("{}: parse(t) failed: {e}", path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
let printed = ailang_surface::print(&parsed_once);
|
|
let parsed_twice = match ailang_surface::parse(&printed) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!(
|
|
"{}: parse(print(parse(t))) failed: {e}\n--- printed ---\n{printed}",
|
|
path.display()
|
|
));
|
|
continue;
|
|
}
|
|
};
|
|
let bytes_a = ailang_core::canonical::to_bytes(&parsed_once);
|
|
let bytes_b = ailang_core::canonical::to_bytes(&parsed_twice);
|
|
if bytes_a != bytes_b {
|
|
let s_a = String::from_utf8_lossy(&bytes_a).into_owned();
|
|
let s_b = String::from_utf8_lossy(&bytes_b).into_owned();
|
|
failures.push(format!(
|
|
"{}: parse → print → parse is NOT idempotent.\nparse(t): {s_a}\nparse(print(parse(t))): {s_b}",
|
|
path.display()
|
|
));
|
|
continue;
|
|
}
|
|
passed += 1;
|
|
}
|
|
|
|
if !failures.is_empty() {
|
|
panic!(
|
|
"{} of {} .ail fixture(s) failed idempotency check (passed: {}):\n{}",
|
|
failures.len(),
|
|
fixtures.len(),
|
|
passed,
|
|
failures.join("\n\n")
|
|
);
|
|
}
|
|
eprintln!("parse→print→parse idempotency ok for {passed} .ail fixtures");
|
|
}
|
|
|
|
/// prep.2 (kernel-extension-mechanics): pins the Form-A round-trip for
|
|
/// `(new T <type-arg> <value-arg>)`. The fixture exercises the
|
|
/// `parse_new` dispatcher + the `write_term` Term::New arm, including
|
|
/// the Type-vs-Value arg disambiguation on the parser side (head
|
|
/// keyword `con` → NewArg::Type; bare integer → NewArg::Value).
|
|
///
|
|
/// Inline rather than `examples/*.ail` because no existing fixture
|
|
/// uses `Term::New` and the iter's out-of-scope note explicitly
|
|
/// declines a fixture migration in prep.2 (the construct will only
|
|
/// see real fixtures once raw-buf's codegen lands).
|
|
#[test]
|
|
fn round_trip_term_new_mixed_args() {
|
|
// (new Series (con Float) 3) — a Type-positional arg followed by
|
|
// a Value-positional arg. The body lives inside an otherwise
|
|
// minimal module shell that the printer emits in canonical form.
|
|
let form_a = "(module new_demo\n (data Series\n (ctor MkSeries (con Int)))\n (fn new\n (type (fn-type (params (con Int)) (ret (con Series))))\n (params n)\n (body (term-ctor Series MkSeries n)))\n (fn main\n (type (fn-type (params) (ret (con Series))))\n (params)\n (body (new Series (con Float) 3))))";
|
|
let parsed_once = ailang_surface::parse(form_a)
|
|
.expect("Form-A round-trip fixture must parse");
|
|
let printed = ailang_surface::print(&parsed_once);
|
|
let parsed_twice = ailang_surface::parse(&printed).expect("print(parse(x)) must re-parse");
|
|
let bytes_a = ailang_core::canonical::to_bytes(&parsed_once);
|
|
let bytes_b = ailang_core::canonical::to_bytes(&parsed_twice);
|
|
assert_eq!(
|
|
bytes_a, bytes_b,
|
|
"Term::New round-trip parse→print→parse is not idempotent.\nfirst: {}\nprint: {printed}\nsecond: {}",
|
|
String::from_utf8_lossy(&bytes_a),
|
|
String::from_utf8_lossy(&bytes_b),
|
|
);
|
|
}
|
|
|
|
/// Parse-determinism (post-form-a-default-authoring §C3): for every
|
|
/// well-formed `.ail` text `t`, `parse(t)` produces the same canonical
|
|
/// bytes every invocation. Loader is a pure function of input — no
|
|
/// randomness, no time dependence, no environment leak.
|
|
#[test]
|
|
fn parse_is_deterministic_over_every_ail_fixture() {
|
|
let mut checked = 0usize;
|
|
for ail_path in list_ail_fixtures() {
|
|
let text = std::fs::read_to_string(&ail_path)
|
|
.unwrap_or_else(|e| panic!("read {ail_path:?}: {e}"));
|
|
let m1 = ailang_surface::parse(&text)
|
|
.unwrap_or_else(|e| panic!("parse {ail_path:?}: {e:?}"));
|
|
let m2 = ailang_surface::parse(&text)
|
|
.unwrap_or_else(|e| panic!("parse {ail_path:?} (2nd): {e:?}"));
|
|
let b1 = ailang_core::canonical::to_bytes(&m1);
|
|
let b2 = ailang_core::canonical::to_bytes(&m2);
|
|
assert_eq!(
|
|
b1, b2,
|
|
"parse non-determinism on {ail_path:?}",
|
|
);
|
|
checked += 1;
|
|
}
|
|
assert!(checked > 0, "no .ail fixtures found");
|
|
}
|
|
|
|
/// prep.3 (kernel-extension-mechanics): the `(kernel)` module-header
|
|
/// attribute round-trips Form-A → JSON → Form-A bit-identical and
|
|
/// preserves the parsed bool through both halves.
|
|
#[test]
|
|
fn kernel_module_header_round_trips() {
|
|
let src = "(module k\n (kernel))\n";
|
|
let m = ailang_surface::parse(src).expect("Form-A parse of (kernel) module");
|
|
assert!(m.kernel, "parsed kernel: true");
|
|
|
|
let printed = ailang_surface::print(&m);
|
|
// Round-trip: print, re-parse, kernel still true.
|
|
let m2 = ailang_surface::parse(&printed).expect("re-parse printed form");
|
|
assert!(m2.kernel, "kernel: true survives round-trip");
|
|
|
|
// Default form (no (kernel)) stays kernel: false.
|
|
let plain = ailang_surface::parse("(module p)\n").expect("Form-A parse plain module");
|
|
assert!(!plain.kernel);
|
|
}
|
|
|
|
/// prep.3 (kernel-extension-mechanics): the `(param-in (var t1 t2 ...))`
|
|
/// TypeDef attribute round-trips Form-A → JSON → Form-A bit-
|
|
/// identical and preserves the parsed BTreeMap through both halves,
|
|
/// including the BTreeSet's deterministic alphabetical iteration
|
|
/// order.
|
|
#[test]
|
|
fn typedef_param_in_round_trips_surface() {
|
|
let src = concat!(
|
|
"(module m\n",
|
|
" (data T (vars a)\n",
|
|
" (ctor MkT a)\n",
|
|
" (param-in (a Int Float))))\n",
|
|
);
|
|
let m = ailang_surface::parse(src).expect("Form-A parse of (param-in ...)");
|
|
let td = match &m.defs[0] {
|
|
ailang_core::ast::Def::Type(t) => t,
|
|
_ => panic!("expected Type def"),
|
|
};
|
|
let allowed = td.param_in.get("a").expect("var `a` bound in param_in");
|
|
assert!(allowed.contains("Int"));
|
|
assert!(allowed.contains("Float"));
|
|
assert_eq!(allowed.len(), 2);
|
|
|
|
let printed = ailang_surface::print(&m);
|
|
let m2 = ailang_surface::parse(&printed).expect("re-parse printed form");
|
|
let td2 = match &m2.defs[0] {
|
|
ailang_core::ast::Def::Type(t) => t,
|
|
_ => panic!("expected Type def after round-trip"),
|
|
};
|
|
assert_eq!(td2.param_in, td.param_in, "param_in survives round-trip");
|
|
}
|