Title: Bound the top-down IR rewrite driver's re-application loop · Issue #351 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Bound the top-down IR rewrite driver's re-application loop · Issue #351 · purescript-lua/purescript-lua
X Title: Bound the top-down IR rewrite driver's re-application loop · Issue #351 · purescript-lua/purescript-lua
Description: Problem pslua rewrites its intermediate representation (IR) through two drivers, both of which re-apply a rule at a node until it stops firing. One of them, rewriteExpBottomUpM, now carries an iteration budget (maxNestedRewrites, #348): ...
Open Graph Description: Problem pslua rewrites its intermediate representation (IR) through two drivers, both of which re-apply a rule at a node until it stops firing. One of them, rewriteExpBottomUpM, now carries an iter...
X Description: Problem pslua rewrites its intermediate representation (IR) through two drivers, both of which re-apply a rule at a node until it stops firing. One of them, rewriteExpBottomUpM, now carries an iter...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/351
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bound the top-down IR rewrite driver's re-application loop","articleBody":"## Problem\n\n`pslua` rewrites its intermediate representation (IR) through two drivers, both of which re-apply a rule at a node until it stops firing. One of them, `rewriteExpBottomUpM`, now carries an iteration budget (`maxNestedRewrites`, #348): without it, beta reduction livelocked on a term with no normal form. The other, `rewriteExpTopDownM`, still has the unbounded shape:\n\n```haskell\nrewriteExpTopDownM ∷ Monad m ⇒ RewriteRuleM m ann → RawExp ann → m (RawExp ann)\nrewriteExpTopDownM rule = visit\n where\n visit expression =\n rule expression >>= \\case\n Just expression' → visit expression'\n Nothing → traverseOf subexpressions visit expression\n```\n\n`visit` recurses on the rule's own result with nothing to stop it. Its termination argument is a contract on the rules rather than a property of the driver, stated in the driver's own Haddock:\n\n> Termination is the rule's obligation: it must not fire on the root of its own result.\n\nTwo passes use this driver, both of them lowerings that consume a pattern from the outside in, which is why they cannot use the bottom-up driver (it would dismantle a chain from the inside out, since every tail of a magic-do chain is itself a chain head): magic-do, which turns `Effect`/`ST` bind chains into straight-line statements (`Language.PureScript.Backend.IR.MagicDo`), and deep-bind flattening, which lambda-lifts continuation chains and A-normalises application spines (`Language.PureScript.Backend.IR.FlattenDeepBinds`).\n\n## How it shows up\n\nIt does not, today — this is hardening, not a live bug. Neither pass substitutes an arbitrary subterm the way `betaReduce` does, so neither has a known input that makes it fire on its own result, and a sweep of the `IR Optimizer` group over hspec seeds 1..500 is clean.\n\nWhat makes it worth closing anyway is the failure mode. A rule that ever breaks the obligation does not produce a wrong answer or a red test: the compiler stops emitting output and pins one core, exactly as #348 did, and the suite prints nothing at all rather than a failure. That is the hardest failure shape to attribute, and it is one refactor of either pass away.\n\n## Approach\n\nGive the top-down driver the same treatment the bottom-up one received: thread a budget through `visit`, and return the term reached when it runs out. The bottom-up version is the shape to copy —\n\n```haskell\nrewriteExpBottomUpM rule = runWriterT . reapply maxNestedRewrites\n where\n reapply budget = transformMOf subexpressions \\e →\n lift (rule e) >>= \\case\n Nothing → pure e\n Just e' → do\n tell Rewritten\n case budget of\n 0 → pure e'\n _ → reapply (budget - 1) e'\n```\n\n— and the reasoning transfers unchanged: each rewrite preserves semantics and the IR invariants on its own, so a term abandoned mid-rewrite is as valid as a fully rewritten one, and stopping early costs optimization and nothing else.\n\nWhether the two drivers should share one constant is the open question. `maxNestedRewrites` is 100 because the deepest converging chain measured across the whole test suite is 5 (1232 examples, 344 of them golden compilations of real PureScript, the ~300-deep constant chains included). The top-down driver's rules consume chains whose length is the *program's*, not a rewrite's, so their legitimate nesting may well scale with chain depth rather than sit at a small constant — in which case the two want separate constants, and the top-down one wants its own measurement.\n\n## Verification / Measurement\n\nInstrument the driver to record the deepest nesting `visit` ever reaches, then run the full suite and the golden corpus, the same way the bottom-up bound was calibrated. The golden corpus is the population that matters here, since it is real compiled PureScript and includes the deep-chain stress modules the flattening pass exists for. Set the bound at a comfortable multiple of the measured peak.\n\nThe golden corpus must stay byte-identical: a bound above the legitimate peak cannot change any output, so any `golden.ir` or `golden.lua` movement means the bound is too low.\n\n## Prerequisites / Relations\n\nIndependent — #348 has landed, and this is the sibling driver it deliberately left alone (that change stayed scoped to the driver whose divergence was reproducible).\n\n## Acceptance criteria\n\n- `rewriteExpTopDownM` terminates on every input, without relying on a rule-level obligation.\n- The bound is justified by a recorded measurement of the deepest nesting real compilation reaches, not by a guess.\n- The relationship to `maxNestedRewrites` is settled either way: one shared constant, or two with the top-down one's own measurement behind it.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-29T11:18:24Z","url":"https://github.com/purescript-lua/purescript-lua/issues/351"}
| 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:34836f54-84c1-f8d4-d659-775dc8ffd0c2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E994:DA744:661378:8AEA63:6B00D945 |
| html-safe-nonce | 8f7f9a4a82fad1fe7951610c6f3f1132762738162147f6d0211ae6e2d1352ca1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTk0OkRBNzQ0OjY2MTM3ODo4QUVBNjM6NkIwMEQ5NDUiLCJ2aXNpdG9yX2lkIjoiODE3MjY0MTU5NzE1NDk3NDAyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5b291437e0583075df918295cb55e73a67ffab66ab8794c340b872124a752a54 |
| hovercard-subject-tag | issue:5008618813 |
| 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/351/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fe94f1d3f61b68bc69e7475890e92a6d077dd9e979c1716a8be839193d80eaf0/purescript-lua/purescript-lua/issues/351 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fe94f1d3f61b68bc69e7475890e92a6d077dd9e979c1716a8be839193d80eaf0/purescript-lua/purescript-lua/issues/351 |
| og:image:alt | Problem pslua rewrites its intermediate representation (IR) through two drivers, both of which re-apply a rule at a node until it stops firing. One of them, rewriteExpBottomUpM, now carries an iter... |
| 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 | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width