Title: Collapse Boolean pattern matches (including inlined show) to a two-way if · Issue #223 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Collapse Boolean pattern matches (including inlined show) to a two-way if · Issue #223 · purescript-lua/purescript-lua
X Title: Collapse Boolean pattern matches (including inlined show) to a two-way if · Issue #223 · purescript-lua/purescript-lua
Description: What A pattern match on a Boolean compiles to a three-way if with a synthesized, unreachable default: if a then return "true" elseif false == a then return "false" else return error("No patterns matched") end a is a Boolean, so true/fals...
Open Graph Description: What A pattern match on a Boolean compiles to a three-way if with a synthesized, unreachable default: if a then return "true" elseif false == a then return "false" else return error("No patterns ma...
X Description: What A pattern match on a Boolean compiles to a three-way if with a synthesized, unreachable default: if a then return "true" elseif false == a then return "false" else return e...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/223
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Collapse Boolean pattern matches (including inlined show) to a two-way if","articleBody":"## What\n\nA pattern match on a `Boolean` compiles to a three-way `if` with a synthesized, unreachable default:\n\n```lua\nif a then\n return \"true\"\nelseif false == a then\n return \"false\"\nelse\n return error(\"No patterns matched\")\nend\n```\n\n`a` is a `Boolean`, so `true`/`false` already cover it. The `elseif false == a` guard is always taken once reached, and the `else` default is dead. The whole thing should reduce to:\n\n```lua\nif a then return \"true\" else return \"false\" end\n```\n\nThe canonical source is the `Show Boolean` instance, so this is the shape every inlined `show` or `logShow` of a boolean produces. Since #180 inlines the boolean `show` dictionary at call sites, it now appears more often.\n\n## Where\n\nThe idiom (signature `elseif false ==`) occurs 16 times across 6 goldens:\n\n- `Golden.RecursiveBindings.Test`: 4\n- `Golden.GenericEqTwoTypes.Test`: 4\n- `Golden.Primops.Test`: 3\n- `Golden.BugListGenericEq.Test`: 3\n- `Golden.StringCodePoints.Test`: 1\n- `Golden.CharLiterals.Test`: 1\n\n## Why it is not folded today\n\nThe IR shape is:\n\n```\nIfThenElse c \"true\" (IfThenElse (Eq (LitBool False) c) \"false\" (Exception \"No patterns matched\"))\n```\n\nNone of the existing if/boolean rules apply:\n\n- `reduceBooleanIf` (#178) collapses `if p then True else False`, but here the branches are the strings `\"true\"`/`\"false\"`, not boolean literals.\n- `constantFolding` folds `Eq True b → b` but has no `Eq False b → not b` counterpart, so `false == c` stays opaque.\n- `removeUnreachableThenBranch` / `removeUnreachableElseBranch` need a literal `if True` / `if False`.\n\nSo the optimizer has no handle on the shape and prints it verbatim.\n\n## Proposed transformations\n\nTwo pieces, both reusing the existing folds.\n\n1. Complete the boolean-equality fold in `constantFolding` (cheap and independently useful):\n\n```haskell\nEq _ (LiteralBool _ False) b → Just (primNot b) -- b : Bool, see Note [IR is assumed well-typed]\nEq _ b (LiteralBool _ False) → Just (primNot b)\nEq _ b (LiteralBool _ True) → Just b -- mirror the existing left-literal case\n```\n\n2. Propagate a branch condition's known value into its branches, the Boolean sibling of the existing `propagateKnownCtorThroughLet`: inside `if c then t else e`, `c` is `True` throughout `t` and `False` throughout `e`. Substituting `c := False` into the else branch lets the existing folds finish the job:\n\n```\nif c then \"true\" else (if (False == c) then \"false\" else Exc)\n -- propagation: in the else branch, c is known False\nif c then \"true\" else (if (False == False) then \"false\" else Exc)\n -- constantFolding (existing): Eq (LitBool False) (LitBool False) → True\nif c then \"true\" else (if True then \"false\" else Exc)\n -- removeUnreachableElseBranch (existing)\nif c then \"true\" else \"false\"\n```\n\nA smaller, more local alternative to (2) is a dominated-condition rule, `if c then t else (if c then _dead else e) → if c then t else e`, which composes with the existing `flipNegatedIf` (after fold (1) turns `false == c` into `not c`) to the same result. Either works. Propagation is the more general choice and matches the constructor propagation the optimizer already does; the dominated-condition rule is the lower-risk one. Both need `c` to be a variable so re-testing it is free and sound.\n\n## Test\n\nWork test-first. A focused `Optimizer` unit test on the IR shape above proves it reduces to the two-way if, including a guard that a genuinely partial match keeps its default. The 16 goldens listed also shrink, since the `elseif false ==` guard and the `error` default disappear. The eval goldens do not move, which is the semantic safety net: runtime output is identical, only the shape changes.\n\n## Out of scope\n\nConstructor-tag matches on closed sums produce a similar dead `error(\"No patterns matched\")` default (the other ~48 of the 64 such defaults in the corpus, for example `genericEqPrime` in `Golden.BugListGenericEq.Test`). Eliminating those is a separate, larger change: proving a tag chain exhaustive needs the complete constructor set per type, which the IR and `UberModule` do not carry today (a `Ctor` node knows its own `TyName` and `CtorName`, not its siblings). That is a plumbing feature rather than a local rewrite, tracked on its own.\n\n## Payoff\n\nModest per site but recurring. This is the shape of every `show` or `logShow` of a boolean, so it turns up in real code as well as across the eval-heavy goldens, and it removes a dead `error` call from otherwise-total code.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-10T09:22:02Z","url":"https://github.com/purescript-lua/purescript-lua/issues/223"}
| 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:d5e04b54-4210-e9b0-cfee-653052a8ea82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BDF2:14700E:66F3AE:84935C:6B00DFBD |
| html-safe-nonce | 6aad8f7b1a2d28e139e320338142f0fb2b92f27cb8c6f0b5741da97a2b20bdf9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREYyOjE0NzAwRTo2NkYzQUU6ODQ5MzVDOjZCMDBERkJEIiwidmlzaXRvcl9pZCI6IjQ3MjE4MTYxNDk5OTI0NjQzMTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ab86c6456594109b6a0f670632958eeff2ff4f3a9c112013c4ffc574e152446a |
| hovercard-subject-tag | issue:4853744740 |
| 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/223/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5a8e6936f482396067ce7647955bf61488214b704983f541ffcfb6149339ea2/purescript-lua/purescript-lua/issues/223 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5a8e6936f482396067ce7647955bf61488214b704983f541ffcfb6149339ea2/purescript-lua/purescript-lua/issues/223 |
| og:image:alt | What A pattern match on a Boolean compiles to a three-way if with a synthesized, unreachable default: if a then return "true" elseif false == a then return "false" else return error("No patterns ma... |
| 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