Why doesn't GHCi display the polymorphic type in this error message?

I attribute the type of the variable to type Num a => a -> ato Int, which causes an error, as expected. However, the error is not what I expected.

Ghci

λ> let x = typeInference 1
λ> :t x
x :: Num a => a -> a
λ> x :: Int

<interactive>:141:1: error:
    • Couldn't match expected type 'Int'
                  with actual type 'Integer -> Integer'
    • Probable cause: 'x' is applied to too few arguments
      In the expression: x :: Int
      In an equation for 'it': it = x :: Int
λ>

Type Definition Definition

typeInference :: Num a => a -> a -> a
typeInference x y = x + y + 1

I was expecting an error message to report with actual type 'Num a => a -> a'what is a polymorphic type, why? Is this related to the default GHCi type?

+4
source share
1 answer

This is really due to the default GHCi type .

The rules for this are described in section 4.3.4 of the Haskell 2010 report , and this part is especially relevant:

, . , .

, , , , , .

, . , , x :: Int ,

, e , ∀ u '. cx ⇒ t, u u ', cx, t. .

( Haskell 2010, u u').

, , (, Num), (Int) ( ), .

, ,

(x :: forall a. Num a => a -> a) :: Int

, , ((->) Int ), , ( , (->)).

Here are a few examples I tested that follow this behavior:

ghci> :set -XExplicitForAll
ghci> (x :: forall a. Num a => a -> a) :: Char -> Char  -- Outermost constructor matches, so 'a' can get unified with Char and the 'a' type variable disappears

<interactive>:5:2: error:
    • No instance for (Num Char)
        arising from an expression type signature
    • In the expression:
          (x :: forall a. Num a => a -> a) :: Char -> Char
      In an equation for ‘it’:
          it = (x :: forall a. Num a => a -> a) :: Char -> Char
ghci> (x :: forall a. Num a => a -> a) :: Maybe Char

<interactive>:8:2: error:
    • Couldn't match expected type ‘Maybe Char’
                  with actual type ‘Integer -> Integer’
    • In the expression: (x :: forall a. Num a => a -> a) :: Maybe Char
      In an equation for ‘it’:
          it = (x :: forall a. Num a => a -> a) :: Maybe Char
ghci> (x :: forall a. Num a => a -> a) :: Either Char Bool

<interactive>:10:2: error:
    • Couldn't match expected type ‘Either Char Bool’
                  with actual type ‘Integer -> Integer’
    • In the expression:
          (x :: forall a. Num a => a -> a) :: Either Char Bool
      In an equation for ‘it’:
          it = (x :: forall a. Num a => a -> a) :: Either Char Bool
ghci> (x :: forall a. Num a => (,) a a) :: Either Char Bool

<interactive>:11:2: error:
    • Couldn't match expected type ‘Either Char Bool’
                  with actual type ‘(Integer, Integer)’
    • In the expression:
          (x :: forall a. Num a => (,) a a) :: Either Char Bool
      In an equation for ‘it’:
          it = (x :: forall a. Num a => (,) a a) :: Either Char Bool
ghci> (x :: forall a. Num a => (,) a a) :: Char

<interactive>:12:2: error:
    • Couldn't match expected type ‘Char’
                  with actual type ‘(Integer, Integer)’
    • In the expression: (x :: forall a. Num a => (,) a a) :: Char
      In an equation for ‘it’:
          it = (x :: forall a. Num a => (,) a a) :: Char
+4
source

Source: https://habr.com/ru/post/1678560/


All Articles