4683af6114
Desugar `Pattern::Lit` (top-level and nested in Ctor) to
`Term::If { cond = (== sv lit), then = body, else_ = fall_k }`.
After 16c, no Pattern::Lit survives the desugar pass; codegen
and typechecker never see one. `is_flat` reclassifies Lit as
non-flat to force the chain machinery; new `build_eq` helper
constructs the equality test per literal kind.
Tests: 99 → 103 (+1 e2e lit_pat_demo, +3 desugar unit). Demo
fixture exercises top-level lit arms (classify) and nested
lit-in-Ctor (categorize_first) with a local IntList; output
100/200/999/-1/0/7.
Known limitations documented in DESIGN.md and JOURNAL: (a)
chain machinery's Unit terminator means non-Wild-terminated
exhaustive matches with lit arms still need a trailing `_`
catch-all to type-check; (b) `==` is currently Int-only, so
Bool/Str/Unit lit patterns desugar correctly but error at
typecheck.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
53 lines
2.0 KiB
Plaintext
53 lines
2.0 KiB
Plaintext
; Iter 16c — lit patterns at top level and inside Ctor sub-patterns.
|
|
; `classify` matches an Int against three lit arms (0, 1, default).
|
|
; `categorize_first` matches a List<Int>: empty -> -1, head==0 -> 0,
|
|
; otherwise the head value. The latter exercises Pat-Lit nested
|
|
; inside Pat-Ctor's first field. Both shapes are now handled by the
|
|
; 16c desugar pass; codegen sees only If/Match-on-Ctor combinations.
|
|
;
|
|
; The trailing `_` catch-all in `categorize_first` is required by the
|
|
; 16a chain machinery: the chain's terminator is a `Unit` literal,
|
|
; reachable when no arm matches. A `_` arm dominates the chain so the
|
|
; terminator stays unreachable in practice and the match type-checks.
|
|
;
|
|
; Expected stdout (one per line): 100, 200, 999, -1, 0, 7.
|
|
|
|
(module lit_pat
|
|
|
|
(data IntList
|
|
(ctor Nil)
|
|
(ctor Cons (con Int) (con IntList)))
|
|
|
|
(fn classify
|
|
(doc "Map 0->100, 1->200, anything else->999.")
|
|
(type (fn-type (params (con Int)) (ret (con Int))))
|
|
(params n)
|
|
(body
|
|
(match n
|
|
(case (pat-lit 0) 100)
|
|
(case (pat-lit 1) 200)
|
|
(case _ 999))))
|
|
|
|
(fn categorize_first
|
|
(doc "Empty -> -1; first element 0 -> 0; otherwise return the head.")
|
|
(type (fn-type (params (con IntList)) (ret (con Int))))
|
|
(params xs)
|
|
(body
|
|
(match xs
|
|
(case (pat-ctor Nil) -1)
|
|
(case (pat-ctor Cons (pat-lit 0) _) 0)
|
|
(case (pat-ctor Cons h _) h)
|
|
(case _ 0))))
|
|
|
|
(fn main
|
|
(doc "Drive both fns. Expected (per line): 100, 200, 999, -1, 0, 7.")
|
|
(type (fn-type (params) (ret (con Unit)) (effects IO)))
|
|
(params)
|
|
(body
|
|
(seq (do io/print_int (app classify 0))
|
|
(seq (do io/print_int (app classify 1))
|
|
(seq (do io/print_int (app classify 17))
|
|
(seq (do io/print_int (app categorize_first (term-ctor IntList Nil)))
|
|
(seq (do io/print_int (app categorize_first (term-ctor IntList Cons 0 (term-ctor IntList Nil))))
|
|
(do io/print_int (app categorize_first (term-ctor IntList Cons 7 (term-ctor IntList Nil))))))))))))
|