Title: Distribute an effect-run application into IfThenElse branches · Issue #266 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Distribute an effect-run application into IfThenElse branches · Issue #266 · purescript-lua/purescript-lua
X Title: Distribute an effect-run application into IfThenElse branches · Issue #266 · purescript-lua/purescript-lua
Description: Problem magicDo wraps a do-block's final action in a run application: App finalAction EffectRunArg. When the final action is a conditional, which is the standard shape of a tail-recursive effect loop (do logShow n; if n <= 1 then log "do...
Open Graph Description: Problem magicDo wraps a do-block's final action in a run application: App finalAction EffectRunArg. When the final action is a conditional, which is the standard shape of a tail-recursive effect lo...
X Description: Problem magicDo wraps a do-block's final action in a run application: App finalAction EffectRunArg. When the final action is a conditional, which is the standard shape of a tail-recursive effec...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/266
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Distribute an effect-run application into IfThenElse branches","articleBody":"## Problem\n\nmagicDo wraps a do-block's final action in a run application: `App finalAction EffectRunArg`. When the final action is a conditional, which is the standard shape of a tail-recursive effect loop (`do logShow n; if n \u003c= 1 then log \"done\" else loop (n - 1)`), the marker applies to the whole `IfThenElse`. Codegen then emits an IIFE that returns the selected action plus a second call that forces it: `(function() if ... then return A else return B end end)()()`. Two costs follow. Each executed conditional tail pays an extra closure and two extra calls, and the branch call `loop (n - 1)` stays partial because the run sits outside the if, so the self-call never becomes a saturated worker call: loopification (#181) sees no tail self-call and cannot rewrite the recursion into `while true`.\n\n## Approach\n\nRewrite `App (IfThenElse c t e) EffectRunArg` to `IfThenElse c (App t EffectRunArg) (App e EffectRunArg)`. Selecting the action first and running it second is the same evaluation order, so the rewrite preserves semantics; it consumes the outer application while adding one marker node per branch, so it terminates and cannot re-fire on its own output. Placed before the late uncurry run (#200), the pushed-in runs saturate the branch spines: `loop(n-1)(run)` becomes `loop$w(n-1, run)`, which the Lua backend emits as the tail call `return M.loop_S_w(n - 1)`, and loopification turns the recursion into a loop.\n\n## Prerequisites / Relations\n\nUnlocks the #181 synergy sketched in #200 for if-terminated effect loops; the `Golden/UncurryEffect` countdown documents today's wrapper round-trip. Nested conditionals distribute recursively.\n\n## Open questions\n\n- Where the rewrite lives: magicDo's `buildThunk` could emit per-branch runs directly, while an optimizer rule would also catch run-of-if shapes from other producers such as ForeignLift.\n- Interaction with the if-folding rules (`removeIfWithEqualBranches`, the boolean folds): distribution strictly consumes the outer run, so fixpoint convergence should hold, but the rule ordering deserves a test.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-13T13:51:40Z","url":"https://github.com/purescript-lua/purescript-lua/issues/266"}
| 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:20629235-9ca9-22e1-a9dd-b99a3b165dba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B06:96DDF:5B4EFA:754541:6B00DB6F |
| html-safe-nonce | 77b4c729720289fe2f17260a8a60d3d55fc0264de6f1e2a9523bc2e34760c419 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjA2Ojk2RERGOjVCNEVGQTo3NTQ1NDE6NkIwMERCNkYiLCJ2aXNpdG9yX2lkIjoiMTI0NTg2MzUwNzc2NTgxMjA3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c113268b34ce45547988f4f9031301c7bdd92e9e7b15da698a348bf28c82b2ea |
| hovercard-subject-tag | issue:4874282898 |
| 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/266/issue_layout |
| twitter:image | https://opengraph.githubassets.com/64f366ab9821ef3b2d13419c3017683a67502764ef90aed8d0cd97314f49e238/purescript-lua/purescript-lua/issues/266 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/64f366ab9821ef3b2d13419c3017683a67502764ef90aed8d0cd97314f49e238/purescript-lua/purescript-lua/issues/266 |
| og:image:alt | Problem magicDo wraps a do-block's final action in a run application: App finalAction EffectRunArg. When the final action is a conditional, which is the standard shape of a tail-recursive effect lo... |
| 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