Title: Constant-fold ordering comparisons on ASCII Char literals · Issue #222 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Constant-fold ordering comparisons on ASCII Char literals · Issue #222 · purescript-lua/purescript-lua
X Title: Constant-fold ordering comparisons on ASCII Char literals · Issue #222 · purescript-lua/purescript-lua
Description: What The IR constant folder evaluates equality on Char literals but leaves ordering comparisons (<, <=, >, >=) on Char literals alone, even when both operands are ASCII. Extend it to evaluate ordering on two ASCII Char literals at compil...
Open Graph Description: What The IR constant folder evaluates equality on Char literals but leaves ordering comparisons (<, <=, >, >=) on Char literals alone, even when both operands are ASCII. Extend it to evaluate order...
X Description: What The IR constant folder evaluates equality on Char literals but leaves ordering comparisons (<, <=, >, >=) on Char literals alone, even when both operands are ASCII. Extend it to ev...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/222
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constant-fold ordering comparisons on ASCII Char literals","articleBody":"## What\n\nThe IR constant folder evaluates equality on `Char` literals but leaves ordering comparisons (`\u003c`, `\u003c=`, `>`, `>=`) on `Char` literals alone, even when both operands are ASCII. Extend it to evaluate ordering on two ASCII `Char` literals at compile time.\n\n## Observed\n\nIn `test/ps/output/Golden.CharLiterals.Test/golden.lua`, `show ('\\t' \u003c '\\n')` compiles to a runtime branch on a constant condition:\n\n```lua\nlocal v = \"Data.Ordering∷Ordering.LT\" == (function()\n if \"\\t\" \u003c \"\\n\" then\n return \"Data.Ordering∷Ordering.LT\"\n else\n return \"Data.Ordering∷Ordering.GT\"\n end\nend)()\nif v then return \"true\" elseif false == v then return \"false\" else return error(\"No patterns matched\") end\n```\n\n`\"\\t\" \u003c \"\\n\"` compares two compile-time constants (bytes 9 and 10), so the whole block could fold to `\"true\"`. The sibling `show ('\\n' == '\\n')` one line above already folds to `\"true\"`, which is what makes the unfolded ordering stand out.\n\n## Why it is unfolded today (deliberate)\n\n`numericCompare`, inside `foldPrimBinOp` in `lib/Language/PureScript/Backend/IR/Optimizer.hs`, folds `PrimLt`/`PrimLe`/`PrimGt`/`PrimGe` only for `(LiteralInt, LiteralInt)` and `(LiteralFloat, LiteralFloat)`. Note [Folding primops follows Lua 5.1] records the reason: an IR `Char`/`String` literal carries a semantic Unicode value, but Lua orders strings by bytes, so folding with the host's `Ord` could disagree with the runtime on non-ASCII operands. Equality still folds because byte-equality and codepoint-equality coincide; only ordering is held back.\n\n## The refinement\n\nFor two `Char` literals whose codepoints are both below U+0080, the single-byte Lua representation orders identically to the codepoint, so host-side folding matches the runtime exactly. Gate the new fold on the ASCII range.\n\nNon-ASCII chars and `String` literals stay out of scope. Their byte order can diverge from codepoint order, and strings add length and multi-byte questions on top. They can be a later follow-up if they ever earn it.\n\n## Proposed implementation\n\nAdd one arm to `numericCompare`:\n\n```haskell\n(LiteralChar _ x, LiteralChar _ y)\n | isAscii x, isAscii y → Just (compare x y) -- isAscii from Data.Char\n```\n\nThe existing downstream folds then cascade: `if True then LT else GT` becomes `LT`, `\"…LT\" == \"…LT\"` becomes `True`, `if True then \"true\"` becomes `\"true\"`, collapsing the CharLiterals example to `log(\"true\")()`.\n\n## Test (ASCII-correctness is the point)\n\nWork test-first. The test has to prove two things, not one:\n\n1. An ASCII pair folds: `'\\t' \u003c '\\n'` reduces to a boolean literal.\n2. A non-ASCII pair is left untouched: comparing two chars with codepoints at or above U+0080 does not fold. This is the soundness guard, and it is the reason to write a dedicated test rather than lean on the golden alone.\n\nA focused `Optimizer` unit test covers both cases directly. The `Golden.CharLiterals.Test` golden also moves, since its final `log` collapses to `log(\"true\")()`.\n\n## Payoff\n\nSmall in practice. Two literal chars compared at compile time is rare in real code, so this is a completeness fix rather than a performance one. It surfaced while reviewing #180 (PR #219), where the dictionary-method inlining folded everything around this comparison but not the comparison itself.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-10T09:03:19Z","url":"https://github.com/purescript-lua/purescript-lua/issues/222"}
| 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:f0c7c8fe-88f7-26bc-03f7-ad941d618402 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A57E:76F02:8F0053:BA101E:6B00DFBD |
| html-safe-nonce | 5c55ccbe79d0caf6f9e4c5f0993552e6a0b11455adbc5d12489679ee42506c0b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTdFOjc2RjAyOjhGMDA1MzpCQTEwMUU6NkIwMERGQkQiLCJ2aXNpdG9yX2lkIjoiMjQwMzgyNjI3MTMxMzg1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fb4984098c1abf6222b5981600659b4bba6be9381c861ce99dae2f2e8bf39382 |
| hovercard-subject-tag | issue:4853609653 |
| 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/222/issue_layout |
| twitter:image | https://opengraph.githubassets.com/210f92f9a3721ea4431b638811209d58b7a4feea4285681f42f80b36d953dd96/purescript-lua/purescript-lua/issues/222 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/210f92f9a3721ea4431b638811209d58b7a4feea4285681f42f80b36d953dd96/purescript-lua/purescript-lua/issues/222 |
| og:image:alt | What The IR constant folder evaluates equality on Char literals but leaves ordering comparisons (<, <=, >, >=) on Char literals alone, even when both operands are ASCII. Extend it to evaluate order... |
| 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