Title: CPR-style worker/wrapper on results: return constructed products as Lua multiple values · Issue #206 · purescript-lua/purescript-lua · GitHub
Open Graph Title: CPR-style worker/wrapper on results: return constructed products as Lua multiple values · Issue #206 · purescript-lua/purescript-lua
X Title: CPR-style worker/wrapper on results: return constructed products as Lua multiple values · Issue #206 · purescript-lua/purescript-lua
Description: Problem A function that always returns a constructed product allocates a table per call even when every caller immediately deconstructs the result. State-shaped code pays this on every bind (a fresh Tuple per step), uncons/splitAt-style ...
Open Graph Description: Problem A function that always returns a constructed product allocates a table per call even when every caller immediately deconstructs the result. State-shaped code pays this on every bind (a fres...
X Description: Problem A function that always returns a constructed product allocates a table per call even when every caller immediately deconstructs the result. State-shaped code pays this on every bind (a fres...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/206
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"CPR-style worker/wrapper on results: return constructed products as Lua multiple values","articleBody":"## Problem\n\nA function that always returns a constructed product allocates a table per call even when every caller immediately deconstructs the result. State-shaped code pays this on every bind (a fresh `Tuple` per step), `uncons`/`splitAt`-style helpers pay it per element. The allocation exists only to carry two or three values across a return boundary.\n\n## Approach\n\nThe result-side twin of the #24 worker/wrapper split (GHC calls it CPR, Constructed Product Result), with Lua's native multiple return values as the unboxed-tuple representation:\n\n```lua\n-- worker returns components, no table\nlocal step_w = function(s) return a, s2 end\n-- wrapper reboxes for callers that consume the product as a value\nlocal step = function(s)\n local a, s2 = step_w(s)\n return { [\"$ctor\"] = \"...Tuple\", value0 = a, value1 = s2 }\nend\n```\n\nAt a call site that immediately deconstructs the result, the inlined wrapper's rebox meets the #177 fold and the product never materializes. The termination side condition GHC's CPR carries (botCpr, unlifted results must not hide bottom) vanishes here: evaluation is strict, so the worker either reaches its return or the program has already diverged, and returning multiple values changes nothing about that.\n\nWhat it takes, and why this is deliberately parked rather than next in line:\n\n- an IR representation for multi-value results (functions currently return exactly one value);\n- consumption-aware rewriting of call sites: only deconstructing sites bypass the wrapper, a result flowing away as a first-class value keeps going through it;\n- interaction with the #24 split: workers are the natural place to change the return convention, so this is a second story on top of `IR/Uncurry.hs`.\n\nAmulet ships exactly this shape (\"unboxed tuples as multiple simultaneous Lua values\"), so there is a working precedent on the same target.\n\n## Prerequisites / Relations\n\nBuilds on the #24 machinery (landed in #202) and on #177 as the rebox canceller. Parked until #198, #180 and #186 have landed and the #172 counters show what share of allocations are immediately-deconstructed products; that number decides whether the plumbing above is worth it.\n\n## Verification / Measurement\n\nAn allocation counter on a State-shaped benchmark (one `Tuple` per bind step today) drops to near zero table allocations per step, while eval goldens stay byte-identical. #172 registers the end-to-end effect.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-07T20:10:11Z","url":"https://github.com/purescript-lua/purescript-lua/issues/206"}
| 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:1e62d165-714a-824b-733a-cbae41cb0888 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A996:21AC3F:A27B49:D18D33:6B00E04E |
| html-safe-nonce | f7e036115ffb289693668da00ee1a382192cdce03c8cd6b0edbab17cbbf1609e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTk2OjIxQUMzRjpBMjdCNDk6RDE4RDMzOjZCMDBFMDRFIiwidmlzaXRvcl9pZCI6IjM5MTQ2ODI3MTU3ODk2NDc5NTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e34ff79ef475d4446680e0b3f4b5e7e1c4e9b5680f16fd3167c22ddf853b038b |
| hovercard-subject-tag | issue:4831875807 |
| 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/206/issue_layout |
| twitter:image | https://opengraph.githubassets.com/454b986a12ced3ceb13c866b2ba3f13ab1852302ed67d6bf846a64389da2f6c9/purescript-lua/purescript-lua/issues/206 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/454b986a12ced3ceb13c866b2ba3f13ab1852302ed67d6bf846a64389da2f6c9/purescript-lua/purescript-lua/issues/206 |
| og:image:alt | Problem A function that always returns a constructed product allocates a table per call even when every caller immediately deconstructs the result. State-shaped code pays this on every bind (a fres... |
| 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