How to refer to an existing type variable in a haskell type specification?

I want to print the spec function f 'defined inside the function f, so that both of their specification types belong to the same type variable. However, when I try to do it, I get a compilation error from the compiler, which suggests that the external element m and m are not the same variable type. Any advice on how to fix this?

 f :: (Monad m) => (String -> Int -> String -> m ()) -> [String] -> m () f _ (x:_) = f' Nothing x where f' :: (Maybe Int) -> String -> m () -- when I comment this line, the code compiles f' _ _ = return () main = undefined 
+4
source share
2 answers

Note http://www.haskell.org/haskellwiki/Scoped_type_variables

From the link: Scoped Type Variables are an extension to Haskell type system that allow free type variables to be re-used in the scope of a function.

+4
source

The Haskell Prelude 98 Prelude also contains the asTypeOf function, which can be used to simulate type variables to a certain extent (if you use a compiler that does not support XScopedTypeVariables).

See http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:asTypeOf

+1
source

Source: https://habr.com/ru/post/1497784/


All Articles