{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreigzl2ggoea7swftr2uzzxinm5ilknvmyco3xn2r4jv3lpnyruy3pi",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mplaojzvl7l2"
},
"path": "/t/ghc-proposal-top-level-io-initialized-bindings/14328?page=2#post_23",
"publishedAt": "2026-07-01T09:05:04.000Z",
"site": "https://discourse.haskell.org",
"textContent": "I love the goal of this proposal, but I don’t think it’s approaching the problem from quite the right angle.\n\nMost of the problems and now additional complexities it brings are a result of having a top-level version of `unsafePerformIO`, but the `unsafePerformIO` bit is really incidental to the original motivation.\n\nWhat this is really trying to do is come up with a way of sharing the result of a top-level `IO` computation. Stripping away the `IO` is _currently_ the implementation for that because it works well for `newIORef`, but that’s very much not the case for arbitrary `IO`.\n\nSo what if instead of restricting it to a “safe” subset of `IO`, a `%TopLevelIO` declaration just kept the `IO` type and desugared into (something equivalent to) this\n\n\n %TopLevelIO\n blah :: IO Blurg\n blah = someEffectfulFunction\n\n ~>\n\n blah_impl :: Blurg\n blah_impl = unsafePerformIO someEffectfulFunction\n {-# OPAQUE blah_impl #-}\n\n blah :: IO Blurg\n blah = pure $! blah_impl\n\n\nThis way the only observable difference compared to not having the `%TopLevelIO` annotation would be that running the computation twice wouldn’t do anything, but we’re already in IO so that’s a perfectly acceptable behavior.\n\nIn order to do anything useful with the resulting `IORef`, you will have to be in `IO` anyway, so this shouldn’t lose much expressiveness (and if you really need the result in pure code, you can always use `unsafePerformIO blah` and deal with the consequences of that `unsafePerformIO` yourself)\n\n<bikeshed>\nIn this case, a better name would probably be something like `%SharedIO` or `%MemoizedIO`\n</bikeshed>",
"title": "GHC Proposal: Top-level IO initialized bindings"
}