ScopedTypeVariables does not transfer type variables into scope

Here's a simple function to return pointer alignment:

{-# LANGUAGE ScopedTypeVariables #-} import Foreign.Ptr (Ptr) import Foreign.Storable (Storable, alignment) main = return () ptrAlign1 :: (Storable a) => Ptr a -> Int ptrAlign1 _ = alignment (undefined :: a) 

But I get the following error:

 Could not deduce (Storable a0) arising from a use of `alignment' from the context (Storable a) bound by the type signature for ptrAlign1 :: Storable a => Ptr a -> Int at prog.hs:8:14-41 The type variable `a0' is ambiguous 

If I rewrite ptrAlign in a ptrAlign fraction, for example:

 ptrAlign2 :: (Storable a) => Ptr a -> Int ptrAlign2 = ptrAlign3 undefined where ptrAlign3 :: (Storable a) => a -> Ptr a -> Int ptrAlign3 x _ = alignment x 

It works great (of course, this version doesn't even need ScopedTypeVariables ).

But I'm still wondering why the first version is causing an error and what can be done to solve it?

+7
haskell ghc type-variables
source share
1 answer

Even when ScopedTypeVariables turned on, ScopedTypeVariables types do not fit into the scope unless you explicitly quantify them quantitatively, i.e.

 ptrAlign1 :: forall a. (Storable a) => Ptr a -> Int ptrAlign1 _ = alignment (undefined :: a) 
+10
source share

All Articles