Title: Lua optimizer rule pushDeclarationsDownTheInnerScope loses sharing and delays evaluation of let-bound values · Issue #136 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Lua optimizer rule pushDeclarationsDownTheInnerScope loses sharing and delays evaluation of let-bound values · Issue #136 · purescript-lua/purescript-lua
X Title: Lua optimizer rule pushDeclarationsDownTheInnerScope loses sharing and delays evaluation of let-bound values · Issue #136 · purescript-lua/purescript-lua
Description: pushDeclarationsDownTheInnerScope (lib/Language/PureScript/Backend/Lua/Optimizer.hs) rewrites function(a) local t = expensive(a); return function(b) ... t ... end end into function(a) return function(b) local t = expensive(a); ... t ... ...
Open Graph Description: pushDeclarationsDownTheInnerScope (lib/Language/PureScript/Backend/Lua/Optimizer.hs) rewrites function(a) local t = expensive(a); return function(b) ... t ... end end into function(a) return functi...
X Description: pushDeclarationsDownTheInnerScope (lib/Language/PureScript/Backend/Lua/Optimizer.hs) rewrites function(a) local t = expensive(a); return function(b) ... t ... end end into function(a) return functi...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/136
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lua optimizer rule pushDeclarationsDownTheInnerScope loses sharing and delays evaluation of let-bound values","articleBody":"`pushDeclarationsDownTheInnerScope` (`lib/Language/PureScript/Backend/Lua/Optimizer.hs`) rewrites\n\n```lua\nfunction(a) local t = expensive(a); return function(b) ... t ... end end\n```\n\ninto\n\n```lua\nfunction(a) return function(b) local t = expensive(a); ... t ... end end\n```\n\nwhenever an outer function body consists of `local` declarations followed by `return function ... end`. The rule dates back to the initial commit and its motivation is not documented anywhere (the spec only pins the mechanics).\n\nTwo consequences:\n\n## 1. Sharing is lost\n\nBefore the rewrite, `expensive(a)` runs once per partial application; after it, once per call of the inner function. PureScript's strict semantics make `let` the standard tool for sharing work across calls, so the degradation can be asymptotic:\n\n```purescript\nmemoized :: Int -> Int\nmemoized = let table = buildBigTable 1000 in \\n -> lookup table n\n```\n\ncompiles to exactly the outer-locals-then-return-function shape this rule matches, and after the rewrite `buildBigTable` runs on every call of `memoized n`.\n\n## 2. Evaluation is delayed, which is observable\n\nIn a strict language the let is evaluated when the outer function is applied. Moving the declaration into the inner function delays it until full application, so effects escape their intended timing. The clearest case is an error:\n\n```purescript\nf = \\a -> let checked = validate a in \\b -> use checked b\n```\n\n`f badInput` should raise at partial application; after the rewrite the raise happens only when the result is applied to `b`. Since generated code also uses `error` for pattern-match failures, this shifts where a crash surfaces.\n\n## What the rule actually pessimizes\n\nDisabling the rule and regenerating the goldens shows it was hitting type-class dictionary resolution: 16 `golden.lua` files change, all in the same direction. Typical diff (`MaybeChain`, `TailRecM2Shadow`):\n\n```diff\n apply = (function()\n+ local bind = M.Control_Bind_bind(M.Effect_monadEffect.Bind1())\n return function(f)\n- local bind = M.Control_Bind_bind(M.Effect_monadEffect.Bind1())\n return function(a)\n```\n\nWith the rule on, `bind`/`pure` were resolved on every call of the partially applied function; without it, once. All eval goldens (the hand-verified runtime oracle) pass unchanged.\n\n## Decision\n\n- The rule is disabled (removed from `rewriteRulesInOrder`, definition kept) and stays disabled.\n- The idiomatic replacement is a float-in pass at the IR level, where usage information is available: sink a `Let` binding toward its use site (into an `IfThenElse` branch that is the only user, next to a sole use), but never across a lambda, following the same discipline GHC's FloatIn applies (see Peyton Jones, Partain, Santos, \"Let-floating: moving bindings to give faster programs\", 1996). That recovers the legitimate part of what this rule may have been after (upvalue-to-local conversion, fewer live upvalues under Lua 5.1's limit of 60 per function) without touching evaluation timing or sharing.\n- Prerequisite for the float-in pass: the \"rename shadowed names first\" pipeline restructuring, so the new pass does not have to re-implement the De Bruijn index bookkeeping that produced #37, #56, and #134.\n\nFound during a backend audit on 2026-07-02.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-02T09:32:17Z","url":"https://github.com/purescript-lua/purescript-lua/issues/136"}
| 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:88907827-42b8-1276-6c2c-fc8a15aaf2bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB50:DB09F:AB8BD1:DC3B3F:6B00F82C |
| html-safe-nonce | ea9c3df6786d1505461dd63d24c8348c6d3325684285c06865c96d75478f3a53 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjUwOkRCMDlGOkFCOEJEMTpEQzNCM0Y6NkIwMEY4MkMiLCJ2aXNpdG9yX2lkIjoiMjAxNTg3Nzg4ODk3NjQxODg2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3d287ee0ce6297485b865d0a4dfa957df3f7413c976ba94c42696deddee4ec7f |
| hovercard-subject-tag | issue:4793120819 |
| 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/136/issue_layout |
| twitter:image | https://opengraph.githubassets.com/00cd4b244810a468e2906e4f984f4a2bb63fac10b74d902b3bea0d719f887c72/purescript-lua/purescript-lua/issues/136 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/00cd4b244810a468e2906e4f984f4a2bb63fac10b74d902b3bea0d719f887c72/purescript-lua/purescript-lua/issues/136 |
| og:image:alt | pushDeclarationsDownTheInnerScope (lib/Language/PureScript/Backend/Lua/Optimizer.hs) rewrites function(a) local t = expensive(a); return function(b) ... t ... end end into function(a) return functi... |
| 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 | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width