9

I've been trying to come up with a simple and intuitive way to use databases with Haskell. I've taken this code from the Yesod book and tried to clean it up so that it can be easier to understand and use.

{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings #-}
{-# LANGUAGE GADTs, FlexibleContexts #-}

import Database.Persist
import Database.Persist.Sqlite (withSqliteConn, runSqlConn, runMigration)
import Database.Persist.TH (share, mkPersist, mkMigrate, sqlSettings, persist)

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist|
Person                              -- Table name
    name String                     -- String value
    age Int Maybe                   -- Numerical value
|]

updateDB x y = withSqliteConn "data.db" $ runSqlConn $ do
    runMigration migrateAll         -- Creates "Person" table if one doesn't exist
    insert $ Person x $ Just y      -- Inserts values into .db file

main = do
    updateDB "Frank Silver" 40      -- adds name "Frank Silver" and age "40" to data.db file

This code almost works, but I get the following error which I haven't been able to resolve.

No instance for (Control.Monad.Trans.Resource.MonadResource IO)
      arising from a use of `updateDB'
    Possible fix:
      add an instance declaration for
      (Control.Monad.Trans.Resource.MonadResource IO)
    In a stmt of a 'do' block: updateDB "Frank Silver" 40
    In the expression: do { updateDB "Frank Silver" 40 }
    In an equation for `main': main = do { updateDB "Frank Silver" 40 }

Any suggestions pointing me in the right direction would be appreciated.

subtlearray
  • 1,251
  • 1
  • 11
  • 23
  • 1
    You need at least a `runResourceT` around the `updateDB` in `main`. I'm not sure if that will be enough, though. – Daniel Fischer Feb 24 '13 at 00:01
  • rewriting the line as runResourceT $ updateDB "Frank Silver" 40 worked! Thank you. Maybe you'd like to post your comment as an answer? – subtlearray Feb 24 '13 at 00:07
  • 4
    I had this problem using persistent as well, but I also had an error with an instance of MonadLogger for IO missing. I'm leaving it here in the hopes it helps some other poor soul. The key is to use `runResourceT . Control.Monad.Logger.runNoLoggingT`, as the instance for MonadLogger IO has been removed. – Khanzor May 28 '13 at 21:56

1 Answers1

9

With

main = do
    updateDB "Frank Silver" 40

the type of updateDB "Frank Silver" 40 is inferred to be IO (), since that's the default type for main (it must have type IO a for some a). But from the definition, its type is inferred to be MonadRescource m => m a for some a (probably a = (), but I'm not sure), and there is no instance MonadResource IO. So you need something to transform the updateDB to an IO action, the normal way to do that is runResourceT, which transforms a ResourceT m a into an m a (here m = IO), so

main = runResourceT $ updateDB "Frank Silver" 40

works.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • I've been using Haskell for almost a year and I'm still learning how to use the type system. :D Thanks again for your help and reply. – subtlearray Feb 24 '13 at 01:51