Files
AILang/examples/lit_pat.ailx
T
Brummel af35612c1d Iter 16d: chain-terminator via __unreachable__ builtin
Eliminates the Unit-typed chain default that 16c left in place.
Adds __unreachable__ : forall a. a — codegen lowers to LLVM
unreachable. The 16a/16c chain machinery now uses it as the
deepest fall-through, so exhaustive matches with non-Unit arms
no longer need a (case _ ...) workaround.

- check/builtins.rs: register __unreachable__ as Forall(a, a).
- codegen: Term::Var "__unreachable__" emits LLVM unreachable
  and sets block_terminated; If/Match/fn-body gate downstream
  work on that flag.
- desugar: chain default switched from Lit{Unit} to Var.
- examples/lit_pat: categorize_first's trailing _ arm removed.
  Hash changes from c4faec3abc2ed388 to 644de0c0ec15fc17.
- examples/unreachable_demo: positive fixture using safe_div
  with __unreachable__ in the impossible branch.
- e2e + desugar tests: 122 → 124 (+2).

Path-a from 16b.2 planning: real primitive over a desugar-time
exhaustiveness pre-check (which would need cross-module type
registry access from desugar).

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

55 lines
2.1 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.
;
; Iter 16d update — the chain machinery's terminator is now the
; polymorphic `__unreachable__` builtin (`forall a. a`, lowered to
; LLVM `unreachable`) instead of a `Unit` literal. That removed the
; pre-16d need for a trailing `(case _ 0)` arm in `categorize_first`
; whose only purpose was to dominate the synthetic `Unit`-typed
; terminator. The two ctor arms (`Nil` and `Cons h _`) are
; exhaustive on `IntList`; the chain default is never reached.
;
; 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))))
(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))))))))))))