Skip to content

WIP: Introduce with# primop (option B)

Ben Gamari requested to merge wip/with2-primop into master

This introduces a new primop, with#, which addresses the problems suffered by touch#, as described in #17760 (closed) and https://gitlab.haskell.org/ghc/ghc/wikis/proposal/with-combinator.

The approach taken here is what is labelled as Option B on the Wiki page. Specifically, the plan is to retain touch# but heavily discourage its use by user code. We then introduce with# which gets rewritten into touch# during Core Prep. Specifically, when we see an application of the form:

with# x k s

We rewrite it to

case k s of (# s', y #) ->
case touch# x s' of s'' ->
(# s'', y #)

This ensures that the simplifier cannot drop the continuation containing the touch# if k diverges (which was the cause of #14346 (closed) and #17746). Moreover, by simply lowering with# to touch# we avoid incurring any runtime overhead (unlike the other options presented on the Wiki page).

TODO

  • Verify that this doesn't incur any unexpected runtime costs
  • Add tests
  • Finish writing Note
  • Add changelog entry
  • Note change in release notes
  • Discourage use of touch# in Haddocks
  • Document with#
Edited by Ben Gamari

Merge request reports