diff --git a/crates/ail/tests/e2e.rs b/crates/ail/tests/e2e.rs index 2efdd77..0caa87e 100644 --- a/crates/ail/tests/e2e.rs +++ b/crates/ail/tests/e2e.rs @@ -305,6 +305,26 @@ fn std_list_stress_1000_element_folds() { ); } +/// Iter 15d: `std_either` end-to-end. First stdlib ADT with two type +/// parameters (`Either`); first combinator with three type vars +/// (`either : (e -> c) -> (a -> c) -> Either -> c`). +/// +/// Property protected: monomorphisation correctly distinguishes per- +/// call-site instantiations, including (a) two `from_right` instances +/// differing only in the unused `e` (Int vs $u) and (b) the +/// 3-type-var `either__I_I_I` instantiation. If wildcard substitution +/// regressed, one of those would either fail to emit or collide with +/// another instantiation. +#[test] +fn std_either_demo() { + let stdout = build_and_run("std_either_demo.ail.json"); + let lines: Vec<&str> = stdout.lines().collect(); + assert_eq!( + lines, + vec!["42", "99", "true", "true", "42", "6", "101"] + ); +} + /// Guards `ail diff`: a modified body changes the hash of `sum`, while /// `main` stays unchanged. Expects exit code 1, `changed` contains exactly /// `sum`, `unchanged` contains `main`, `added`/`removed` empty. diff --git a/docs/JOURNAL.md b/docs/JOURNAL.md index ed55bbb..838c749 100644 --- a/docs/JOURNAL.md +++ b/docs/JOURNAL.md @@ -2515,3 +2515,94 @@ the stdlib can grow without them. + +--- + +## Iter 15d — `std_either`: 2-type-var ADT + 3-type-var eliminator + +**Goal.** Third stdlib module. Either is the canonical 2-type-var +disjoint sum. The eliminator combinator `either : (e → c) → (a → c) → +Either → c` introduces a third type var on top of the data, +making it the most polymorphism-dense fn shipped to date. + +**What shipped.** + +- `examples/std_either.ailx` (74 LOC). Defines `Either e a = Left e | Right a` + plus 5 combinators: + - `from_right : a → Either → a` — eliminate Right or use default. + - `is_left`, `is_right : Either → Bool`. + - `map_right : (a → b) → Either → Either` — + Functor-style on the Right side. + - `either : (e → c) → (a → c) → Either → c` — + catamorphism / fold-on-sum. +- `examples/std_either_demo.ailx` exercises every combinator, + including the eliminator with two distinct concrete instantiations + (over `Left 5` and over `Right 100`). +- Canonical JSON for both, parsed via `ail parse`, type-checked, + built, and executed end-to-end. + +**Output (deterministic).** + +``` +42 ; from_right 0 (Right 42) +99 ; from_right 99 (Left 7) +true ; is_left (Left 7) +true ; is_right (Right 42) +42 ; from_right 0 (map_right inc (Right 41)) +6 ; either inc inc (Left 5) +101 ; either inc inc (Right 100) +``` + +**Monomorphisation evidence.** Six distinct instantiations across the +five combinators, all generated correctly with the `$u` wildcard for +unused type-var positions: + +``` +@ail_std_either_from_right__I_I ; e=Int, a=Int +@ail_std_either_from_right__U_I ; e=$u, a=Int +@ail_std_either_is_left__I_U ; e=Int, a=$u +@ail_std_either_is_right__U_I ; e=$u, a=Int +@ail_std_either_map_right__U_I_I ; e=$u, a=Int, b=Int +@ail_std_either_either__I_I_I ; e=Int, a=Int, c=Int +``` + +The two `from_right` variants are notable: same source fn, two +different concrete `e` per call site (Int when scrutinee is Left 7; +$u when scrutinee is Right _ since Left's payload is unused). The +14a monomorphisation machinery picks the right one per site without +extra ceremony. + +The 3-type-var instantiation `either__I_I_I` is the deepest type +substitution shipped through monomorphisation so far. Both call +sites in main hit the same instantiation (`e=a=c=Int`) so a single +emit suffices. + +**No new compiler bugs surfaced.** First stdlib iter without a fresh +codegen/check fix. Indicator that 14a / 14h / 15b coverage was wide +enough to handle 2-type-var data + 3-type-var fns out of the box. + +**Discovered (not fixed in this iter):** `ail render` and `ail parse` +are not symmetric, despite the help text in the CLI claiming they +are. `render` calls the older `ailang_core::pretty::module` +human-pretty printer (form B-ish), while `parse` consumes form (A). +The form-(A) printer in `ailang_surface::print` is correctly the +inverse of `parse` — confirmed by the round-trip test in +`crates/ailang-surface/tests/round_trip.rs` which now covers 25 +fixtures including `std_either.ail.json` — but it is not exposed via +the CLI. Logged as 15e. + +**Tests: 89/89 (was 88, +1 e2e for std_either_demo).** Cumulative +31 e2e tests. + +**Cumulative state, post-15d.** + +- Stdlib modules: 3 (`std_maybe`, `std_list`, `std_either`). +- Combinators: 19. +- Type-system surface area exercised: 1-type-var data (Maybe, List + with recursion) and 2-type-var data (Either); 1- and 2- and + 3-type-var polymorphic fns; cross-module recursive ADTs; + monomorphisation-with-wildcards across all. + +**Queue update.** 15d done; remaining queue: 15e (CLI render/parse +symmetry — small, just discovered), then `std_pair` if more stdlib +is wanted, then 16a/16b language gaps. diff --git a/examples/std_either.ail.json b/examples/std_either.ail.json new file mode 100644 index 0000000..114667f --- /dev/null +++ b/examples/std_either.ail.json @@ -0,0 +1 @@ +{"defs":[{"ctors":[{"fields":[{"k":"var","name":"e"}],"name":"Left"},{"fields":[{"k":"var","name":"a"}],"name":"Right"}],"doc":"Disjoint sum: Left for the failure branch, Right for the success branch. Right is the conventional 'success' side.","kind":"type","name":"Either","vars":["e","a"]},{"body":{"arms":[{"body":{"name":"v","t":"var"},"pat":{"ctor":"Right","fields":[{"name":"v","p":"var"}],"p":"ctor"}},{"body":{"name":"default","t":"var"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Project the Right payload, or use the default for Left<_>.","kind":"fn","name":"from_right","params":["default","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"k":"var","name":"a"},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"var","name":"a"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"lit":{"kind":"bool","value":true},"t":"lit"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}},{"body":{"lit":{"kind":"bool","value":false},"t":"lit"},"pat":{"ctor":"Right","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Returns true iff x is Left<_>.","kind":"fn","name":"is_left","params":["x"],"type":{"body":{"effects":[],"k":"fn","params":[{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"con","name":"Bool"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"lit":{"kind":"bool","value":false},"t":"lit"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}},{"body":{"lit":{"kind":"bool","value":true},"t":"lit"},"pat":{"ctor":"Right","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Returns true iff x is Right<_>.","kind":"fn","name":"is_right","params":["x"],"type":{"body":{"effects":[],"k":"fn","params":[{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"con","name":"Bool"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"args":[{"args":[{"name":"v","t":"var"}],"fn":{"name":"f","t":"var"},"t":"app"}],"ctor":"Right","t":"ctor","type":"Either"},"pat":{"ctor":"Right","fields":[{"name":"v","p":"var"}],"p":"ctor"}},{"body":{"args":[{"name":"l","t":"var"}],"ctor":"Left","t":"ctor","type":"Either"},"pat":{"ctor":"Left","fields":[{"name":"l","p":"var"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Apply f to the Right payload; pass Left through untouched.","kind":"fn","name":"map_right","params":["f","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"effects":[],"k":"fn","params":[{"k":"var","name":"a"}],"ret":{"k":"var","name":"b"}},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"args":[{"k":"var","name":"e"},{"k":"var","name":"b"}],"k":"con","name":"Either"}},"k":"forall","vars":["e","a","b"]}},{"body":{"arms":[{"body":{"args":[{"name":"l","t":"var"}],"fn":{"name":"on_left","t":"var"},"t":"app"},"pat":{"ctor":"Left","fields":[{"name":"l","p":"var"}],"p":"ctor"}},{"body":{"args":[{"name":"r","t":"var"}],"fn":{"name":"on_right","t":"var"},"t":"app"},"pat":{"ctor":"Right","fields":[{"name":"r","p":"var"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Eliminator: apply on_left to a Left payload, on_right to a Right payload, fold both branches into the same result type c.","kind":"fn","name":"either","params":["on_left","on_right","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"effects":[],"k":"fn","params":[{"k":"var","name":"e"}],"ret":{"k":"var","name":"c"}},{"effects":[],"k":"fn","params":[{"k":"var","name":"a"}],"ret":{"k":"var","name":"c"}},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"var","name":"c"}},"k":"forall","vars":["e","a","c"]}}],"imports":[],"name":"std_either","schema":"ailang/v0"} diff --git a/examples/std_either.ailx b/examples/std_either.ailx new file mode 100644 index 0000000..02f8eee --- /dev/null +++ b/examples/std_either.ailx @@ -0,0 +1,81 @@ +; Iter 15d — third stdlib module: tagged-disjoint values. +; Either is the canonical 2-type-var ADT. First stdlib module +; with two type parameters; first to ship an eliminator combinator +; (`either`) that introduces a third type var on top of the data. + +(module std_either + + (data Either (vars e a) + (doc "Disjoint sum: Left for the failure branch, Right for the success branch. Right is the conventional 'success' side.") + (ctor Left e) + (ctor Right a)) + + (fn from_right + (doc "Project the Right payload, or use the default for Left<_>.") + (type + (forall (vars e a) + (fn-type + (params a (con Either e a)) + (ret a)))) + (params default x) + (body + (match x + (case (pat-ctor Right v) v) + (case (pat-ctor Left _) default)))) + + (fn is_left + (doc "Returns true iff x is Left<_>.") + (type + (forall (vars e a) + (fn-type + (params (con Either e a)) + (ret (con Bool))))) + (params x) + (body + (match x + (case (pat-ctor Left _) true) + (case (pat-ctor Right _) false)))) + + (fn is_right + (doc "Returns true iff x is Right<_>.") + (type + (forall (vars e a) + (fn-type + (params (con Either e a)) + (ret (con Bool))))) + (params x) + (body + (match x + (case (pat-ctor Left _) false) + (case (pat-ctor Right _) true)))) + + (fn map_right + (doc "Apply f to the Right payload; pass Left through untouched.") + (type + (forall (vars e a b) + (fn-type + (params (fn-type (params a) (ret b)) + (con Either e a)) + (ret (con Either e b))))) + (params f x) + (body + (match x + (case (pat-ctor Right v) + (term-ctor Either Right (app f v))) + (case (pat-ctor Left l) + (term-ctor Either Left l))))) + + (fn either + (doc "Eliminator: apply on_left to a Left payload, on_right to a Right payload, fold both branches into the same result type c.") + (type + (forall (vars e a c) + (fn-type + (params (fn-type (params e) (ret c)) + (fn-type (params a) (ret c)) + (con Either e a)) + (ret c)))) + (params on_left on_right x) + (body + (match x + (case (pat-ctor Left l) (app on_left l)) + (case (pat-ctor Right r) (app on_right r)))))) diff --git a/examples/std_either_demo.ail.json b/examples/std_either_demo.ail.json new file mode 100644 index 0000000..eb8bd46 --- /dev/null +++ b/examples/std_either_demo.ail.json @@ -0,0 +1 @@ +{"defs":[{"body":{"args":[{"name":"x","t":"var"},{"lit":{"kind":"int","value":1},"t":"lit"}],"fn":{"name":"+","t":"var"},"t":"app"},"doc":"Add 1 to an Int. Used as the function arg to map_right and either.","kind":"fn","name":"inc","params":["x"],"type":{"effects":[],"k":"fn","params":[{"k":"con","name":"Int"}],"ret":{"k":"con","name":"Int"}}},{"body":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":0},"t":"lit"},{"args":[{"lit":{"kind":"int","value":42},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":99},"t":"lit"},{"args":[{"lit":{"kind":"int","value":7},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"args":[{"lit":{"kind":"int","value":7},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.is_left","t":"var"},"t":"app"}],"op":"io/print_bool","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"args":[{"lit":{"kind":"int","value":42},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.is_right","t":"var"},"t":"app"}],"op":"io/print_bool","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":0},"t":"lit"},{"args":[{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":41},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.map_right","t":"var"},"t":"app"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"name":"inc","t":"var"},{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":5},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.either","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"args":[{"args":[{"name":"inc","t":"var"},{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":100},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.either","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"doc":"Drive each std_either combinator. Expected outputs (per line): 42, 99, true, true, 42, 6, 101.","kind":"fn","name":"main","params":[],"type":{"effects":["IO"],"k":"fn","params":[],"ret":{"k":"con","name":"Unit"}}}],"imports":[{"module":"std_either"}],"name":"std_either_demo","schema":"ailang/v0"} diff --git a/examples/std_either_demo.ailx b/examples/std_either_demo.ailx new file mode 100644 index 0000000..ea01e4a --- /dev/null +++ b/examples/std_either_demo.ailx @@ -0,0 +1,31 @@ +; Iter 15d — third consumer demo. +; Imports std_either, exercises all five combinators. First demo to +; pass two function arguments to a single combinator (the eliminator +; `either`); first to drive a 3-type-var polymorphic fn end-to-end. + +(module std_either_demo + + (import std_either) + + (fn inc + (doc "Add 1 to an Int. Used as the function arg to map_right and either.") + (type (fn-type (params (con Int)) (ret (con Int)))) + (params x) + (body (app + x 1))) + + (fn main + (doc "Drive each std_either combinator. Expected outputs (per line): 42, 99, true, true, 42, 6, 101.") + (type (fn-type (params) (ret (con Unit)) (effects IO))) + (params) + (body + (seq (do io/print_int (app std_either.from_right 0 (term-ctor std_either.Either Right 42))) + (seq (do io/print_int (app std_either.from_right 99 (term-ctor std_either.Either Left 7))) + (seq (do io/print_bool (app std_either.is_left (term-ctor std_either.Either Left 7))) + (seq (do io/print_bool (app std_either.is_right (term-ctor std_either.Either Right 42))) + (seq (do io/print_int (app std_either.from_right 0 + (app std_either.map_right inc + (term-ctor std_either.Either Right 41)))) + (seq (do io/print_int (app std_either.either inc inc + (term-ctor std_either.Either Left 5))) + (do io/print_int (app std_either.either inc inc + (term-ctor std_either.Either Right 100))))))))))))