Skip to content

Lower IList functional combinators to scf loops - #728

Draft
nathanieltornow wants to merge 2 commits into
QuEraComputing:mainfrom
nathanieltornow:feat/ilist-map-to-scf
Draft

Lower IList functional combinators to scf loops#728
nathanieltornow wants to merge 2 commits into
QuEraComputing:mainfrom
nathanieltornow:feat/ilist-map-to-scf

Conversation

@nathanieltornow

@nathanieltornow nathanieltornow commented Sep 8, 2026

Copy link
Copy Markdown

Adds rewrite rules and an IListToLoop pass that lower Map, Foldl,
Foldr, Scan, ForEach, Any, and All to indexed scf.For loops.
Callbacks remain function calls; Map and Scan build output lists through
singleton concatenation.

The PR contains two commits:

  1. Fix Foldr execution and static unrolling to match its declared
    element-first callback signature:
    foldr(f, [a, b, c], z) = f(a, f(b, f(c, z))).
    This changes runtime behavior for callbacks that depend on argument order.
  2. Add the loop rewrites and composing pass. The pass infers types when
    needed and rewrites nested regions within the current method.

Rewriting, e.g., Map to Scf, might induce the existing SCF inference issue for growing loop-carried lists (#512). Rerunning inference after lowering could therefore infer unsound list lengths.

Evaluate right folds as fn(x1, fn(x2, fn(x3, init))), matching the declared callback signature. Update the interpreter and static unroller; Foldl retains accumulator-first calls.

This changes Foldr runtime behavior for callbacks sensitive to argument order. Cover subtraction and distinct element and accumulator types in execution, inference, and unrolling.
Add indexed-loop rewrites for Map, Foldl, Foldr, Scan, ForEach, Any, and All, composed by IListToLoop. Preserve callback calls and inferred result types, using singleton concatenation for output lists.

Test execution, empty inputs, types, nested control flow, closure captures, callback effects, and repeated application.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant