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

Mic*_*ard 1 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
Run Code Online (Sandbox Code Playgroud)

: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)
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 5

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

isFree testDay :: Foo Bar Bool
Run Code Online (Sandbox Code Playgroud)

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

不相关,isFree的最后一行会更好 return $ L.null match

  • `liftIO(return x)`可能就是`return x`.不确定"MonadIO"或"MonadTrans"实例是否有任何"法律",但如果有,那肯定会是一个.=) (2认同)