Are type variables in "where" sentences in the same namespace with parents?
No *. It gets a little easier if you think of foo :: s -> s in terms of foo :: forall s. s -> s foo :: forall s. s -> s . After all, a type variable indicates that the function works for any type s . Let me add explicit quantitative values ββto your code:
{-
As you can see, there are two forall s. . But one in bar is wrong. In the end, you cannot select s there, but one that is already used in s . This can be done by enabling ScopedTypeVariables :
{-
However, there are some tricks to get rid of ScopedTypeVariables . For example, in this case:
data T s = T (s -> s) foo :: T s -> s -> s foo (T f) x = (bar `asTypeOf` idType x) x where bar a = fa idType :: a -> a -> a idType a _ = a -- For completion, definition and type of 'asTypeOf' -- asTypeOf :: a -> a -> a -- asTypeOf x _ = x
For any x :: s term idType x is of type s -> s , and asTypeOf forces both to be of the same type.
Depending on your actual code, something like this may be more or less doable.
* Well, in this case, since you can use ScopedTypeVariables , see the later part of the answer.
source share