Parse error using lexically scoped type variables in Haskell
When I submit to GHC the code
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, ScopedTypeVariables #-}
class Modular s a | s -> a where modulus :: s -> a
newtype M s a = M {unM :: a} deriving (Eq, Show)
normalize :: (Modular s a, Integral a) => a -> M s a
normalize x :: M s a = M (x `mod` (modulus (undefined :: s)))
I get the following error:
config1.hs:10:1: Parse error i开发者_JS百科n pattern: normalize
Can you help?
Eric Macaulay
normalize x :: M s a = -- ...
This is just wrong. There's no reason to declare the return type in the definition like this, you've already declared it in the type signature on the line before. And as a matter of fact it isn't syntactically valid and the reason why you get the parse error.
However once you've fixed the parse error (by removing :: M s a
), it still won't work because you're not actually using scoped type variables yet:
In order to make use of the scoped type variables extension, you need to explicitly declare your type variables using the forall
keyword. The fixed definition will look like this:
normalize :: forall s a. (Modular s a, Integral a) => a -> M s a
normalize x = M (x `mod` (modulus (undefined :: s)))
I think what you want is this:
normalize :: forall s a . (Modular s a, Integral a) => a -> M s a
normalize x = M (x `mod` (modulus (undefined :: s)))
Note the forall s a.
which bring the type variables s
and a
in scope in the body of the function, when you use the ScopedTypeVariables
language feature.
The syntax you tried has never been supported by GHC.
精彩评论