Iter 15d: std_either ships — 2-type-var ADT + 3-type-var eliminator
Third stdlib module. Either<e, a> is the first 2-type-var data def, and the eliminator `either : (e -> c) -> (a -> c) -> Either<e, a> -> c` the first fn with three type vars on top of the data — the deepest polymorphism shipped end-to-end so far. Five combinators: from_right, is_left, is_right, map_right, either. Demo exercises every one and runs to deterministic output. IR shows six distinct monomorphisations including two `from_right` variants (Left=Int vs Left=$u depending on call site) and `either__I_I_I`. No new compiler bugs surfaced: 14a / 14h / 15b coverage was wide enough to handle 2-type-var data plus 3-type-var fns out of the box. Discovered (queued as 15e): `ail render` and `ail parse` are not symmetric. Form-(A) printer in ailang_surface::print is the actual inverse of `parse` (round-trip test covers it across all 25 fixtures including std_either) but is not exposed via the CLI; `render` still calls the older ailang_core::pretty::module printer. Tests: 89/89 (was 88, +1 e2e for std_either_demo). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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<e, a> → 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<e, a> → a` — eliminate Right or use default.
|
||||
- `is_left`, `is_right : Either<e, a> → Bool`.
|
||||
- `map_right : (a → b) → Either<e, a> → Either<e, b>` —
|
||||
Functor-style on the Right side.
|
||||
- `either : (e → c) → (a → c) → Either<e, a> → 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.
|
||||
|
||||
Reference in New Issue
Block a user