...">

Failed to output (Bounded a1) resulting from using "minBound"

This is probably a dumb question, but why is this feature

myTest :: (Bounded a) => a myTest = minBound :: a 

not typecheck?

It works

 myTest' :: Int myTest' = minBound :: Int 

and they seem to be the same to me, except that you would have to enter the first (for example, myTest :: Int) for it to work.

The error I get is

 β€’ Could not deduce (Bounded a1) arising from a use of 'minBound' from the context: Bounded a bound by the type signature for: myTest :: Bounded a => a 
+8
types haskell
source share
1 answer

You must enable ScopedTypeVariables with {-# LANGUAGE ScopedTypeVariables #-} , which allows you to use type variables from the function signature inside the function itself. You will also need to modify your example as follows:

 {-# LANGUAGE ScopedTypeVariables #-} myTest :: forall a. (Bounded a) => a myTest = minBound :: a 

forall tells the compiler about area a . Definitions without explicit forall c have default behavior (not common).

Otherwise, a inside the function is a different a (changed to a1 compiler) than those indicated in the main type signature. He cannot deduce that a1 bounded only from a context that is bounded by some other type of a .

The second example works because Int not a type variable, it is a specific type, which means that it belongs to the same type no matter what type variables are or do not have scope.

additional literature

+7
source share

All Articles