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>
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,164 @@
|
||||
; Iter 15b — second stdlib module: polymorphic singly-linked lists.
|
||||
; Imports std_maybe so head/tail can return Maybe<a> / Maybe<List<a>>.
|
||||
; All cross-module references use the qualified form per the Iter 14h
|
||||
; convention: `std_maybe.Maybe` at type-name slots, `std_maybe.from_maybe`
|
||||
; for fns. Bare ctor names (`Just`, `Nothing`) stay unqualified — once
|
||||
; the type is resolved the ctor lookup is unambiguous.
|
||||
|
||||
(module std_list
|
||||
|
||||
(import std_maybe)
|
||||
|
||||
(data List (vars a)
|
||||
(doc "Polymorphic singly-linked list: Nil or Cons<a, List<a>>.")
|
||||
(ctor Nil)
|
||||
(ctor Cons a (con List a)))
|
||||
|
||||
(fn fold_left
|
||||
(doc "Tail-recursive left fold. The recursive call is in tail position and is marked.")
|
||||
(type
|
||||
(forall (vars a b)
|
||||
(fn-type
|
||||
(params (fn-type (params b a) (ret b))
|
||||
b
|
||||
(con List a))
|
||||
(ret b))))
|
||||
(params f acc xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) acc)
|
||||
(case (pat-ctor Cons h t)
|
||||
(tail-app fold_left f (app f acc h) t)))))
|
||||
|
||||
(fn fold_right
|
||||
(doc "Right fold. Constructor-blocked: the recursive call is the second arg of f, NOT a tail position.")
|
||||
(type
|
||||
(forall (vars a b)
|
||||
(fn-type
|
||||
(params (fn-type (params a b) (ret b))
|
||||
b
|
||||
(con List a))
|
||||
(ret b))))
|
||||
(params f acc xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) acc)
|
||||
(case (pat-ctor Cons h t)
|
||||
(app f h (app fold_right f acc t))))))
|
||||
|
||||
(fn length
|
||||
(doc "Length via fold_left. The accumulating lambda ignores the element and increments the counter.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a))
|
||||
(ret (con Int)))))
|
||||
(params xs)
|
||||
(body
|
||||
(app fold_left
|
||||
(lam (params (typed c (con Int)) (typed _ a))
|
||||
(ret (con Int))
|
||||
(body (app + c 1)))
|
||||
0
|
||||
xs)))
|
||||
|
||||
(fn reverse
|
||||
(doc "Reverse via fold_left with a flipping accumulator. Tail-recursive by virtue of the fold_left call.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a))
|
||||
(ret (con List a)))))
|
||||
(params xs)
|
||||
(body
|
||||
(app fold_left
|
||||
(lam (params (typed acc (con List a)) (typed h a))
|
||||
(ret (con List a))
|
||||
(body (term-ctor List Cons h acc)))
|
||||
(term-ctor List Nil)
|
||||
xs)))
|
||||
|
||||
(fn is_empty
|
||||
(doc "True iff the list is Nil.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a))
|
||||
(ret (con Bool)))))
|
||||
(params xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) true)
|
||||
(case (pat-ctor Cons _ _) false))))
|
||||
|
||||
(fn head
|
||||
(doc "Returns Just<a> of the first element, or Nothing for an empty list. Cross-module Maybe.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a))
|
||||
(ret (con std_maybe.Maybe a)))))
|
||||
(params xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) (term-ctor std_maybe.Maybe Nothing))
|
||||
(case (pat-ctor Cons h _) (term-ctor std_maybe.Maybe Just h)))))
|
||||
|
||||
(fn tail
|
||||
(doc "Returns Just<List<a>> of the tail, or Nothing for an empty list.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a))
|
||||
(ret (con std_maybe.Maybe (con List a))))))
|
||||
(params xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) (term-ctor std_maybe.Maybe Nothing))
|
||||
(case (pat-ctor Cons _ t) (term-ctor std_maybe.Maybe Just t)))))
|
||||
|
||||
(fn append
|
||||
(doc "Concatenate two lists. Constructor-blocked recursion: the recursive call is inside Cons, NOT a tail.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (con List a) (con List a))
|
||||
(ret (con List a)))))
|
||||
(params xs ys)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) ys)
|
||||
(case (pat-ctor Cons h t)
|
||||
(term-ctor List Cons h (app append t ys))))))
|
||||
|
||||
(fn map
|
||||
(doc "Polymorphic map. Constructor-blocked recursion.")
|
||||
(type
|
||||
(forall (vars a b)
|
||||
(fn-type
|
||||
(params (fn-type (params a) (ret b))
|
||||
(con List a))
|
||||
(ret (con List b)))))
|
||||
(params f xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) (term-ctor List Nil))
|
||||
(case (pat-ctor Cons h t)
|
||||
(term-ctor List Cons (app f h) (app map f t))))))
|
||||
|
||||
(fn filter
|
||||
(doc "Keep elements where p is true. Constructor-blocked when p holds; non-tail recursive call when p is false.")
|
||||
(type
|
||||
(forall (vars a)
|
||||
(fn-type
|
||||
(params (fn-type (params a) (ret (con Bool)))
|
||||
(con List a))
|
||||
(ret (con List a)))))
|
||||
(params p xs)
|
||||
(body
|
||||
(match xs
|
||||
(case (pat-ctor Nil) (term-ctor List Nil))
|
||||
(case (pat-ctor Cons h t)
|
||||
(if (app p h)
|
||||
(term-ctor List Cons h (app filter p t))
|
||||
(app filter p t)))))))
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,61 @@
|
||||
; 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)))))))))))))))
|
||||
Reference in New Issue
Block a user