Stephan
Stephan

Reputation: 766

Lens and State, library inconsistencies

I am trying to use lens to modify the state in a StateT Monad Transformer. As far as I understand, this code should compile:

{-# LANGUAGE TemplateHaskell #-}

import Control.Lens (makeLenses, (+=))
import Control.Monad.State (State)

data Game = Game {
    _player :: String,
    _points :: Int
} deriving (Show)

makeLenses ''Game

play :: State Game ()
play = do
    points += 10
    return ()

main :: IO ()
main = undefined    

But ghc tells me this:

No instance for (mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                   Game
                   (Control.Monad.Trans.State.Lazy.StateT
                      Game Data.Functor.Identity.Identity))
  arising from a use of ‘+=’
...

I see that mtl may be the problem, so I type ghc-pkg hide mtl, and compile it again: no errors, it compiles! I have the transformers library installed, so it's using that and that helps.

Then I change State to StateT by changing these two lines: import Control.Monad.State (State) -> import Control.Monad.Trans.State (StateT) and play :: State Game () -> play :: StateT Game IO (), and again, the compiler returns a similar error. What's going on?

Upvotes: 2

Views: 173

Answers (1)

Stephan
Stephan

Reputation: 766

I solved it by fixing package inconsistencies, thanks to a hint by Rufflewind (see comment on my question above).

I first checked my existing package configuration with ghc-pkg check, and then used ghc-pkg unregister --force to remove all broken packages. I iteratively repeated it until no more broken packages were found. Then I reinstalled lens and now it works.

Upvotes: 1

Related Questions