Haskell scoping in nested function definitions using where

haskell, scope, type-mismatch, types

Solution

The problem with your code is the locally scoped f1 type signature. It specifies that f1 can take any type

`f1 :: Eq a => a -> [a]`

Even though this is a local function, you've generalized this function to be able to take a type that won't exist within f, whatever this function receives HAS to come from f, so the type signature is unnecessary.

Just remove the f1 type signature.

Edit: Read my post back to myself, it's a bit unclear. a in f1 is a parameterized type that can take anything, but the arguments passed to it are already bound in f. So this function can only receive what its parent function receives, the type signature you're giving it breaks that rule. Hope that's a little more clear.

Problem

I have a problem with Haskell's scoping in `where` definitions. When I have the following function `f`, where I want to pass the `x` to the locally defined function `f1` without explicitely using it as a parameter, I get an error saying that the type of `x` is incompatible with the one in the output of `f1`, although it should be the same: ``` f :: Eq a => a -> [a] f x = f1 x where f1 :: Eq a => a -> [a] f1 y = [ x, y ] ``` The error is the following: ``` Couldn't match expected type `a1' against inferred type `a' `a1' is a rigid type variable bound by the type signature for `f1' at test.hs:4:11 `a' is a rigid type variable bound by the type signature for `f' at test.hs:1:8 In the expression: x In the expression: [x, y] In the definition of `f1': f1 y = [x, y] Failed, modules loaded: none. ``` When I however pass the `x` as an additional parameter, as I did in the following code with the function `g`, it works fine: ``` g :: Eq a => a -> [a] g x = g1 x x where g1 :: Eq a => a -> a -> [a] g1 x y = [ x, y ] ``` Is there a way to make the type `a` in `f` compatible to the type `a` (or `a1`) in `f1`?

Original source