Domanda

I wish to type spec a function f' defined inside a function f so that both of their type specs refer to the same type variable. However, when I try to do this, I get a compile error from the compiler, which assumes that the m outside and the m inside are not the same type variable. Any tip 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
È stato utile?

Soluzione

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

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

Altri suggerimenti

The Haskell 98 Prelude also contains a function asTypeOf which can be used to mimic scoped 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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top