Files
AILang/examples/std_list_demo.ailx
T
Brummel 92f4b4f8c7 Iter 15b: std_list ships, three more compiler gaps closed
Second stdlib module. Tester wrote std_list.ailx (10 combinators,
164 LOC) and a consumer demo. std_list typechecked standalone;
demo did not, surfacing three compiler bugs:

1. Check-side: Iter 14h's qualify_local_types was applied to
   Term::Var cross-module lookup but not to ctor-field types in
   Term::Ctor synth or Pattern::Ctor resolution. First recursive
   cross-module ADT (List has Cons a (List a) — recursive Con
   self-ref) triggers the bug. std_maybe slipped through because
   Maybe's ctors have no recursive Con field.
2. Codegen-side: same gap mirrored across 4 sites in codegen
   (Term::Ctor synth, lower_ctor, lower_match) plus a tweak to
   unify_for_subst (recurse on re-bind instead of strict equality
   so sibling-derived List<Int> accepts nullary-ctor's List<$u>
   wildcard).
3. Const codegen: emit_const rejected non-literal const bodies.
   The demo's xs : List<Int> = Cons 1 (...) requires it. Fix:
   per-module const table, Term::Var resolution loads literal
   consts from global, inlines non-literal bodies. Bare and
   qualified refs both supported.

All three fixes carry an "Iter 15b" code comment at their site.
~349/25 LOC across ailang-check, ailang-codegen, e2e.rs.

Tests 85 -> 87. New e2e std_list_demo asserts 11-line stdout:
length 5, is_empty false/true, head via from_maybe, tail length,
append length, reverse head, map double head, filter is_even
length, fold_left sum, fold_right sum. New ailang-check unit
test cross_module_recursive_adt_term_and_pat_ctor covers both
the original bug and the symmetric pat-ctor latent twin.

Hash invariance: all pre-15b fixtures + std_maybe defs
bit-identical. 14a / 14e / 14h regressions all green.

Cumulative state: 2 stdlib modules (std_maybe, std_list), 14
combinators, cross-module recursive ADT working end-to-end.
Three compiler bugs surfaced + fixed in dogfood since 14a (each
dogfood iter has surfaced ≥1).

Authoring observation: form (A) at 10 combinators is fine; main
friction is paren-counting in nested seq chains, not the form
itself. n-ary seq would help but is sugar.

Plan 15c: 1000-element list stress test for fold_left (tail-call-
marked) vs fold_right (constructor-blocked).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 18:34:49 +02:00

62 lines
2.4 KiB
Plaintext

; Iter 15b — second consumer demo.
; Imports both std_maybe and std_list. Exercises every combinator at
; least once. xs is a top-level fn `() -> List<Int>` (consts cannot
; reference user fns; the brief flagged this).
(module std_list_demo
(import std_maybe)
(import std_list)
(fn inc
(doc "Add 1 to an Int. Used as the (a -> b) arg to map.")
(type (fn-type (params (con Int)) (ret (con Int))))
(params x)
(body (app + x 1)))
(fn add
(doc "Binary +. Used as the fold accumulator op.")
(type (fn-type (params (con Int) (con Int)) (ret (con Int))))
(params a b)
(body (app + a b)))
(fn is_even
(doc "Predicate: x mod 2 == 0.")
(type (fn-type (params (con Int)) (ret (con Bool))))
(params x)
(body (app == (app % x 2) 0)))
(fn double
(doc "Multiply by 2. Used as the map arg.")
(type (fn-type (params (con Int)) (ret (con Int))))
(params x)
(body (app * x 2)))
(const xs
(doc "The canonical list [1,2,3,4,5] for the demo. Pure ctor expression, so a const works.")
(type (con std_list.List (con Int)))
(body
(term-ctor std_list.List Cons 1
(term-ctor std_list.List Cons 2
(term-ctor std_list.List Cons 3
(term-ctor std_list.List Cons 4
(term-ctor std_list.List Cons 5
(term-ctor std_list.List Nil))))))))
(fn main
(doc "Drive each std_list combinator once. Expected outputs (per line): 5, false, true, 1, 4, 10, 5, 2, 2, 15, 15.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(seq (do io/print_int (app std_list.length xs))
(seq (do io/print_bool (app std_list.is_empty xs))
(seq (do io/print_bool (app std_list.is_empty (term-ctor std_list.List Nil)))
(seq (do io/print_int (app std_maybe.from_maybe -1 (app std_list.head xs)))
(seq (do io/print_int (app std_list.length (app std_maybe.from_maybe xs (app std_list.tail xs))))
(seq (do io/print_int (app std_list.length (app std_list.append xs xs)))
(seq (do io/print_int (app std_maybe.from_maybe -1 (app std_list.head (app std_list.reverse xs))))
(seq (do io/print_int (app std_maybe.from_maybe -1 (app std_list.head (app std_list.map double xs))))
(seq (do io/print_int (app std_list.length (app std_list.filter is_even xs)))
(seq (do io/print_int (app std_list.fold_left add 0 xs))
(do io/print_int (app std_list.fold_right add 0 xs)))))))))))))))