Title: General monad-agnostic nesting breaker: drop bind/discard name-gating in flattenDeepBinds · Issue #108 · purescript-lua/purescript-lua · GitHub
Open Graph Title: General monad-agnostic nesting breaker: drop bind/discard name-gating in flattenDeepBinds · Issue #108 · purescript-lua/purescript-lua
X Title: General monad-agnostic nesting breaker: drop bind/discard name-gating in flattenDeepBinds · Issue #108 · purescript-lua/purescript-lua
Description: Follow-up to #104 (PR #107). Background flattenDeepBinds breaks up deeply-nested do/>>= chains that would otherwise exceed Lua 5.1's parser nesting cap (chunk has too many syntax levels). It recognises bind/discard steps and lambda-lifts...
Open Graph Description: Follow-up to #104 (PR #107). Background flattenDeepBinds breaks up deeply-nested do/>>= chains that would otherwise exceed Lua 5.1's parser nesting cap (chunk has too many syntax levels). It recogn...
X Description: Follow-up to #104 (PR #107). Background flattenDeepBinds breaks up deeply-nested do/>>= chains that would otherwise exceed Lua 5.1's parser nesting cap (chunk has too many syntax levels)....
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/108
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"General monad-agnostic nesting breaker: drop bind/discard name-gating in flattenDeepBinds","articleBody":"Follow-up to #104 (PR #107).\n\n## Background\n\n`flattenDeepBinds` breaks up deeply-nested `do`/`>>=` chains that would otherwise exceed Lua 5.1's parser nesting cap (`chunk has too many syntax levels`). It recognises bind/discard steps and lambda-lifts the continuation chain into flat `$kont` helpers. #107 widened recognition to cover `discard` statement lines and monad-transformer binds, with goldens for Maybe, Either, State, Reader, Writer, Except, and a `StateT`/`Except` stack.\n\nRecognition is name-anchored: it matches `Control.Bind.bind` and `Control.Bind.discard` by name, and a `Bind` dictionary by its `bind` and `Apply0` fields. That is precise but incomplete. Two properties make a different design possible.\n\nFirst, the transform does not depend on recognition for correctness. Lambda-lifting only relocates continuations into `let`-bound helpers and forwards live variables by name. The head and action are kept verbatim, and no `bind`/`k` call is reordered, dropped, or duplicated. Flattening an expression that is not a bind is therefore still semantics-preserving. Recognition decides which expressions get restructured, never whether the result is correct.\n\nSecond, `NestingCheck` is a backstop. Anything that is not flattened and still nests too deeply is rejected at compile time with a clear error, never emitted as unloadable Lua.\n\n## The limitation\n\nBecause recognition is name-anchored, a chain shape it does not explicitly know about silently fails to flatten and falls through to a `NestingTooDeep` compile error instead of working:\n\n- Applicative chains: `ado`/`apply`, and `bindFlipped`/`=\u003c\u003c`.\n- Any future dictionary encoding that does not match the `bind` + `Apply0` literal-record shape.\n- A new monad whose bind head reduces through some form the normaliser does not handle. This is how the State transformer case was originally missed (see #107).\n\nThe State case showed the failure is graceful (a compile error, not a miscompile) but real: a category of valid `do` blocks fails to compile where it could work.\n\n## Proposal\n\nReplace name-anchored recognition with a structure-anchored breaker: flatten any sufficiently deep right-nested continuation chain `f a (\\x -> ...)` (last argument a lambda), whether or not `f` is a known bind. This relies on the two properties above. False positives are semantically safe, and `NestingCheck` catches whatever still overflows.\n\nIt subsumes the name-gated cases and removes the fragile surface: hardcoded names, dictionary-field assumptions, and reduction-shape assumptions. This is the general Lua-AST-level nesting breaker the #107 scope referred to as separate work.\n\n## Tradeoffs and open questions\n\nGolden churn. Any existing chain deeper than the threshold that is a right-nested lambda nest but not a bind would start flattening, changing its golden. The threshold (currently 50) bounds this, and in practice nests deeper than 50 are almost always binds. Needs a golden reset and a review of what actually moves.\n\nCodegen quality. For genuinely non-bind deep nests, helper extraction may be less tidy than the original. Acceptable when the alternative is a compile error, but worth eyeballing the output.\n\nPlacement. This may fit better as a Lua-AST-level pass (truly monad-agnostic, and able to catch non-`bind` constructs like deep `case` trees or long `\u003c>` chains) than as an IR pass. The IR-vs-Lua-AST choice is open.\n\nUpvalue budget. The `maxLiveSet` bail and the per-segment upvalue cost still apply. A structure-anchored pass fires on more shapes, so the bail behaviour should be re-checked.\n\n## Acceptance\n\n- A deep right-nested non-`bind` continuation chain, plus the currently-unsupported `ado`/`bindFlipped` cases, compiles to Lua that loads and runs under stock Lua 5.1.\n- No regression in the existing monad goldens.\n- `NestingCheck` retained as the backstop.\n\n## Non-goals\n\n- Reworking `NestingCheck` itself.\n- Improving codegen quality of the flattened output beyond \"loads and runs\".\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-06-22T13:45:51Z","url":"https://github.com/purescript-lua/purescript-lua/issues/108"}
| 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:9dc3ceac-51b0-2eb0-72d9-f8c09baf1d03 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D030:25A204:7F5AEF:AA1E92:6B00DB6C |
| html-safe-nonce | 1bdea62740148b0282465e66f80beef76255f696acc0f064d50f544445938922 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDMwOjI1QTIwNDo3RjVBRUY6QUExRTkyOjZCMDBEQjZDIiwidmlzaXRvcl9pZCI6IjMzNjQ0MTk4Mjg0NDk4NDE3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | be59d5d1de558d9dff67edd6d371f3226723f83c7871af5a829c6901bac5b5f0 |
| hovercard-subject-tag | issue:4717203645 |
| 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/108/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8c8ab08dab349c48301890329170d94fdf2cc062bff67f8070999a70c6daac39/purescript-lua/purescript-lua/issues/108 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8c8ab08dab349c48301890329170d94fdf2cc062bff67f8070999a70c6daac39/purescript-lua/purescript-lua/issues/108 |
| og:image:alt | Follow-up to #104 (PR #107). Background flattenDeepBinds breaks up deeply-nested do/>>= chains that would otherwise exceed Lua 5.1's parser nesting cap (chunk has too many syntax levels). It recogn... |
| 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