Title: feat(optimizer): collapse Boolean pattern matches to a two-way if (#223) by Unisay · Pull Request #290 · purescript-lua/purescript-lua · GitHub
Open Graph Title: feat(optimizer): collapse Boolean pattern matches to a two-way if (#223) by Unisay · Pull Request #290 · purescript-lua/purescript-lua
X Title: feat(optimizer): collapse Boolean pattern matches to a two-way if (#223) by Unisay · Pull Request #290 · purescript-lua/purescript-lua
Description: Closes #223. What A pattern match on a Boolean compiled to a three-way if with a synthesized, unreachable default — the shape every inlined show or logShow of a boolean produces: if a then return "true" elseif false == a then return "false" else return error("No patterns matched") end a is a Boolean, so true/false already cover it, but no existing rule had a handle on the shape: constantFolding knew only the left-literal true == b case, and the unreachable-branch rules need a literal condition. It now reduces to: if a then return "true" else return "false" end How Both pieces proposed in the issue, reusing the existing folds for the actual collapse: The boolean-equality fold in constantFolding is completed. false == b and b == false fold to not b, and b == true folds to b, mirroring the existing left-literal true == b case (sound per Note [IR is assumed well-typed]). New rule propagateKnownCondIntoBranches — the Boolean sibling of propagateKnownCtorThroughLet, the more general of the issue's two alternatives. Inside if c then t else e a variable condition c is true throughout t and false throughout e, so its free occurrences in the branches are replaced with the matching literal; the existing folds then finish the job: if c then "true" else (if (false == c) then "false" else Exc) -- propagation: in the else branch, c is known false if c then "true" else (if (false == false) then "false" else Exc) -- constantFolding (existing): false == false → true if c then "true" else (if true then "false" else Exc) -- removeUnreachableElseBranch (existing) if c then "true" else "false" The rule fires only on a variable condition (re-reading an immutable binding is free and yields the value the test just observed), leaves no free occurrence behind so it cannot re-fire on its own result, and never grows the tree (a Ref becomes a literal node-for-node). It is placed after removeIfWithEqualBranches in the rewrite chain: branches that are equal while still naming c collapse to a single copy, which substituting the two literals first would unequalize — a unit test pins this ordering. Tests Written test-first (red before the fix): unit tests pin the three new equality folds, the end-to-end three-way→two-way collapse, then-branch propagation (if a then a else e → a or e), and the guards — a genuinely partial match keeps its default, and a guard testing a different variable keeps the error arm. The randomized IR specs pass across 5 extra seeds. Goldens The elseif false == idiom disappears from every golden that carried it (9 sites across 6 goldens at current main; the issue counted 16 against an older corpus). Sample from Golden.RecursiveBindings.Test: yes_S_0 = function(v_S_2) - if v_S_2 then - return no_S_1(false) - elseif false == v_S_2 then - return no_S_1(true) - else - return error("No patterns matched") - end + if v_S_2 then return no_S_1(false) else return no_S_1(true) end end The collapse also cascades: the shrunk boolean show bodies now clear the call-site inline budget, so logShow pastes at its call sites in the GenericEq/NumberIsNaN goldens, and in Golden.NumberIsNaN.Test that dead-codes the whole Ring dictionary: -local Data_Semiring_foreign = { - numAdd = function(x) return function(y) return x + y end end, - numMul = function(x) return function(y) return x * y end end -} ... -local Data_Ring_sub = function(dict) return dict.sub end -local Data_Ring_ringNumber = { - sub = Data_Ring_foreign.numSub, - Semiring0 = function() - return { - add = Data_Semiring_foreign.numAdd, - zero = 0.0, - mul = Data_Semiring_foreign.numMul, - one = 1.0 - } - end -} Golden.GenericEqTwoTypes.Test keeps its 4 remaining error("No patterns matched") defaults — those are constructor-tag matches on closed sums, explicitly out of scope per the issue. Eval goldens are unchanged, which is the semantic safety net: runtime output is identical, only the shape moved.
Open Graph Description: Closes #223. What A pattern match on a Boolean compiled to a three-way if with a synthesized, unreachable default — the shape every inlined show or logShow of a boolean produces: if a then return...
X Description: Closes #223. What A pattern match on a Boolean compiled to a three-way if with a synthesized, unreachable default — the shape every inlined show or logShow of a boolean produces: if a then return...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/pull/290
X: @github
Domain: github.com
| 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/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:08b42ed5-c04b-ee16-3482-dc6d831538a2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AFDA:2463BD:C7E65D:10254DC:6B00EF99 |
| html-safe-nonce | 1bb0b688d5ebfe5b7ab3435a49741db99c3430588bfa2f52610ea949603287d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkRBOjI0NjNCRDpDN0U2NUQ6MTAyNTREQzo2QjAwRUY5OSIsInZpc2l0b3JfaWQiOiI1MTAzODM0NjM2MDY1MjM4OTM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 71688074d85031c004988e19382a708e3e2b2230fd6b116b9b280b3f337b0db0 |
| hovercard-subject-tag | pull_request:4116515981 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/pull/290/files |
| twitter:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1009751?s=400&v=4 |
| og:image:alt | Closes #223. What A pattern match on a Boolean compiled to a three-way if with a synthesized, unreachable default — the shape every inlined show or logShow of a boolean produces: if a then return... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width