这是代码:
class Problem p where
readProblem :: String -> p
solveProblem :: p -> String
readAndSolve = solveProblem . readProblem
Run Code Online (Sandbox Code Playgroud)
这是GHC产生的错误信息:
Ambiguous type variable `b0' in the constraint:
(Problem b0) arising from a use of `readProblem'
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `(.)', namely `readProblem'
In the expression: solveProblem . readProblem
In an equation for `readAndSolve':
readAndSolve = solveProblem . readProblem
Run Code Online (Sandbox Code Playgroud)
据我所知,我必须以某种方式告诉编译器使用的Problem实例solveProblem和readProblem类型相同,但我认为没有办法声明它.为什么不能自己解决这个问题呢?
您无需告诉编译器它必须是相同的类型,编译器会自行解决.但是,它无法确定使用哪种类型.这个问题的典型着名例子是
foo = show . read
Run Code Online (Sandbox Code Playgroud)
如果foo有合法类型,那就是
foo :: (Read a, Show a) => String -> String
Run Code Online (Sandbox Code Playgroud)
现在,编译器如何确定?
你readAndSolve会有类型的
readAndSolve :: Problem p => String -> String
Run Code Online (Sandbox Code Playgroud)