René's URL Explorer Experiment


Title: Lower self-recursive tail calls to while loops (loopification) by Unisay · Pull Request #205 · purescript-lua/purescript-lua · GitHub

Open Graph Title: Lower self-recursive tail calls to while loops (loopification) by Unisay · Pull Request #205 · purescript-lua/purescript-lua

X Title: Lower self-recursive tail calls to while loops (loopification) by Unisay · Pull Request #205 · purescript-lua/purescript-lua

Description: Closes #181. What A self-recursive tail call now lowers to a while true do loop with simultaneous multiple assignment of the parameters. return go$w(e1, e2) in tail position becomes p1, p2 = e1, e2 followed by fall-through to the end of the loop body; every other exit path keeps its return, which leaves the loop. The transform lives in the Lua code generator (new module Language.PureScript.Backend.Lua.Loopify), applied to recursive-group bindings at the two places they are lowered: top-level groups (self-reference through the M table) and Let-bound groups (plain local). The expression IR keeps no loop node, the same choice magicDo makes for statements. Foreign code never passes through it. Tail positions are found by walking the block spine of the generated function: the final statement of the body and, recursively, the final statement of each branch of a block-final if. This is exactly where the code generator puts IR tail positions (through Let bodies and IfThenElse branches), and it is what makes fall-through work as "continue" on a target without goto. Two things fell out beyond the issue text: Plain unary self-recursions loopify too, without needing a worker/wrapper split: countdown n = ... countdown (n - 1), and structural recursions like bat (Succ n) = bat n (which becomes n = n.value0). Only the tail self-calls need rewriting. Non-tail self-calls and the wrapper indirection stay real calls, each starting a fresh activation with its own loop. McCarthy 91 in the golden shows both in one body. Correctness guards Capture veto. Lua closures capture variables by reference, and parameters of a loopified function are shared across iterations, so a closure created in one iteration and surviving into the next would observe the reassigned values. A body that references a parameter from inside a nested function literal is therefore not loopified. Immediately-invoked scope wrappers (function() ... end)() are looked through transparently; without that, the comparison predicates the case compiler emits (IIFEs reading the scrutinee) would veto nearly everything. The golden's CPS accumulator (sumCPS n k = ... sumCPS (n - 1) (\r -> k (r + n))) pins the veto: it stays recursive, and the hand-written eval oracle would catch the corruption if it ever loopified. Explist balancing. The assignment's right-hand explist is balanced to the variable count, because the code generator drops trailing unused parameters and trailing Prim.undefined arguments, so the two lists can disagree in length. A short list is padded with explicit nils (declined when the last argument is a call or vararg, whose multiple results the pad would truncate where the call form spreads them); surplus expressions facing dropped parameters are removed when syntactically pure, and decline the rewrite otherwise. This also keeps luacheck's unbalanced-assignment check quiet. Tests New golden Golden.Loopification.Test with six shapes (unary countdown, binary accumulator, local where-bound worker, McCarthy 91, CPS capture veto, dead trailing parameter) and a hand-written eval oracle. Nine unit tests in Lua.Spec cover the decision points through fromUberModule: top-level and Let-bound groups, non-tail calls, the capture veto, and all four explist-balancing outcomes. Four existing goldens moved: Uncurry (the sumTo worker), LongCallbackChain, PatternMatching.Test2, and StringCodePoints (the library's codePointAtFallback worker loopifies). golden.ir files are untouched, since the transform is strictly Lua-side. All eval oracles pass unchanged. Measurements On curried_step (n=2e6, medians): PUC Lua 0.363s to 0.348s, the modest constant-factor win the issue predicted, since the loop body is still dominated by curried FFI closure allocation. Under LuaJIT the loop is blacklisted (IFUNCF to ILOOP in the accepted trace report) because those same per-iteration FNEWs abort trace recording, and the benchmark regresses from 0.196s to 0.366s for now. That interim regression should flip once #178/#180/#186 remove the allocations from loop bodies; it is tracked in #204.

Open Graph Description: Closes #181. What A self-recursive tail call now lowers to a while true do loop with simultaneous multiple assignment of the parameters. return go$w(e1, e2) in tail position becomes p1, p2 = e1, e2...

