Title: Unbox Ref/STRef operations in value positions (while-conditions, stored actions) · Issue #317 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Unbox Ref/STRef operations in value positions (while-conditions, stored actions) · Issue #317 · purescript-lua/purescript-lua
X Title: Unbox Ref/STRef operations in value positions (while-conditions, stored actions) · Issue #317 · purescript-lua/purescript-lua
Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
Open Graph Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
X Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
Opengraph URL: https://github.com/purescript-lua/purescript-lua
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unbox Ref/STRef operations in value positions (while-conditions, stored actions)","articleBody":"## Problem\n\nThe cell unboxing of #239 lowers a `Ref`/`STRef` operation only in run position — where the application carries the magic-do run marker, or is the body of a literal loop lambda the native-loop lowering runs per iteration. An operation used as a first-class value keeps the whole cell boxed. The most visible casualty is the `while` condition: `map (_ > 0) (STRef.read value)` is an ST action value built once and forced per iteration, so the cell it reads stays a `{value = …}` table even when everything else about it would unbox. From `test/ps/output/Golden.NativeLoopsST.Test/golden.lua` after #239 — `steps` (run positions only) unboxes while `value` (read inside the condition value) stays boxed:\n\n```lua\nlocal steps = 0\nlocal value = Control_Monad_ST_Internal_new(start)()\ndo\n local _S_cond0 = Control_Monad_ST_Internal_map_(function(v)\n return v >= 0 and v ~= 0\n end)(Control_Monad_ST_Internal_read(value))\n while _S_cond0() do\n```\n\n## Approach\n\nLower a recognised operation in value position to a nullary Lua closure over the unboxed local — `function() return g(value) end` for the condition above — which is semantically what the boxed foreign chain builds (the closure shares the local as an upvalue, so each forcing sees the current contents). The subtlety is evaluation timing: the boxed form evaluates the operation's non-reference arguments when the thunk is built, not when it is forced, so a non-atomic argument must be pre-bound outside the closure (or the analysis must require atomic arguments in value positions). The while-condition case can go further: when the condition's compiled run is a pure expression, the native-loop lowering can inline it as the `while` predicate directly instead of building the thunk at all.\n\n## Relations\n\nExtends #239; the value-position restriction and this exact `countDown` shape are pinned by `Golden.NativeLoopsST.Test`. Independent of the Effect-side dictionary gap (filed separately).\n\n## Verification\n\n`countDown`'s `value` cell in `Golden.NativeLoopsST.Test` compiles with no `{value = …}` table and the loop condition reads the local; eval golden unchanged. A stored-action shape (`let action = STRef.write 5 r` re-run twice) unboxes with a fresh golden pinning it.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-27T12:27:26Z","url":"https://github.com/purescript-lua/purescript-lua/issues/317"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:1d513a44-1f68-dc70-68cc-8a6a0a4464a7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BDE4:683FF:82AF28:AA138E:6B00D94A |
| html-safe-nonce | 8400fc8bb5c8eeb9325278983c55a7525de352c9074d850a488c5442e7c68f0e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREU0OjY4M0ZGOjgyQUYyODpBQTEzOEU6NkIwMEQ5NEEiLCJ2aXNpdG9yX2lkIjoiMTM5NDUzNDI5ODkzMzA1Nzg2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 74343cee20c47bf75c6d3f3b7452d6b293d27ab3859903d528e8c4b5fe7fc183 |
| hovercard-subject-tag | repository:587200487 |
| 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/purescript-lua/purescript-lua/issues/317 |
| twitter:image | https://opengraph.githubassets.com/638e8137a878305176b6a64ef473983d87906a525f956c0c5c3b1c72df4432e4/purescript-lua/purescript-lua |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/638e8137a878305176b6a64ef473983d87906a525f956c0c5c3b1c72df4432e4/purescript-lua/purescript-lua |
| og:image:alt | Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| turbo-cache-control | no-cache |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width