Title: freshenBinders renames a free reference sharing its Standalone Let binder's name · Issue #345 · purescript-lua/purescript-lua · GitHub
Open Graph Title: freshenBinders renames a free reference sharing its Standalone Let binder's name · Issue #345 · purescript-lua/purescript-lua
X Title: freshenBinders renames a free reference sharing its Standalone Let binder's name · Issue #345 · purescript-lua/purescript-lua
Description: Problem freshenBinders — the traversal that alpha-renames an expression's binders before a copy of it is pasted somewhere else — renames the binders of a Let by collecting every binder name of the Let into a rename map up front, then ren...
Open Graph Description: Problem freshenBinders — the traversal that alpha-renames an expression's binders before a copy of it is pasted somewhere else — renames the binders of a Let by collecting every binder name of the ...
X Description: Problem freshenBinders — the traversal that alpha-renames an expression's binders before a copy of it is pasted somewhere else — renames the binders of a Let by collecting every binder name of ...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/345
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"freshenBinders renames a free reference sharing its Standalone Let binder's name","articleBody":"## Problem\n\n`freshenBinders` — the traversal that alpha-renames an expression's binders before a copy of it is pasted somewhere else — renames the binders of a `Let` by collecting every binder name of the `Let` into a rename map up front, then renaming all the right-hand sides and the body under that one map. It says so itself:\n\n```haskell\n Let ann binds body → do\n -- Under unique binders no Let name can be referenced before it is\n -- bound, so all the groupings can enter the rename map up front.\n renames' ← foldlM (…) renames (filter (/= discardName) (bindingNames =\u003c\u003c toList binds))\n let renameBound (bindAnn, name, expr) =\n (bindAnn, Map.findWithDefault name name renames',) \u003c$> go renames' expr\n Let ann \u003c$> traverse (traverse renameBound) binds \u003c*> go renames' body\n```\n\nThat is sound only under the global uniqueness condition (GUC), which the comment names. The IR's scoping rule is sequential — `Note [Sequential scoping of Let bindings]` — and its first clause says a `Standalone` binding is non-recursive: its own name is *not* in scope in its right-hand side, so a reference to that name from its own RHS resolves to an outer binder. Under GUC no free variable can share a binder's name, so hoisting the map cannot capture anything. On input that is not GUC, it can: the binder's rename entry is already in the map when its own RHS is walked, so a *free* occurrence that happens to share the binder's name is renamed along with the binder, silently repointing it.\n\nThe minimal shape is `let x = x in l`, where the `x` on the right is free. Freshening it should rename only the binder, but renames both:\n\n```\n Let\n- Standalone ( Nothing , Name \"x$0\" , Ref Nothing (Local (Name \"x$0\")) ) -- actual\n+ Standalone ( Nothing , Name \"x\" , Ref Nothing (Local (Name \"x\")) ) -- expected\n Ref Nothing (Local (Name \"l\"))\n```\n\n`freshenBinders` is the odd one out here. `alphaEq` implements the sequential rule explicitly — it compares a `Standalone` right-hand side *before* binding the name, with the comment `-- The RHS of a Standalone binding does not see its own binder:` — and `countFreeRefs` does the same. Even `freshenBinders`' own `LetValues` case threads the map correctly, for the same reason: `-- The RHS is renamed under the incoming map — the binders scope over the body only`. Only the `Let` case hoists.\n\n## How it shows up\n\nAs an intermittent failure of the property `IR Optimizer / inlines expressions referenced once`, at roughly 1 run in 100. That test feeds `optimizedExpression` generated expressions that are deliberately *not* uniquified, inlines a once-used binding, and compares the pasted copy to the original up to alpha-equivalence. It fails exactly when the generator happens to produce the degenerate self-shadowing shape above, which is why the rate is low.\n\nThe failure reproduces on unmodified `main` (245 examples, same test) at the same rate, so it is not new. It also needs the whole `IR Optimizer` group to run — 200 runs of the test in isolation never reproduced it, because the group's global seed drives which hedgehog seeds this test receives.\n\n## Impact\n\nNone on emitted code. `uniquifyNames` is the pipeline's entry pass and every pass declares `passRequires = guc`, so real compilation never presents non-GUC input to `freshenBinders`. The cost is a test suite that reddens ~1% of the time, which trains everyone to re-run rather than read.\n\n## Approach\n\nThread the rename map through the groupings instead of hoisting it, matching the scoping rule the rest of the codebase implements: a `Standalone` right-hand side is renamed under the incoming map and only then does its binder enter; a `RecursiveGroup`'s members all enter before any of their right-hand sides are walked.\n\n```haskell\n Let ann binds body → do\n (renames', binds') ← mapAccumM freshenGrouping renames binds\n Let ann binds' \u003c$> go renames' body\n```\n\n```haskell\n freshenGrouping renames = \\case\n Standalone (bindAnn, name, expr) → do\n expr' ← go renames expr -- before the binder is in scope\n (renames', name') ← bindFresh renames name\n pure (renames', Standalone (bindAnn, name', expr'))\n RecursiveGroup members → do\n (renames', names) ←\n mapAccumM (\\rs (_, n, _) → bindFresh rs n) renames members\n members' ← forM (NE.zip members names) \\((bindAnn, _, expr), name') →\n (bindAnn, name',) \u003c$> go renames' expr\n pure (renames', RecursiveGroup members')\n```\n\n`bindFresh` keeps the existing `discardName` exemption, which must survive: a magic-do-lowered thunk binds several `Let` statements to `_`, and a single name-keyed entry would send them all to the same fresh name. `mapAccumM` is already the idiom the `AbsN` case uses for parameters.\n\n## Verification\n\nThe change is a no-op on GUC input, where the two orders provably agree, so the whole golden corpus should stay byte-identical and the suite green — that is the check that no caller (`substituteCopyM` / `substituteMoveM`, the call-pattern specializer, and the five call-site inliners in the optimizer) relied on the hoisted behaviour.\n\nThe existing `prop \"is an alpha-renaming of GUC-shaped input\"` deliberately restricts itself to uniquified input, so it cannot catch this. A non-GUC sibling is the red-first test: assert that freshening `let x = x in l` leaves the free `x` alone.\n\n## Prerequisites / Relations\n\nIndependent — nothing needs to land first. Surfaced while implementing #251 (PR #344), but not caused by it: the flake reproduces identically on the `main` commit before that change. The fix touches `freshenBinders`, which every call-site inliner and the call-pattern specializer go through, so it wants its own diff rather than riding along with feature work.\n\n## Acceptance criteria\n\n- Freshening a `Standalone` binding renames the binder without touching a free reference of the same name in its own right-hand side.\n- The `discard` binder exemption still holds.\n- `IR Optimizer / inlines expressions referenced once` survives a few hundred consecutive runs of the whole spec group.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-29T08:25:24Z","url":"https://github.com/purescript-lua/purescript-lua/issues/345"}
| 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:1be31a7b-cdc6-a3a5-3ce0-c06628dabd2f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFD6:BBAAA:7E960B:A91968:6B00DB6A |
| html-safe-nonce | febc41d1917f6786a7b109a961e9e590ef9e5025ce643571a74c25b255cd1f7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkQ2OkJCQUFBOjdFOTYwQjpBOTE5Njg6NkIwMERCNkEiLCJ2aXNpdG9yX2lkIjoiNDc2NzMxNjQyNzkzNTg5ODQ3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 48b79a79cf231ac819fda471262cde21e9ca26e7706091a45b6c1e903b54a349 |
| hovercard-subject-tag | issue:5007099645 |
| 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/345/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ac5a492d82e16601dadfd728cbfbcac4000882061312d08d6e133a17d2de4959/purescript-lua/purescript-lua/issues/345 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ac5a492d82e16601dadfd728cbfbcac4000882061312d08d6e133a17d2de4959/purescript-lua/purescript-lua/issues/345 |
| og:image:alt | Problem freshenBinders — the traversal that alpha-renames an expression's binders before a copy of it is pasted somewhere else — renames the binders of a Let by collecting every binder name of the ... |
| 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