In the solution to another question, I came up with the following code, which type checks nicely:
import Control.Applicative
import Data.Foldable
tryCombination :: Alternative f => Int -> Int -> f (Int,Int)
tryCombination x y
| x * y == 20 = pure (x,y)
| otherwise = empty
result :: Alternative f => f (Int,Int)
result = asum [tryCombination x y | x <- [1..5], y <- [1..5]]
However, when I remove the type signature from result
, ghc tells me about ambiguous type variables:
No instance for (Alternative f0) arising from a use of ‘asum’
The type variable ‘f0’ is ambiguous
Relevant bindings include
result :: f0 (Int, Int) (bound at BreakLoop.hs:12:1)
Note: there are several potential instances:
...
It looks like ghc has almost completely inferred the correct type. So, why is it that this inference cannot be entirely completed?
What appears even stranger is the error message I get when I use the following partial type signature:
result :: Alternative f => f _
The message is:
No instance for (Alternative f) arising from a use of ‘asum’
Possible fix:
add (Alternative f) to the context of
the inferred type of result :: f (Int, Int)
...
Is there any reasonable explanation for this behavior or is it just a bug?