Search code examples
haskelltypesscopeparameterized

ScopedTypeVariables are sometimes in scope, sometimes not


I'm trying to figure out when my type parameters will be in scope. I have what appears to me to be two identical examples, but one doesn't work.

{-# LANGUAGE ScopedTypeVariables #-}
class IntegerAsType a where
  value :: a -> Integer   

class BaseRing a where
  primroot :: a -> Integer -> a

newtype Zq q = Zq Integer deriving (Eq)

instance (IntegerAsType q) => Num (Zq q) where
...
negate (Zq x) = Zq ((value (undefined :: q)) - x) --THIS WORKS

instance (IntegerAsType q) => BaseRing (Zq q) where
primroot = (xyz::(Zq q)) m = (value q)+m --(as an example), DOES NOT WORK

What is the difference between these two instantiations (why does one work, and one not work)?

I'm referencing a previous post, where it was suggested that I put the type in function parameter. That worked previously, but in this case leaving out the explicit type of the argument gets me the same error:

Not in scope 'q'

Thanks


Solution

  • q is a type variable, not a regular variable. That's why value (undefined :: q) works and value q doesn't.