Iter 15d: std_either ships — 2-type-var ADT + 3-type-var eliminator
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>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"defs":[{"ctors":[{"fields":[{"k":"var","name":"e"}],"name":"Left"},{"fields":[{"k":"var","name":"a"}],"name":"Right"}],"doc":"Disjoint sum: Left<e> for the failure branch, Right<a> for the success branch. Right is the conventional 'success' side.","kind":"type","name":"Either","vars":["e","a"]},{"body":{"arms":[{"body":{"name":"v","t":"var"},"pat":{"ctor":"Right","fields":[{"name":"v","p":"var"}],"p":"ctor"}},{"body":{"name":"default","t":"var"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Project the Right<a> payload, or use the default for Left<_>.","kind":"fn","name":"from_right","params":["default","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"k":"var","name":"a"},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"var","name":"a"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"lit":{"kind":"bool","value":true},"t":"lit"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}},{"body":{"lit":{"kind":"bool","value":false},"t":"lit"},"pat":{"ctor":"Right","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Returns true iff x is Left<_>.","kind":"fn","name":"is_left","params":["x"],"type":{"body":{"effects":[],"k":"fn","params":[{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"con","name":"Bool"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"lit":{"kind":"bool","value":false},"t":"lit"},"pat":{"ctor":"Left","fields":[{"p":"wild"}],"p":"ctor"}},{"body":{"lit":{"kind":"bool","value":true},"t":"lit"},"pat":{"ctor":"Right","fields":[{"p":"wild"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Returns true iff x is Right<_>.","kind":"fn","name":"is_right","params":["x"],"type":{"body":{"effects":[],"k":"fn","params":[{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"con","name":"Bool"}},"k":"forall","vars":["e","a"]}},{"body":{"arms":[{"body":{"args":[{"args":[{"name":"v","t":"var"}],"fn":{"name":"f","t":"var"},"t":"app"}],"ctor":"Right","t":"ctor","type":"Either"},"pat":{"ctor":"Right","fields":[{"name":"v","p":"var"}],"p":"ctor"}},{"body":{"args":[{"name":"l","t":"var"}],"ctor":"Left","t":"ctor","type":"Either"},"pat":{"ctor":"Left","fields":[{"name":"l","p":"var"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Apply f to the Right<a> payload; pass Left<e> through untouched.","kind":"fn","name":"map_right","params":["f","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"effects":[],"k":"fn","params":[{"k":"var","name":"a"}],"ret":{"k":"var","name":"b"}},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"args":[{"k":"var","name":"e"},{"k":"var","name":"b"}],"k":"con","name":"Either"}},"k":"forall","vars":["e","a","b"]}},{"body":{"arms":[{"body":{"args":[{"name":"l","t":"var"}],"fn":{"name":"on_left","t":"var"},"t":"app"},"pat":{"ctor":"Left","fields":[{"name":"l","p":"var"}],"p":"ctor"}},{"body":{"args":[{"name":"r","t":"var"}],"fn":{"name":"on_right","t":"var"},"t":"app"},"pat":{"ctor":"Right","fields":[{"name":"r","p":"var"}],"p":"ctor"}}],"scrutinee":{"name":"x","t":"var"},"t":"match"},"doc":"Eliminator: apply on_left to a Left payload, on_right to a Right payload, fold both branches into the same result type c.","kind":"fn","name":"either","params":["on_left","on_right","x"],"type":{"body":{"effects":[],"k":"fn","params":[{"effects":[],"k":"fn","params":[{"k":"var","name":"e"}],"ret":{"k":"var","name":"c"}},{"effects":[],"k":"fn","params":[{"k":"var","name":"a"}],"ret":{"k":"var","name":"c"}},{"args":[{"k":"var","name":"e"},{"k":"var","name":"a"}],"k":"con","name":"Either"}],"ret":{"k":"var","name":"c"}},"k":"forall","vars":["e","a","c"]}}],"imports":[],"name":"std_either","schema":"ailang/v0"}
|
||||
@@ -0,0 +1,81 @@
|
||||
; 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))))))
|
||||
@@ -0,0 +1 @@
|
||||
{"defs":[{"body":{"args":[{"name":"x","t":"var"},{"lit":{"kind":"int","value":1},"t":"lit"}],"fn":{"name":"+","t":"var"},"t":"app"},"doc":"Add 1 to an Int. Used as the function arg to map_right and either.","kind":"fn","name":"inc","params":["x"],"type":{"effects":[],"k":"fn","params":[{"k":"con","name":"Int"}],"ret":{"k":"con","name":"Int"}}},{"body":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":0},"t":"lit"},{"args":[{"lit":{"kind":"int","value":42},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":99},"t":"lit"},{"args":[{"lit":{"kind":"int","value":7},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"args":[{"lit":{"kind":"int","value":7},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.is_left","t":"var"},"t":"app"}],"op":"io/print_bool","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"args":[{"lit":{"kind":"int","value":42},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.is_right","t":"var"},"t":"app"}],"op":"io/print_bool","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"lit":{"kind":"int","value":0},"t":"lit"},{"args":[{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":41},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.map_right","t":"var"},"t":"app"}],"fn":{"name":"std_either.from_right","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"lhs":{"args":[{"args":[{"name":"inc","t":"var"},{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":5},"t":"lit"}],"ctor":"Left","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.either","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"rhs":{"args":[{"args":[{"name":"inc","t":"var"},{"name":"inc","t":"var"},{"args":[{"lit":{"kind":"int","value":100},"t":"lit"}],"ctor":"Right","t":"ctor","type":"std_either.Either"}],"fn":{"name":"std_either.either","t":"var"},"t":"app"}],"op":"io/print_int","t":"do"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"t":"seq"},"doc":"Drive each std_either combinator. Expected outputs (per line): 42, 99, true, true, 42, 6, 101.","kind":"fn","name":"main","params":[],"type":{"effects":["IO"],"k":"fn","params":[],"ret":{"k":"con","name":"Unit"}}}],"imports":[{"module":"std_either"}],"name":"std_either_demo","schema":"ailang/v0"}
|
||||
@@ -0,0 +1,31 @@
|
||||
; Iter 15d — third consumer demo.
|
||||
; Imports std_either, exercises all five combinators. First demo to
|
||||
; pass two function arguments to a single combinator (the eliminator
|
||||
; `either`); first to drive a 3-type-var polymorphic fn end-to-end.
|
||||
|
||||
(module std_either_demo
|
||||
|
||||
(import std_either)
|
||||
|
||||
(fn inc
|
||||
(doc "Add 1 to an Int. Used as the function arg to map_right and either.")
|
||||
(type (fn-type (params (con Int)) (ret (con Int))))
|
||||
(params x)
|
||||
(body (app + x 1)))
|
||||
|
||||
(fn main
|
||||
(doc "Drive each std_either combinator. Expected outputs (per line): 42, 99, true, true, 42, 6, 101.")
|
||||
(type (fn-type (params) (ret (con Unit)) (effects IO)))
|
||||
(params)
|
||||
(body
|
||||
(seq (do io/print_int (app std_either.from_right 0 (term-ctor std_either.Either Right 42)))
|
||||
(seq (do io/print_int (app std_either.from_right 99 (term-ctor std_either.Either Left 7)))
|
||||
(seq (do io/print_bool (app std_either.is_left (term-ctor std_either.Either Left 7)))
|
||||
(seq (do io/print_bool (app std_either.is_right (term-ctor std_either.Either Right 42)))
|
||||
(seq (do io/print_int (app std_either.from_right 0
|
||||
(app std_either.map_right inc
|
||||
(term-ctor std_either.Either Right 41))))
|
||||
(seq (do io/print_int (app std_either.either inc inc
|
||||
(term-ctor std_either.Either Left 5)))
|
||||
(do io/print_int (app std_either.either inc inc
|
||||
(term-ctor std_either.Either Right 100))))))))))))
|
||||
Reference in New Issue
Block a user