I've got a situation in a Haskell web project where I'm getting the error Ambiguous type variable
.
The relevant code is
--- Other import statements
import qualified Model as Model
---------- HTTP Handlers
needItem db user itemName = do
Model.changeItemStatus db user itemName Model.Need
listItems db user
gotItem db user itemName = do
Model.changeItemStatus db user itemName Model.Got
listItems db user
newItem db user itemName comment count = do
Model.insertItem db user itemName comment count
listItems db user
listItems db user = do
lst <- Model.listItems db user
resOk lst
--- rest of the program
The errors are specifically complaining about insertItem
and changeItemStatus
, both of which are acid-state
query functions
insertItem db name itemName comment count = withAccount db name newItem
where item = Item { itemName = itemName, itemComment = comment, itemCount = count, itemStatus = Need }
newItem account = update' db $ NewItem account item
-- other stuff
changeItemStatus db name itemName status = withAccount db name cItem
where cItem account = withItem account itemName
(\i -> update' db $ ChangeItem account $ i { itemStatus = status})
withAccount
and withItem
are helper functions to help me deal with Maybe
return values from the database. They're defined as
withAccount :: MonadIO m => AcidState GoGetDB -> String -> (Account -> a) -> m (Maybe a)
withAccount db name fn = do
maybeAccount <- query' db $ AccountByName name
return $ do acct <- maybeAccount
return $ fn acct
withItem :: Account -> String -> (Item -> a) -> Maybe a
withItem account itemName fn = do
item <- getOne $ (accountItems account) @= itemName
return $ fn item
Ok, now then.
I've read over the AcidState documentation in the Happstack crash-course several times, but it didn't help much; they use queries directly in their response generating functions which
- I've tried and got the same
Ambiguous type variable
error, except pointing at thequery'
call iteslf, - I don't really want to do since that would force me to mix my model/controller code and
- doesn't help my particular situation, since it doesn't show me what the concrete return type of a function that calls
query'
orupdate'
would be (their functions are allAcidState DBName -> ServerPart Response
, since they generate the response directly).
I've tried to put together type signatures for insertItem
and changeItemStatus
by using :t
on pieces of the expression, but every attempt has given me what I assume is the worse error of No instance for (MonadIO m1)
instead.
I'm not a particularly adept Haskeller yet, so I feel like the only other thing I could try is sprinkling random return $
s around the place, but that doesn't sound like it would have a good chance of actually fixing the problem, or teaching me anything.
The general concept I'm trying to implement is: "Make this change to the DB, and then return the (potentially altered) list of relevant elements for the current user".
Any hints on what I should try next, or where I'm specifically going wrong? Am I thinking about this in entirely the wrong way? Is there other documentation I could consult on the subject?
PS. I've included what I think is all relevant code above, but if you want to see the complete source, it's here and here.
EDIT: Full type errors
/home/inaimathi/projects/goget/goget.hs:31:3:
Ambiguous type variable `m2' in the constraint:
(Control.Monad.IO.Class.MonadIO m2)
arising from a use of `Model.changeItemStatus'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' block:
Model.changeItemStatus db user itemName Model.Need
In the expression:
do { Model.changeItemStatus db user itemName Model.Need;
listItems db user }
In an equation for `needItem':
needItem db user itemName
= do { Model.changeItemStatus db user itemName Model.Need;
listItems db user }
/home/inaimathi/projects/goget/goget.hs:35:3:
Ambiguous type variable `m1' in the constraint:
(Control.Monad.IO.Class.MonadIO m1)
arising from a use of `Model.changeItemStatus'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' block:
Model.changeItemStatus db user itemName Model.Got
In the expression:
do { Model.changeItemStatus db user itemName Model.Got;
listItems db user }
In an equation for `gotItem':
gotItem db user itemName
= do { Model.changeItemStatus db user itemName Model.Got;
listItems db user }
/home/inaimathi/projects/goget/goget.hs:39:3:
Ambiguous type variable `m0' in the constraint:
(Control.Monad.IO.Class.MonadIO m0)
arising from a use of `Model.insertItem'
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' block:
Model.insertItem db user itemName comment count
In the expression:
do { Model.insertItem db user itemName comment count;
listItems db user }
In an equation for `newItem':
newItem db user itemName comment count
= do { Model.insertItem db user itemName comment count;
listItems db user }
:t
gives me in GHCi) to everything, I wouldn't be having a problem here. Incidentally, GHCi saysinsertItem :: AcidState GoGetDB -> String -> String -> String -> Integer -> m1 (Maybe (m (acid-state-0.8.2:Data.Acid.Common.EventResult NewItem)))
andchangeItemStatus :: (MonadIO m, MonadIO m1) => AcidState GoGetDB -> String -> String -> ItemStatus -> m1 (Maybe (Maybe (m (acid-state-0.8.2:Data.Acid.Common.EventResult ChangeItem))))
respectively.