iter it.1: loop/recur additive — Term::Loop/Term::Recur/LoopBinder end-to-end

Iteration-discipline milestone, 1 of 3. Adds named loop + recur as
strictly-additive first-class AST nodes: parse/print/prose/serde/
round-trip/schema lockstep, typecheck (binder typing + recur
arity/type unification via loop_stack threaded as mut.2's
mut_scope_stack; recur-tail-position via verify_loop_body), codegen
(loop-header + one phi per binder; recur back-edge br with a NEW
parallel block_terminated setter; lambda-boundary loop_frames
save/restore). Four Recur* CheckError variants. Strictly additive:
zero deletions touch tail-app/tail-do or the seven existing
block_terminated sites — this is what makes the destructive it.3
safe. recur synth = fresh metavar (resolves the plan's flagged
Type::unit() risk). loop_counter->55, loop_in_lambda->49, four
negatives fire, tail-app byte-identical, cargo test --workspace
green. Spec fda9b78, plan 7381a42.
This commit is contained in:
2026-05-15 14:38:55 +02:00
parent 7381a4233b
commit 96db54d15d
38 changed files with 1891 additions and 32 deletions
+12
View File
@@ -0,0 +1,12 @@
(module loop_counter
(fn main
(doc "Iter it.1 — sum 1..10 via an accumulator loop. Expected stdout: 55.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(app print
(loop ((var acc (con Int) 0) (var i (con Int) 1))
(if (app > i 10)
acc
(recur (app + acc i) (app + i 1))))))))
+24
View File
@@ -0,0 +1,24 @@
(module loop_in_lambda_e2e
(fn apply
(doc "apply a fn-of-Int to an Int")
(type (fn-type (params (fn-type (params (con Int)) (ret (con Int))) (con Int)) (ret (con Int))))
(params f x)
(body (app f x)))
(fn main
(doc "Iter it.1 — a loop inside a lambda body, invoked via a closure. The lambda computes x*x by summing x exactly x times through an accumulator loop; apply 7 prints 49. Codegen-soundness gate for the lambda-boundary loop_frames save/restore.")
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(app print
(app apply
(lam
(params (typed x (con Int)))
(ret (con Int))
(body
(loop ((var acc (con Int) 0) (var k (con Int) 0))
(if (app == k x)
acc
(recur (app + acc x) (app + k 1))))))
7)))))
+15
View File
@@ -0,0 +1,15 @@
(module loop_nested_in_lambda
(fn make_adder
(doc "Iter it.1 — a loop inside a lambda body (lambda-boundary analogue).")
(type (fn-type (params (con Int)) (ret (fn-type (params (con Int)) (ret (con Int))))))
(params base)
(body
(lam
(params (typed x (con Int)))
(ret (con Int))
(body
(loop ((var acc (con Int) base) (var k (con Int) 0))
(if (app == k x)
acc
(recur (app + acc 1) (app + k 1)))))))))
+11
View File
@@ -0,0 +1,11 @@
(module loop_smoke
(fn count_to
(doc "Iter it.1 — single counted loop; counts i up to n and returns n.")
(type (fn-type (params (con Int)) (ret (con Int))))
(params n)
(body
(loop ((var i (con Int) 0))
(if (app == i n)
i
(recur (app + i 1)))))))
@@ -0,0 +1,60 @@
{
"schema": "ailang/v0",
"name": "test_recur_arity_mismatch",
"imports": [],
"defs": [
{
"kind": "fn",
"name": "main",
"type": {
"k": "fn",
"params": [],
"ret": {
"k": "con",
"name": "Int"
},
"effects": []
},
"params": [],
"doc": "Iter it.1: recur passing 2 args to a 1-binder loop fires recur-arity-mismatch.",
"body": {
"t": "loop",
"binders": [
{
"name": "i",
"type": {
"k": "con",
"name": "Int"
},
"init": {
"t": "lit",
"lit": {
"kind": "int",
"value": 0
}
}
}
],
"body": {
"t": "recur",
"args": [
{
"t": "lit",
"lit": {
"kind": "int",
"value": 1
}
},
{
"t": "lit",
"lit": {
"kind": "int",
"value": 2
}
}
]
}
}
}
]
}
@@ -0,0 +1,60 @@
{
"schema": "ailang/v0",
"name": "test_recur_not_in_tail_position",
"imports": [],
"defs": [
{
"kind": "fn",
"name": "main",
"type": {
"k": "fn",
"params": [],
"ret": {
"k": "con",
"name": "Int"
},
"effects": []
},
"params": [],
"doc": "Iter it.1: recur in the lhs of a seq (non-tail) fires recur-not-in-tail-position.",
"body": {
"t": "loop",
"binders": [
{
"name": "i",
"type": {
"k": "con",
"name": "Int"
},
"init": {
"t": "lit",
"lit": {
"kind": "int",
"value": 0
}
}
}
],
"body": {
"t": "seq",
"lhs": {
"t": "recur",
"args": [
{
"t": "var",
"name": "i"
}
]
},
"rhs": {
"t": "lit",
"lit": {
"kind": "int",
"value": 0
}
}
}
}
}
]
}
+34
View File
@@ -0,0 +1,34 @@
{
"schema": "ailang/v0",
"name": "test_recur_outside_loop",
"imports": [],
"defs": [
{
"kind": "fn",
"name": "main",
"type": {
"k": "fn",
"params": [],
"ret": {
"k": "con",
"name": "Int"
},
"effects": []
},
"params": [],
"doc": "Iter it.1: a recur with no enclosing loop fires recur-outside-loop.",
"body": {
"t": "recur",
"args": [
{
"t": "lit",
"lit": {
"kind": "int",
"value": 1
}
}
]
}
}
]
}
@@ -0,0 +1,53 @@
{
"schema": "ailang/v0",
"name": "test_recur_type_mismatch",
"imports": [],
"defs": [
{
"kind": "fn",
"name": "main",
"type": {
"k": "fn",
"params": [],
"ret": {
"k": "con",
"name": "Int"
},
"effects": []
},
"params": [],
"doc": "Iter it.1: recur passing a Bool to an Int binder fires recur-type-mismatch.",
"body": {
"t": "loop",
"binders": [
{
"name": "i",
"type": {
"k": "con",
"name": "Int"
},
"init": {
"t": "lit",
"lit": {
"kind": "int",
"value": 0
}
}
}
],
"body": {
"t": "recur",
"args": [
{
"t": "lit",
"lit": {
"kind": "bool",
"value": true
}
}
]
}
}
}
]
}