Search code examples
haskelllenses

Why can't I bind and reuse a haskell lens like a normal variable?


(I'm using lens-family, not lens)

I have a fairly deep data structure and I need to focus on two parts that have a common path. So very intuitively I define _table with the intention to reuse it:

let _table = _sOutput.at (O.tName table)._Just'
tp' <- evalCacheable (_table.O._stPerm) M.empty
... reuse _table

but this yields an error:

Could not deduce (Functor f0) arising from a use of ‘_sOutput’
from the context (MonadReader (EvalConf State All) m, ...

Yet, directly pasting the value of _table into the argument works:

tp' <- evalCacheable (_sOutput.at (O.tName table)._Just'.O._stPerm) M.empty

What is going on? I can give more details about the types involved, but to me this seems puzzling regardless. I was under the impression that

let x = y
z <- f x

was equivalent to

z <- f y

in all cases.


Solution

  • This could be related to the Monomorphism Restriction. Try putting {-# LANGUAGE NoMonomorphismRestriction #-} at the top of your file. –randomusername

    It worked! Someone on #haskell also solved it by adding an explicit type signature to _table. according to him Rank2Types was messing with type inference. – BruceBerry