有人可以向我解释我应该如何修复这种类型的签名?

时间:2011-11-03 01:03:05

标签: haskell types persistent yesod

这是代码,我试着让类型推断找出函数的类型。代码编译时,它在运行时失败。

Ambiguous type variables `b0', `m0' in the constraint:
  (PersistBackend b0 m0) arising from a use of `isFree'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: isFree testDay
In an equation for `it': it = isFree testDay

:t isFree

isFree :: PersistBackend b m => C.Day -> b m Bool

>isFree day = do
   match <- selectList [TestStartDate ==. day,
                        TestStatus !=. Passed,
                        TestStatus !=. Failed] []
   if (L.null match) then (liftIO $ return True) else (liftIO $ return False)

3 个答案:

答案 0 :(得分:5)

ghci告诉您,它不知道为bm选择哪种类型的表达式。你所要做的就是告诉它,

isFree testDay :: Foo Bar Bool

在实际程序中,这些类型变量通常在使用站点确定,因此您很少需要在那里指定表达式的类型。在ghci提示符下,缺少上下文,因此您经常需要这样做。

无关,isFree的最后一行最好是return $ L.null match

答案 1 :(得分:1)

“代码编译时,它在运行时失败。” 非常不可能 在向此函数提供类型时出错。

“失败”是什么意思?

答案 2 :(得分:1)

这令人尴尬。一方面,这个问题确实让我深入研究了Yesod monads的深度。另一方面,这个问题已经为我解答了。运行数据库操作时,将结果传递给runDB,如此。

>isFree day = do
   match <- runDB $ selectList [TestStartDate ==. day,
                                TestStatus !=. Passed,
                                TestStatus !=. Failed] []
   if (L.null match) then (liftIO $ return True) else (liftIO $ return False)