I'm writing a function which uses the lenses library, but curiously, the code doesn't compile when I remove the type annotation.
{-# LANGUAGE TemplateHaskell, Rank2Types #-}
import Control.Lens
import Control.Monad.State
import Data.List (elemIndex)
data MyRecord = MyRecord { _someField :: [Int], _anotherField :: [String] }
makeLenses ''MyRecord
updateRecord :: Eq a => a -> Lens' b [a] -> (Int -> c) -> State b c
updateRecord elem lens f = do field <- view lens <$> get
case elemIndex elem field of
Just index -> return $ f index
Nothing -> do modify (over lens (++[elem]))
return . f $ length field
When I comment out the type signature for updateRecord
, I get this error:
• Couldn't match type ‘Const [a] s’ with ‘Identity s’
Expected type: ASetter s s [a] [a]
Actual type: Getting [a] s [a]
Why is the type signature required in this case?