Title: Handwritten semantic-rewrite layer for record surgery and identity foreigns · Issue #236 · purescript-lua/purescript-lua · GitHub
Open Graph Title: Handwritten semantic-rewrite layer for record surgery and identity foreigns · Issue #236 · purescript-lua/purescript-lua
X Title: Handwritten semantic-rewrite layer for record surgery and identity foreigns · Issue #236 · purescript-lua/purescript-lua
Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
Open Graph Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
X Description: Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub.
Opengraph URL: https://github.com/purescript-lua/purescript-lua
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Handwritten semantic-rewrite layer for record surgery and identity foreigns","articleBody":"## Problem\n\nThe source-derived `ForeignLift` (#178) is the right tool for scalar primops and uncurried wrappers: it reads the real FFI source, so the registry cannot drift from the package. But a class of higher-level rewrites cannot be expressed as a lift of FFI source: record surgery (`Record.Builder.unsafeInsert`/`Delete`/`Modify`, `Record.Unsafe.unsafeGet`/`Set`/`Has`, `unsafeUnionFn`) and identity foreigns (`unsafeCoerce a → a`, `Record.Builder.copyRecord r → r`). A small handwritten registry folds these: when the record is statically known, the surgery collapses into a record literal or update, and the identity coercions vanish. On the Lua target, where a table allocation is expensive, folding record surgery on a known record removes a real allocation.\n\n## Approach\n\nA narrow handwritten semantic layer on top of `ForeignLift`, keyed by qualified name and spine shape, kept small. When the record operand is a manifest literal or update, rewrite the surgery into the resulting literal/update; rewrite `unsafeCoerce`/`copyRecord` to their argument unconditionally. Anything not statically known is left as a call. This stays strictly complementary to the source-derived lift: no scalar or uncurried rewrites move here.\n\n## Prerequisites / Relations\n\nIndependent. Complements `ForeignLift` (#178) rather than replacing it, and relates to the allowlist-extension direction. Ordering fusion (`compare … is LT → \u003c`) is deliberately out of scope: it is already covered by #180 (Ordering-constructor collapse through the if-tree) plus #220 and #222.\n\n## Verification / Measurement\n\n`unsafeInsert \"k\" v \u003crecord literal>` folds to a record literal carrying the new field, and `unsafeCoerce x` to `x`, in focused optimizer tests; a surgery on an unknown record is left as a call. Allocation-counter drop on a record-builder benchmark through #172; eval goldens unchanged.\n","author":{"url":"https://github.com/Unisay","@type":"Person","name":"Unisay"},"datePublished":"2026-07-11T12:02:45Z","url":"https://github.com/purescript-lua/purescript-lua/issues/236"}
| 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/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:560b2941-6254-2ebe-eb5b-a705a613437a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DD4:26ECEB:9B86EE:C9966D:6B00E044 |
| html-safe-nonce | d7318b8a0d2280046a78ccccb509c0d5cc450c71b1b8100730096a5988935fcc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REQ0OjI2RUNFQjo5Qjg2RUU6Qzk5NjZEOjZCMDBFMDQ0IiwidmlzaXRvcl9pZCI6IjU0NDcyNDU1NTM3NDAwNzkxNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eb3414fcabd95103b8ff1ae1937f2d9acbdab7b4798f559664d389ed3a426747 |
| hovercard-subject-tag | repository:587200487 |
| 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/purescript-lua/purescript-lua/issues/236 |
| twitter:image | https://opengraph.githubassets.com/638e8137a878305176b6a64ef473983d87906a525f956c0c5c3b1c72df4432e4/purescript-lua/purescript-lua |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/638e8137a878305176b6a64ef473983d87906a525f956c0c5c3b1c72df4432e4/purescript-lua/purescript-lua |
| og:image:alt | Purescript compiler back-end for Lua. Contribute to purescript-lua/purescript-lua development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-cache |
| 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