Haskell Yesod:在ghci中运行“runDB”函数时出现类型实例错误

Haskell Yesod:在ghci中运行“runDB”函数时出现类型实例错误,haskell,yesod,persistent,ghci,Haskell,Yesod,Persistent,Ghci,在ghci中加载搭建的站点后,获得runDB返回的正确实例是什么?例如,在运行此句子时: runDB $ selectList [UserName ==. "Renny"] [] 错误是: Couldn't match type `PersistMonadBackend (YesodPersistBackend site0 (HandlerT site0 IO))' with `persistent-1.3.0.6:Database.Persist.Sql.Types.SqlBackend'

在ghci中加载搭建的站点后,获得runDB返回的正确实例是什么?例如,在运行此句子时:

runDB $ selectList [UserName ==. "Renny"] []
错误是:

Couldn't match type `PersistMonadBackend
(YesodPersistBackend site0 (HandlerT site0 IO))'
with `persistent-1.3.0.6:Database.Persist.Sql.Types.SqlBackend'
The type variable `site0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Expected type: PersistMonadBackend
                 (YesodPersistBackend site0 (HandlerT site0 IO))
  Actual type: PersistEntityBackend User
In the second argument of `($)', namely
  `selectList [UserName ==. "Renny"] []'
In the expression: runDB $ selectList [UserName ==. "Renny"] []
In an equation for `it':
    it = runDB $ selectList [UserName ==. "Renny"] []
提前谢谢

编辑: 我忘记了YesODscaffold的
runDB
返回一个处理程序,这使我找到了这个解决方法(尽管我相信这是一个更好的解决方案):


问题在于,与依赖于环境状态的命令式语言不同,Haskell依赖于显式(和隐式)状态传递

从ghci运行
runDB$…
时,您试图直接在IO中运行此代码段,因此您没有对应用程序状态(包括数据库连接)的引用。类型错误通知您类型变量“site0”不明确,因为它无法推断您试图运行此语句的应用程序状态


在ghci中,前缀
runSqlite“MyProject.sqlite3”
起作用,因为您专门设置了在正确的数据库上运行的环境,而runSqlite在IO中起作用,这正是ghci想要的。

如果您关闭with
:set-XNoMonomorphismRestriction
,会发生什么?这是一个好问题。“我甚至没有想过使用GHCi作为持久化的前端。”nomen感谢您的回复。我想我找到了一个解决方案(请参阅问题的最后编辑版本)。拜伊
xs <- runSqlite "MyProject.sqlite3" (selectList [UserName ==. "Renny"] [])