Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonadReader instance for ContT has bad semantics #83

Open
KingoftheHomeless opened this issue Oct 14, 2020 · 1 comment
Open

MonadReader instance for ContT has bad semantics #83

KingoftheHomeless opened this issue Oct 14, 2020 · 1 comment
Milestone

Comments

@KingoftheHomeless
Copy link

KingoftheHomeless commented Oct 14, 2020

We'd expect local id == id, but this isn't true for ContT.

Let:

localTheCont :: MonadReader Int m => ContT r m ()
localTheCont = ContT $ \c -> local (+1) (c ())

test1 :: MonadReader Int m => ContT r m Int
test1 = localTheCont >> ask

test2 :: MonadReader Int m => ContT r m Int
test2 = local id localTheCont >> ask

then

runReader (evalContT test1) 1 == 2
runReader (evalContT test2) 1 == 1

Breaking apart the instance reveals what goes wrong:

  local id localTheCont
= ContC $ \c ->
    i <- ask
    local id $ runContC localTheCont (local (const i) . c)
= ContC $ \c ->
    i <- ask
    local id $ local (+1) ((local (const i) . c) ())
= ContC $ \c ->
    i <- ask
    local (const i . (+1) . id) (c ())
= ContC $ \c ->
    i <- ask
    local (const i) (c ())

Removing the instance would undoubtedly cause too much breakage, but perhaps a warning in the docs is warranted.

@ekmett
Copy link
Member

ekmett commented Oct 18, 2020

It is known that Reader for Cont is a bit wonky. For me this seems like another argument for splitting up MonadReader into the ask and local parts. That said that transition will be painful and may have to be part of a large scale mtl 3 shift.

@chessai chessai added this to the MTL 3 milestone Jul 16, 2021
@kozross kozross mentioned this issue May 8, 2022
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants