Title: Splice a tail-position application of a function literal into the enclosing body · Issue #295 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Splice a tail-position application of a function literal into the enclosing body · Issue #295 · purescript-lua/purescript-lua
X Title: Splice a tail-position application of a function literal into the enclosing body · Issue #295 · purescript-lua/purescript-lua
Description: Problem #293 and #294 eliminated the scope-call closures, but the trailing-call fold stops one step short: when the scope call's tail returned a function literal, the pushed application lands on that literal and stays there. From Golden....
Open Graph Description: Problem #293 and #294 eliminated the scope-call closures, but the trailing-call fold stops one step short: when the scope call's tail returned a function literal, the pushed application lands on th...
X Description: Problem #293 and #294 eliminated the scope-call closures, but the trailing-call fold stops one step short: when the scope call's tail returned a function literal, the pushed application lands o...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/295
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Splice a tail-position application of a function literal into the enclosing body","articleBody":"## Problem\n\n#293 and #294 eliminated the scope-call closures, but the trailing-call fold stops one step short: when the scope call's tail returned a function literal, the pushed application lands on that literal and stays there. From `Golden.LongApplyChain.Test` on current main, `applySecond = lift2 (const identity)` compiles to:\n\n```lua\nlocal Golden_LongApplyChain_Test_applySecond_S_w = function(a_S_133, b_S_134)\n local v_S_625 = (function()\n ...\n end)()\n return (function(v1_S_626)\n if \"Data.Maybe∷Maybe.Just\" == v_S_625[1] then\n ...\n end\n end)(b_S_134)\nend\n```\n\nThe `(function(v1_S_626) … end)(b_S_134)` tail is a beta-redex in Lua clothing: one closure allocation and one extra call per invocation — per chain node in the 40-deep apply chain of that golden. The same shape sits in `Golden.BugListGenericEq.Test`'s `eq` (per list-element comparison), in `Golden.StringCodePoints.Test`'s `unsafeCodePointAt0` ordering scope (`(function(y_S_1601) … end)(1)`), and in `Golden.Issue37.Test` / `Golden.ArrayOfUnits.Test`.\n\n### Why the pipeline cannot fix this today\n\n`collapseTailScopeCall` (#230) pattern-matches a nullary callee — `FunctionCall (Function [] …) []` — because splicing a parameterized call needs a binding for the parameters, not just a statement splice. `foldCallThroughScopeCall` (#294) is what builds these shapes: it moves the application inward onto the returned literal, but it does not reduce it. And the IR's own beta reduction cannot see them — these literals only materialize during lowering and Lua-level rewriting.\n\n## Approach\n\nGeneralize `collapseTailScopeCall`: when the last statement of a function body is `return (function(p1, …, pn) \u003cstmts> end)(a1, …, an)` and every parameter is named, splice as a single simultaneous binding followed by the body:\n\n```lua\nlocal p1, …, pn = a1, …, an\n\u003cstmts>\n```\n\nThe one-statement `local` is the exact translation of Lua's call binding: the whole right-hand list is evaluated before any name binds (so an argument reading an outer `x` can never see a parameter named `x`), a multi-valued expression last in the list expands, missing values fill with `nil`, and extra values are evaluated then discarded — the same adjustment rules a call performs. And unlike #294's fold, the arguments do not move across any scope boundary: the call site already was the parent's tail return, so they are evaluated in the same environment at the same program point. The tail-position safety argument from #230 carries over verbatim for `\u003cstmts>`.\n\nConditions on top of the existing rule:\n\n- every parameter is `ParamNamed`: a `ParamVararg` cannot be bound by a `local`, and `ParamUnused` has no name to bind (IR-derived literals always name their parameters, so declining costs nothing);\n- zero parameters with nonempty arguments declines (`local = a1` does not exist); zero parameters with zero arguments is exactly the current rule, so the natural implementation is a generalization where the binding statement degenerates to nothing;\n- the local budget from #230 additionally counts the n parameter locals;\n- the vararg check on `\u003cstmts>` and the self-re-application after the merge are inherited unchanged.\n\nA duplicate parameter name (`function(x, x)`) binds the last occurrence in Lua, and `local x, x = a, b` reproduces exactly that; still, declining on duplicates is one `ordNub` away and removes the need to reason about it.\n\nThe alias local this leaves behind (`local v1_S_626 = b_S_134` above) is an accepted cost: one register slot inside the budget, no allocation, no call. Cleaning single-use aliases is a separate (IR- or Lua-level copy-propagation) concern.\n\n## Prerequisites / Relations\n\nBuilds directly on #230 (PR #293) and PR #294 — the fold is what exposes the redex in tail position. Independent of the magic-do redesign tracked in #228. Non-tail occurrences (a literal application in a `local` initializer, e.g. the `v_S_625` header above) are out of scope: splicing those needs return-to-assignment rewriting, a different transform.\n\n## Verification / Measurement\n\n`applySecond_S_w` becomes straight-line (zero closures per invocation):\n\n```lua\nlocal Golden_LongApplyChain_Test_applySecond_S_w = function(a_S_133, b_S_134)\n local v_S_625 = (function()\n ...\n end)()\n local v1_S_626 = b_S_134\n if \"Data.Maybe∷Maybe.Just\" == v_S_625[1] then\n ...\n end\nend\n```\n\nThe splice is visible in the `LongApplyChain`, `BugListGenericEq`, `StringCodePoints`, `Issue37` and `ArrayOfUnits` goldens; eval goldens unchanged. The `bench/ci` FNEW census should drop wherever the shape sits in a function body, with the corresponding trace-abort sites disappearing from the trace reports.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-24T09:55:42Z","url":"https://github.com/purescript-lua/purescript-lua/issues/295"}
| 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:ca004cbf-5b25-28c5-3598-b225f8d211ad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0A6:34EA7D:CF1CFF:109FC4E:6B00EF29 |
| html-safe-nonce | 34ef1a8b9dc6896ff0f7f5eac291d168c0a243328f089c4434b5eb897ce2e6f5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEE2OjM0RUE3RDpDRjFDRkY6MTA5RkM0RTo2QjAwRUYyOSIsInZpc2l0b3JfaWQiOiI2MDkwMjEyMDkyMjEwNjM4NjM0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e667d85055c59160cc750a6956915d0ba7953d0461559a767e63feedfcf5c431 |
| hovercard-subject-tag | issue:4967330594 |
| 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/295/issue_layout |
| twitter:image | https://opengraph.githubassets.com/552b814885fb1fb6bee6ca974f09a55669c2b7f9d5ddef04301dbf671d48099e/purescript-lua/purescript-lua/issues/295 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/552b814885fb1fb6bee6ca974f09a55669c2b7f9d5ddef04301dbf671d48099e/purescript-lua/purescript-lua/issues/295 |
| og:image:alt | Problem #293 and #294 eliminated the scope-call closures, but the trailing-call fold stops one step short: when the scope call's tail returned a function literal, the pushed application lands on th... |
| 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 | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width