df5b76bd65
Third stdlib module. Either<e, a> is the first 2-type-var data def, and the eliminator `either : (e -> c) -> (a -> c) -> Either<e, a> -> c` the first fn with three type vars on top of the data — the deepest polymorphism shipped end-to-end so far. Five combinators: from_right, is_left, is_right, map_right, either. Demo exercises every one and runs to deterministic output. IR shows six distinct monomorphisations including two `from_right` variants (Left=Int vs Left=$u depending on call site) and `either__I_I_I`. No new compiler bugs surfaced: 14a / 14h / 15b coverage was wide enough to handle 2-type-var data plus 3-type-var fns out of the box. Discovered (queued as 15e): `ail render` and `ail parse` are not symmetric. Form-(A) printer in ailang_surface::print is the actual inverse of `parse` (round-trip test covers it across all 25 fixtures including std_either) but is not exposed via the CLI; `render` still calls the older ailang_core::pretty::module printer. Tests: 89/89 (was 88, +1 e2e for std_either_demo). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
82 lines
2.3 KiB
Plaintext
82 lines
2.3 KiB
Plaintext
; Iter 15d — third stdlib module: tagged-disjoint values.
|
|
; Either<e, a> is the canonical 2-type-var ADT. First stdlib module
|
|
; with two type parameters; first to ship an eliminator combinator
|
|
; (`either`) that introduces a third type var on top of the data.
|
|
|
|
(module std_either
|
|
|
|
(data Either (vars e a)
|
|
(doc "Disjoint sum: Left<e> for the failure branch, Right<a> for the success branch. Right is the conventional 'success' side.")
|
|
(ctor Left e)
|
|
(ctor Right a))
|
|
|
|
(fn from_right
|
|
(doc "Project the Right<a> payload, or use the default for Left<_>.")
|
|
(type
|
|
(forall (vars e a)
|
|
(fn-type
|
|
(params a (con Either e a))
|
|
(ret a))))
|
|
(params default x)
|
|
(body
|
|
(match x
|
|
(case (pat-ctor Right v) v)
|
|
(case (pat-ctor Left _) default))))
|
|
|
|
(fn is_left
|
|
(doc "Returns true iff x is Left<_>.")
|
|
(type
|
|
(forall (vars e a)
|
|
(fn-type
|
|
(params (con Either e a))
|
|
(ret (con Bool)))))
|
|
(params x)
|
|
(body
|
|
(match x
|
|
(case (pat-ctor Left _) true)
|
|
(case (pat-ctor Right _) false))))
|
|
|
|
(fn is_right
|
|
(doc "Returns true iff x is Right<_>.")
|
|
(type
|
|
(forall (vars e a)
|
|
(fn-type
|
|
(params (con Either e a))
|
|
(ret (con Bool)))))
|
|
(params x)
|
|
(body
|
|
(match x
|
|
(case (pat-ctor Left _) false)
|
|
(case (pat-ctor Right _) true))))
|
|
|
|
(fn map_right
|
|
(doc "Apply f to the Right<a> payload; pass Left<e> through untouched.")
|
|
(type
|
|
(forall (vars e a b)
|
|
(fn-type
|
|
(params (fn-type (params a) (ret b))
|
|
(con Either e a))
|
|
(ret (con Either e b)))))
|
|
(params f x)
|
|
(body
|
|
(match x
|
|
(case (pat-ctor Right v)
|
|
(term-ctor Either Right (app f v)))
|
|
(case (pat-ctor Left l)
|
|
(term-ctor Either Left l)))))
|
|
|
|
(fn either
|
|
(doc "Eliminator: apply on_left to a Left payload, on_right to a Right payload, fold both branches into the same result type c.")
|
|
(type
|
|
(forall (vars e a c)
|
|
(fn-type
|
|
(params (fn-type (params e) (ret c))
|
|
(fn-type (params a) (ret c))
|
|
(con Either e a))
|
|
(ret c))))
|
|
(params on_left on_right x)
|
|
(body
|
|
(match x
|
|
(case (pat-ctor Left l) (app on_left l))
|
|
(case (pat-ctor Right r) (app on_right r))))))
|