Why one function compiles successfully, and the second doesn't, when the second only differs with type declaration of inner function?

Viewed 59
doublePrepend :: a -> [a] -> [a]
doublePrepend x xs = pair ++ xs
    where pair = [x, x]

doublePrepend2 :: a -> [a] -> [a]
doublePrepend2 x xs = pair ++ xs
    where pair :: [a]
          pair = [x, x]

The function doublePrepend compiles successfully. The function doublePrepend2 is the same, but inner helper function pair also has additionally type declaration.

Compile error:

test.hs:9:19: error:
    • Couldn't match expected type ‘a1’ with actual type ‘a’
      ‘a1’ is a rigid type variable bound by
        the type signature for:
          pair :: forall a1. [a1]
        at test.hs:8:11-21
      ‘a’ is a rigid type variable bound by
        the type signature for:
          doublePrepend2 :: forall a. a -> [a] -> [a]
        at test.hs:6:1-33
    • In the expression: x
      In the expression: [x, x]
      In an equation for ‘pair’: pair = [x, x]
    • Relevant bindings include
        pair :: [a1] (bound at test.hs:9:11)
        xs :: [a] (bound at test.hs:7:18)
        x :: a (bound at test.hs:7:16)
        doublePrepend2 :: a -> [a] -> [a] (bound at test.hs:7:1)
  |
9 |           pair = [x, x]
  |

But the x in the doublePrepend2 and the x in pair are the same and their types are the same - a, so why haskell cannot match them?

0 Answers
Related