Title: Lower ST/Effect loop combinators (foreachE/forE/whileE) to native Lua for/while · Issue #233 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Lower ST/Effect loop combinators (foreachE/forE/whileE) to native Lua for/while · Issue #233 · purescript-lua/purescript-lua
X Title: Lower ST/Effect loop combinators (foreachE/forE/whileE) to native Lua for/while · Issue #233 · purescript-lua/purescript-lua
Description: Problem An ST/Effect loop stays a call into a foreign higher-order combinator. Effect.foreachE arr f, forE lo hi f, whileE cond body and their ST twins compile to a foreign function handed a Lua closure that it calls once per iteration: ...
Open Graph Description: Problem An ST/Effect loop stays a call into a foreign higher-order combinator. Effect.foreachE arr f, forE lo hi f, whileE cond body and their ST twins compile to a foreign function handed a Lua cl...
X Description: Problem An ST/Effect loop stays a call into a foreign higher-order combinator. Effect.foreachE arr f, forE lo hi f, whileE cond body and their ST twins compile to a foreign function handed a Lua cl...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/233
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lower ST/Effect loop combinators (foreachE/forE/whileE) to native Lua for/while","articleBody":"## Problem\n\nAn `ST`/`Effect` loop stays a call into a foreign higher-order combinator. `Effect.foreachE arr f`, `forE lo hi f`, `whileE cond body` and their `ST` twins compile to a foreign function handed a Lua closure that it calls once per iteration: one closure allocation and one call per step, on top of the combinator's own call. magicDo flattens the surrounding bind-chain but never touches the loop: the body stays a lambda passed to a foreign iterator.\n\nLowered at codegen time they become native loops:\n\n- `foreachE arr f` → `for i = 1, #arr do \u003cbody> end` with `f`'s parameter bound to `arr[i]`\n- `forE lo hi f` → `for i = lo, hi - 1 do \u003cbody> end`\n- `whileE cond body` → `while cond() do \u003cbody> end`\n\nso both the per-iteration closure and the foreign call disappear. This is the largest untapped constant-factor gap, and it is exactly the imperative shape hot code uses (`Data.Array.ST`, VDom-style traversals).\n\nA grep of `lib/` confirms pslua has no lowering for any of these combinators today.\n\n## Approach\n\nA codegen-time, name-keyed native-inline table matched to pslua's target. Match a saturated application of a known loop combinator by qualified name and spine shape, and emit the corresponding Lua statement instead of the call, inlining the body lambda as the loop body with its parameter as the loop variable:\n\n- ranged `forE lo hi \\i -> ...` → `for i = lo, hi - 1 do ... end` (PureScript `forE` is the half-open `[lo, hi)`, so the numeric `for` runs to `hi - 1`);\n- `foreachE arr \\x -> ...` → `for i = 1, #arr do ... end` with `x` bound to `arr[i]`, the shape the foreign implementation itself uses;\n- `whileE \\_ -> cond \\_ -> body` → `while cond do ... end`.\n\nThe menu is portable; the emission is Lua-specific. Fire only on the `Effect`/`ST` dictionaries (the gate magicDo already uses), never on a polymorphic `foreachE` whose dictionary is unresolved.\n\n## Prerequisites / Relations\n\nIndependent to land. Relates to the magicDo work: #182 (recognise Effect/ST by qualified name) and #228 (marker-driven do-chunks) both make the effect core easier to match structurally, and #182's own design record names \"compiling forE/whileE/tailRecM over Effect into native loops\" as a revisit trigger. This is that work. Composes with #239 (ref-cell unboxing): a native loop mutating an unboxed local is the end shape. Orthogonal to loopification (#181), which builds loops from tail recursion rather than from combinators.\n\n## Verification / Measurement\n\nA `foreachE`/`forE` body compiles to a Lua `for` with no per-iteration closure and no foreign call in `golden.lua`, and a matching eval golden proves identical runtime output (iteration count, side-effect order). A LuaJIT FNEW-census drop on the loop body through the #172 harness. A non-`Effect`/`ST` use of the same combinator names is left as a call, the soundness guard, tested directly.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-11T12:02:42Z","url":"https://github.com/purescript-lua/purescript-lua/issues/233"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:122910df-5c9b-102c-6aa5-561aaaba1ab1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D156:3C21B:97352B:C50788:6B00E047 |
| html-safe-nonce | 9f2b5b8d63529433f3ddff90d952b789e317bbbf2245f8b3bf27c8cec690475b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTU2OjNDMjFCOjk3MzUyQjpDNTA3ODg6NkIwMEUwNDciLCJ2aXNpdG9yX2lkIjoiNzIyNDA4MDAwMDkwNTk2MTU0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f1e8d45982489ca35b8ab018ddc20e917686ad250ec17d86a1f1a0e817cdd582 |
| hovercard-subject-tag | issue:4862140120 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/purescript-lua/purescript-lua/233/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dd2090bfaa36683f38abb2fcdfdec5b897b38e080be5814755cb1fb66f251505/purescript-lua/purescript-lua/issues/233 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dd2090bfaa36683f38abb2fcdfdec5b897b38e080be5814755cb1fb66f251505/purescript-lua/purescript-lua/issues/233 |
| og:image:alt | Problem An ST/Effect loop stays a call into a foreign higher-order combinator. Effect.foreachE arr f, forE lo hi f, whileE cond body and their ST twins compile to a foreign function handed a Lua cl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unisay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git |
| octolytics-dimension-user_id | 131581790 |
| octolytics-dimension-user_login | purescript-lua |
| octolytics-dimension-repository_id | 587200487 |
| octolytics-dimension-repository_nwo | purescript-lua/purescript-lua |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 587200487 |
| octolytics-dimension-repository_network_root_nwo | purescript-lua/purescript-lua |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width