X Description: Closes #181. What A self-recursive tail call now lowers to a while true do loop with simultaneous multiple assignment of the parameters. return go$w(e1, e2) in tail position becomes p1, p2 = e1, e2...

Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/205

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,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/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:6aa48444-35be-7de0-3543-d4f96a88ae9f
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idBD52:2E124E:910BD0:BA9A91:6B00EF99
html-safe-noncefe3aad1f10082a13488223b95a4c8a47e59feb37df1edc2d9b4410cf2c321a9e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDUyOjJFMTI0RTo5MTBCRDA6QkE5QTkxOjZCMDBFRjk5IiwidmlzaXRvcl9pZCI6IjUyODUwMDY0NjY5NjM1MzM3MjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac3d66ada9f7f1c95f5a7858737ab664f1a59c5a26708c1b665e2964dbeb0a88f2
hovercard-subject-tagpull_request:4009497751
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/purescript-lua/purescript-lua/pull/205/files
twitter:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1009751?s=400&v=4
og:image:altCloses #181. What A self-recursive tail call now lowers to a while true do loop with simultaneous multiple assignment of the parameters. return go$w(e1, e2) in tail position becomes p1, p2 = e1, e2...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/purescript-lua/purescript-lua git https://github.com/purescript-lua/purescript-lua.git
octolytics-dimension-user_id131581790
octolytics-dimension-user_loginpurescript-lua
octolytics-dimension-repository_id587200487
octolytics-dimension-repository_nwopurescript-lua/purescript-lua
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587200487
octolytics-dimension-repository_network_root_nwopurescript-lua/purescript-lua
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release9983dd5da06943f833c971dd5b6bc988e2858be5
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/purescript-lua/purescript-lua/pull/205/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F205%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpurescript-lua%2Fpurescript-lua%2Fpull%2F205%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=purescript-lua%2Fpurescript-lua
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/205/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/205/files
Reloadhttps://github.com/purescript-lua/purescript-lua/pull/205/files
purescript-luahttps://github.com/purescript-lua
purescript-luahttps://github.com/purescript-lua/purescript-lua
Codehttps://github.com/purescript-lua/purescript-lua
Issues48 (48)https://github.com/purescript-lua/purescript-lua/issues
Pull requestshttps://github.com/purescript-lua/purescript-lua/pulls
Discussionshttps://github.com/purescript-lua/purescript-lua/discussions
Actionshttps://github.com/purescript-lua/purescript-lua/actions
Projectshttps://github.com/purescript-lua/purescript-lua/projects
Security and qualityhttps://github.com/purescript-lua/purescript-lua/security
Insightshttps://github.com/purescript-lua/purescript-lua/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpurescript-lua%2Fpurescript-lua%2Fissues%2Fnew%2Fchoose
Unisayhttps://github.com/Unisay
mainhttps://github.com/purescript-lua/purescript-lua/tree/main
issue-181/loopificationhttps://github.com/purescript-lua/purescript-lua/tree/issue-181/loopification
Conversation 1 https://github.com/purescript-lua/purescript-lua/pull/205
Commits 3 https://github.com/purescript-lua/purescript-lua/pull/205/commits
Checks 2 https://github.com/purescript-lua/purescript-lua/pull/205/checks
Files changed https://github.com/purescript-lua/purescript-lua/pull/205/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/205/files
Lower self-recursive tail calls to while loops (loopification) https://github.com/purescript-lua/purescript-lua/pull/205/files#top
Show all changes 3 commits https://github.com/purescript-lua/purescript-lua/pull/205/files
6d3ebba feat: lower self-recursive tail calls to while loops (#181) Unisay Jul 7, 2026 https://github.com/purescript-lua/purescript-lua/pull/205/commits/6d3ebba220717906e598743cf8497f80584bd5c2
a1b7a53 test: golden and unit coverage for loopification Unisay Jul 7, 2026 https://github.com/purescript-lua/purescript-lua/pull/205/commits/a1b7a53df6ba9d9d068d8e339c7b35eaa1f4997f
2f44a0a bench: accept counter goldens for the loopified output Unisay Jul 7, 2026 https://github.com/purescript-lua/purescript-lua/pull/205/commits/2f44a0a4b2bf2660625c6a315df0c5f154b3f322
Clear filters https://github.com/purescript-lua/purescript-lua/pull/205/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/205/files
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/205/files
fnew_Bench.CurriedStep.txt https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-9de4e16b2ce92ac7a937d19a7f41550431e0a7734850f1a7d2d5b4544f5dcdfe
trace_curried_step.txt https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-cffe8a9ca26527d8aa503cf24b37bbd80e42185b7f1d8c98b82deb500073ce49
20260707_220000_unisay_loopification.md https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-f263bc41ea13f90206fdbe71ed375344127146b08c0dda91e5989dc5022c45ff
Lua.hs https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
Loopify.hs https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-2b8347ddf2793d46cc24d232c8b2aba1f2f3757c2aee6275444964b7718021ac
pslua.cabal https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-cf0302393cf6443df4429c19c0fc326753a83405d0366075a98ac2753e5e1ab2
Spec.hs https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-77712c610ee12122e63661289b0f15ab3392c5fc044dbbbf84b63e1436240266
golden.lua https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-0f80457c0b35d0736c4041d3b36ae67c88c3cce1cb9d8f8114bb21515291c8e2
corefn.json https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-8cb159d8d74a544fce1faac8a91793c11c4359dc8cbbee34dd82c36be8d73195
.gitignore https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-fffeb9f36c63e4666751e6f61905c78483b3e9578fc28ef6342c51f2ce4da261
golden.txt https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-b0a803fe1443b0d6acbb8217e500845a42018b2d2e19a3fa3c33376fdf04263c
golden.ir https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-c87daf3e43a53dd52fa264f09901b86021b6ee624f9da16bea90707758b8abfe
golden.lua https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-7a0f829c8097adffc9b5bff2f03fd5a9a7d69ed4d2e1719bd21e8c484253266d
golden.lua https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-579dad171afe8a5e04a0ebb3ca706532b1f14c969e19f9c2550ecd29b5ab7dad
golden.lua https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-8f846c962c84e2efb0046e7848c15b3bac3e044be37a2d2ee0620f823f2e9b0f
golden.lua https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-12b1d0aa4433bf963d1b2b3685ea3ec7726991cd5b797cdb7c8ce480aa7d24dd
Test.purs https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-bc9cbce95cda910414bb097b05529e812a36850079b01c371c2157c580561321
bench/goldens/fnew_Bench.CurriedStep.txthttps://github.com/purescript-lua/purescript-lua/pull/205/files#diff-9de4e16b2ce92ac7a937d19a7f41550431e0a7734850f1a7d2d5b4544f5dcdfe
View file https://github.com/purescript-lua/purescript-lua/blob/2f44a0a4b2bf2660625c6a315df0c5f154b3f322/bench/goldens/fnew_Bench.CurriedStep.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/205/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-9de4e16b2ce92ac7a937d19a7f41550431e0a7734850f1a7d2d5b4544f5dcdfe
bench/goldens/trace_curried_step.txthttps://github.com/purescript-lua/purescript-lua/pull/205/files#diff-cffe8a9ca26527d8aa503cf24b37bbd80e42185b7f1d8c98b82deb500073ce49
View file https://github.com/purescript-lua/purescript-lua/blob/2f44a0a4b2bf2660625c6a315df0c5f154b3f322/bench/goldens/trace_curried_step.txt
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/205/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-cffe8a9ca26527d8aa503cf24b37bbd80e42185b7f1d8c98b82deb500073ce49
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-cffe8a9ca26527d8aa503cf24b37bbd80e42185b7f1d8c98b82deb500073ce49
changelog.d/20260707_220000_unisay_loopification.mdhttps://github.com/purescript-lua/purescript-lua/pull/205/files#diff-f263bc41ea13f90206fdbe71ed375344127146b08c0dda91e5989dc5022c45ff
View file https://github.com/purescript-lua/purescript-lua/blob/2f44a0a4b2bf2660625c6a315df0c5f154b3f322/changelog.d/20260707_220000_unisay_loopification.md
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/205/{{ revealButtonHref }}
lib/Language/PureScript/Backend/Lua.hshttps://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
View file https://github.com/purescript-lua/purescript-lua/blob/2f44a0a4b2bf2660625c6a315df0c5f154b3f322/lib/Language/PureScript/Backend/Lua.hs
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/purescript-lua/purescript-lua/pull/205/{{ revealButtonHref }}
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
https://github.com/purescript-lua/purescript-lua/pull/205/files#diff-11295f6f97401e5cc1fdfa2a8caa3fc80edc56cd58262d3de010e803f8d3640f
Please reload this pagehttps://github.com/purescript-lua/purescript-lua/pull/205/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.