Files
AILang/examples/std_maybe.ailx
T
Brummel 41d406bcbb Iter 14g: Term::If restored (revert of 14d)
Reconsidered 14d's removal of Term::If. The decision was wrong.
"No redundancies" requires judgment; reducibility (if -> match)
is not redundancy in the strong sense. Term::If is a primitive
control-flow shape; bool branching is the second most common
shape after sequencing, and removing it cost 3x tokens on every
branch site (`(if c a b)` 4 tokens vs the match-on-Bool form
12 tokens).

Meta-pattern fixed: I had been treating user observations as
directives. User said "if is a subset of match"; I jumped to
remove it citing CLAUDE.md, with no independent conviction.
The leak appeared in 14f's JOURNAL prose ("three lines for what
if used to do in one"), which read as regret. Two feedback
memories saved (memory/feedback_user_suggestions_not_directives,
memory/feedback_no_nostalgia_for_removed_features) to head this
off.

Implementation: mechanical reverse-application of 14d's diff at
every site (AST, check including the 14e tail-position arm,
codegen 4 sites, surface parser/printer, pretty, CLI walker,
e2e test mutation). Removed lower_bool_match helper — it existed
only because 14d's migration shape needed codegen for non-ptr
match scrutinees; with Term::If back, match-on-Bool returns to
its pre-14d unsupported state. Three fixtures (sum, sort, max3)
restored to pre-14d shape. gc_stress (added in 14f) also
migrated back to (if ...) since it was authored under the wrong
constraint.

14e (musttail) and 14f (GC_malloc) verified intact in IR.

Hashes restored to pre-14d values:
- sum.sum: db33f57cb329935e
- sort.insert: 697fcb9f30f8633a
- max3.max: 65c45d6a45dd0a72
- max3.max3: 624b14429bf302f5

All other defs across all 18 fixtures keep their post-14f
hashes. Tests 80/80 green; cargo doc 0 warnings. LOC delta
+265/-295 net -30.

DESIGN.md Decision 7 preserved with a "Status: REVERTED" header
for audit trail. Form-(A) `if-term` production restored.

Plan: back to 15a (std_maybe stdlib module).

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

65 lines
1.6 KiB
Plaintext

; Iter 15a — first stdlib module: optional values.
; Polymorphic Maybe with four pure combinators.
(module std_maybe
(data Maybe (vars a)
(doc "Polymorphic optional value: either Just<a> or Nothing.")
(ctor Nothing)
(ctor Just a))
(fn from_maybe
(doc "Project out of Maybe<a> with a default for the Nothing case.")
(type
(forall (vars a)
(fn-type
(params a (con Maybe a))
(ret a))))
(params default m)
(body
(match m
(case (pat-ctor Just x) x)
(case (pat-ctor Nothing) default))))
(fn is_some
(doc "Returns true iff m is Just<_>.")
(type
(forall (vars a)
(fn-type
(params (con Maybe a))
(ret (con Bool)))))
(params m)
(body
(match m
(case (pat-ctor Just _) true)
(case (pat-ctor Nothing) false))))
(fn is_none
(doc "Returns true iff m is Nothing.")
(type
(forall (vars a)
(fn-type
(params (con Maybe a))
(ret (con Bool)))))
(params m)
(body
(match m
(case (pat-ctor Just _) false)
(case (pat-ctor Nothing) true))))
(fn map_maybe
(doc "Apply f to the wrapped value, or pass Nothing through.")
(type
(forall (vars a b)
(fn-type
(params (fn-type (params a) (ret b))
(con Maybe a))
(ret (con Maybe b)))))
(params f m)
(body
(match m
(case (pat-ctor Just x)
(term-ctor Maybe Just (app f x)))
(case (pat-ctor Nothing)
(term-ctor Maybe Nothing))))))