Title: Generic fallback: flatten deeply-nested do-blocks for non-Effect/ST monads · Issue #104 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Generic fallback: flatten deeply-nested do-blocks for non-Effect/ST monads · Issue #104 · purescript-lua/purescript-lua
X Title: Generic fallback: flatten deeply-nested do-blocks for non-Effect/ST monads · Issue #104 · purescript-lua/purescript-lua
Description: Split off from #46. #46 fixes the Lua parser nesting limit (chunk has too many syntax levels, LUAI_MAXCCALLS ≈ 200) for the common case by adding magic-do for Effect and ST: their bind/discard/pure chains compile to a flat statement sequ...
Open Graph Description: Split off from #46. #46 fixes the Lua parser nesting limit (chunk has too many syntax levels, LUAI_MAXCCALLS ≈ 200) for the common case by adding magic-do for Effect and ST: their bind/discard/pure...
X Description: Split off from #46. #46 fixes the Lua parser nesting limit (chunk has too many syntax levels, LUAI_MAXCCALLS ≈ 200) for the common case by adding magic-do for Effect and ST: their bind/discard/pure...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/104
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic fallback: flatten deeply-nested do-blocks for non-Effect/ST monads","articleBody":"Split off from #46.\n\n#46 fixes the Lua parser nesting limit (`chunk has too many syntax levels`, `LUAI_MAXCCALLS` ≈ 200) for the common case by adding magic-do for **Effect** and **ST**: their `bind`/`discard`/`pure` chains compile to a flat statement sequence instead of nested continuations.\n\nThat leaves the **general case**: a long straight-line `do`/`>>=` chain in *any other* monad (`Maybe`, `Either`, `Aff`, `State`, `Writer`, a custom parser/decoder, or a large applicative `ado`) still emits ~200+ lexically nested closures and hits the same parser limit.\n\n### Why this matters specifically for the Lua backend\n\nOn the JS/ES backends deep nesting is only a performance cost — their parsers have no comparable cap. On Lua it is a **correctness** failure: the file will not even load. So, unlike upstream, we eventually need a backend-agnostic nesting breaker, not just Effect/ST magic-do.\n\n### Why it is harder than magic-do\n\nmagic-do works because Effect is `\\() -> ...` (a thunk we can inline as `local x = m()`). For a general monad, `bind m k` is a real call with monad-specific semantics — it cannot be turned into a sequential `local`. The naive \"hoist continuations into named locals\" does not work either: a `x \u003c- m` continuation captures `x`, and a later action may reference earlier `x`s, so the continuation cannot leave the enclosing scope.\n\nA real fix needs one of:\n- a CPS / trampoline-style transform, or\n- chunking the chain into helper functions that **thread the live environment** as explicit parameters, breaking the chain into segments each below the nesting cap.\n\n### Realistic triggers\n\nUncommon in hand-written code; realistic in machine-generated code, decoders/validators for very wide records, and large `ado` constructors (and `Aff`, if/when it is ported to Lua). Lower priority than #46 accordingly.\n\n### Acceptance\n\nA `do` block of N ≫ 200 statements in a non-Effect monad (e.g. a long `Maybe`/`State` chain) compiles to Lua that loads and runs correctly under stock Lua 5.1.","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-06-15T14:38:49Z","url":"https://github.com/purescript-lua/purescript-lua/issues/104"}
| 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:19cbc91f-4aa4-cff9-8ac5-5b5963494b0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D04A:95C88:7CB56B:A796A8:6B00DB6F |
| html-safe-nonce | 46fe177b2b8d0183fe797ef0dbae27dd187f1f556c078782abcd172a8f355c62 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDRBOjk1Qzg4OjdDQjU2QjpBNzk2QTg6NkIwMERCNkYiLCJ2aXNpdG9yX2lkIjoiNTkyMTE4ODUxMTg4NDU2NTM1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ff450548b8522f9ae5d6e118c05c111cf33773e964839238839eaf72ab045d5e |
| hovercard-subject-tag | issue:4666090164 |
| 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/104/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8d3176d7d43f9cefc83dafd6f3b4f2396493d3fd301165adb8caaf7532176f04/purescript-lua/purescript-lua/issues/104 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8d3176d7d43f9cefc83dafd6f3b4f2396493d3fd301165adb8caaf7532176f04/purescript-lua/purescript-lua/issues/104 |
| og:image:alt | Split off from #46. #46 fixes the Lua parser nesting limit (chunk has too many syntax levels, LUAI_MAXCCALLS ≈ 200) for the common case by adding magic-do for Effect and ST: their bind/discard/pure... |
| 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