During a lecture on functional programming we saw the following Haskell function:
f :: Bool -> Int -> (a -> Int) -> Int
f x y z = if x then y + y else (z x) + (z y)
It is expected that this function will fail to typecheck. However, the reason why this happens was not explained. When trying it out in GHCI I got the following output:
Prelude> :l test [1 of 1] Compiling Main ( test.hs,
interpreted )
test.hs:2:35:
Couldn't match expected type `a' with actual type `Bool'
`a' is a rigid type variable bound by
the type signature for f :: Bool -> Int -> (a -> Int) -> Int
at test.hs:1:6
Relevant bindings include
z :: a -> Int (bound at test.hs:2:7)
f :: Bool -> Int -> (a -> Int) -> Int (bound at test.hs:2:1)
In the first argument of `z', namely `x'
In the first argument of `(+)', namely `(z x)' Failed, modules loaded: none.
Why does this happen?
flikef True 3 (\n -> n+1). What would you expect to happen?f :: Bool -> Int -> (a -> Int) -> Intmeans that the caller gets to pick thea. So it perfectly fine for the caller to choose a function with typeString -> Int.