Title: Fold a let-bound conditional of known constructors into its eliminating reads · Issue #336 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Fold a let-bound conditional of known constructors into its eliminating reads · Issue #336 · purescript-lua/purescript-lua
X Title: Fold a let-bound conditional of known constructors into its eliminating reads · Issue #336 · purescript-lua/purescript-lua
Description: Problem The constructor folds fire on a constructor that is syntactically in place: reduceKnownConstructor matches ReflectCtor (Ctor …) and DataArgumentByIndex i (Ctor …), and propagateKnownCtorThroughLet (#214) extends this through a St...
Open Graph Description: Problem The constructor folds fire on a constructor that is syntactically in place: reduceKnownConstructor matches ReflectCtor (Ctor …) and DataArgumentByIndex i (Ctor …), and propagateKnownCtorThr...
X Description: Problem The constructor folds fire on a constructor that is syntactically in place: reduceKnownConstructor matches ReflectCtor (Ctor …) and DataArgumentByIndex i (Ctor …), and propagateKnownCtorThr...
Opengraph URL: https://github.com/purescript-lua/purescript-lua/issues/336
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Fold a let-bound conditional of known constructors into its eliminating reads","articleBody":"## Problem\n\nThe constructor folds fire on a constructor that is syntactically in place: `reduceKnownConstructor` matches `ReflectCtor (Ctor …)` and `DataArgumentByIndex i (Ctor …)`, and `propagateKnownCtorThroughLet` (#214) extends this through a `Standalone` Let whose right-hand side is a saturated `Ctor` read only through constructor-eliminating reads. A *conditional* of known constructors slips through both. `case (if b then Just x else Just y) of …` let-binds the scrutinee (it is read more than once: a tag test plus a field read), so the eliminating reads see `ReflectCtor (Ref v)` and `DataArgumentByIndex 0 (Ref v)` with `v = IfThenElse b (Ctor Just [x]) (Ctor Just [y])` — a Ref, never the conditional of `Ctor`s — and nothing folds. The #243 distribution (`pushEliminatorIntoIfBranches`) does not reach it either: it fires on an eliminator whose scrutinee is *directly* an `IfThenElse`, not one hidden behind a Let binder. The runtime cost is the un-folded shape: both arms allocate a constructor table only for the tag to be read back off and the payload extracted.\n\n## Approach\n\nAn if-of-ctors sibling of `propagateKnownCtorThroughLet`: when a Let binder's RHS is an `IfThenElse` whose arms are saturated `Ctor` applications (of the same algebraic type) and the binder is read only through constructor-eliminating reads, distribute the reads arm-wise — e.g. rewrite the body's `ReflectCtor v` / `DataArgumentByIndex i v` occurrences under a per-arm binding, or equivalently push the whole Let body into the conditional when the duplication is admissible. The duplication concern is the same one #243 sidesteps: the body appears once per arm, so the safe subset needs either a cheapness gate on the body or the join points of #234, which is what makes the unrestricted version out of scope today.\n\n## Prerequisites / Relations\n\nSharpest after #234 (join points would remove the duplication constraint entirely); a gated subset (trivial bodies, or single eliminating read) can ship without it. Relates to #243 (the direct-scrutinee distribution), #214 (the Let-propagation machinery this would extend), and #206/#208 (whose specializations produce exactly such conditional-of-constructor RHSs at StateT-style boundaries).\n\n## Verification / Measurement\n\nA focused unit test: `let v = if b then Just x else Just y in (tag test + field read of v)` folds to a conditional over the extracted payloads with no `Ctor` allocation left; a non-eliminating extra read of `v` declines. A golden showing the allocation disappear from the `.lua`, with eval output unchanged.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-28T11:54:08Z","url":"https://github.com/purescript-lua/purescript-lua/issues/336"}
| 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:6b0bdadf-21c0-b334-1118-925db2f2247a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1D2:14F601:8724A1:AF4943:6B00D942 |
| html-safe-nonce | 05149fe4ae345b8addcb8b674325f28a2b5c5aee320f038d3772b4272f20fe7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUQyOjE0RjYwMTo4NzI0QTE6QUY0OTQzOjZCMDBEOTQyIiwidmlzaXRvcl9pZCI6IjQ1MTE2OTM4OTQ0NjY5ODQyNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2e7c09a22b3adfa9351077101d63c84370fae82beb615253b05cd1706ad45e3b |
| hovercard-subject-tag | issue:4998242107 |
| 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/336/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a5129aef21f71f5b18df400155eacc4ccc357fcc1d214b18eb1d7068f867a32f/purescript-lua/purescript-lua/issues/336 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a5129aef21f71f5b18df400155eacc4ccc357fcc1d214b18eb1d7068f867a32f/purescript-lua/purescript-lua/issues/336 |
| og:image:alt | Problem The constructor folds fire on a constructor that is syntactically in place: reduceKnownConstructor matches ReflectCtor (Ctor …) and DataArgumentByIndex i (Ctor …), and propagateKnownCtorThr... |
| 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 | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width