3

I have a question about head, tail, init and last. The following works in GHCi:

Prelude Data.List Data.Char> let n = [1..10] in (head n : tail n)
[1,2,3,4,5,6,7,8,9,10]

As expected, I get the whole list. So this should work for init and last too, right?

Prelude Data.List Data.Char> let n = [1..10] in (init n : last n)

<interactive>:39:1:
    Non type-variable argument in the constraint: Enum [[a]]
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Enum a, Enum [[a]], Num a, Num [[a]]) => [[a]]

If I look at the type signatures for the functions, then head and last look the same -- they both return an element. Also init and tail look the same, because they both return lists.

Prelude Data.List Data.Char> :info head
head :: [a] -> a        -- Defined in ‘GHC.List’
Prelude Data.List Data.Char> :info tail
tail :: [a] -> [a]      -- Defined in ‘GHC.List’
Prelude Data.List Data.Char> :info init
init :: [a] -> [a]      -- Defined in ‘GHC.List’
Prelude Data.List Data.Char> :info last
last :: [a] -> a        -- Defined in ‘GHC.List’

So what does Non type-variable argument in the constraint: Enum [[a]] mean? If I do init n or last n without the construction of a new list, I get [1..9] and 10.

2
  • Turns out you can't delete a comment?!
    – inktrap
    Jun 28, 2016 at 10:50
  • 6
    That's an absolutely terrible error message. The type checker ran into a more subtle problem first and reported it instead of getting to the real point.
    – dfeuer
    Jun 28, 2016 at 10:56

3 Answers 3

10

Ahh snap, : takes an element and the rest like in x:xs, not a list and the last element, like in xs:x.

Prelude Data.List Data.Char> :info :  
data [] a = ... | a : [a] >·-- Defined in ‘GHC.Types’
infixr 5 : 

data [] a = ... | a : [a] >·-- Defined in ‘GHC.Types’
infixr 5 : 

I still would like to know how you understand GHCi error messages and I have to wait 2 days to accept my own answer anyway.

Edit: I get that x and xs are only variable names chosen by convention, and (xs:_) would match the head but is unconventionally/confusingly named.

Edit 2: I upvoted and accepted Daniel Wagner's answer, because he explained the error message step by step. Really nice! Thank you!

2
  • (Better to avoid gendered language altogether in this context, I think. I have a gender, but not everyone does. Also, "girls" can be seen as disrespectful to women.)
    – dfeuer
    Jun 28, 2016 at 10:58
  • 1
    You should probably edit this answer, so that it reads like an answer. Writing :t (:) in GHCi yields (:) :: a -> [a] -> [a]. But you are passing it a [a] as the first arg, and an a as second.
    – Undreren
    Jun 28, 2016 at 11:06
7

It is true that head/last and tail/init have identical types. So if you had simply swapped out last for head and init for tail, you would have had no problem:

> let n = [1..10] in last n : init n
[10,1,2,3,4,5,6,7,8,9]

But you didn't. You did both that swap and another: you changed the order of arguments to :. It so happens that : doesn't take two arguments of the same type:

> :t (:)
(:) :: a -> [a] -> [a]

So this last swap is not okay! In fact, if you give n a slightly more specific type signature, ghci will give a better error:

> let n :: [Integer]; n = [1..10] in init n : last n

<interactive>:1:50:
    Couldn't match type ‘Integer’ with ‘[[Integer]]’
    Expected type: [[[Integer]]]
      Actual type: [Integer]
    In the first argument of ‘last’, namely ‘n’
    In the second argument of ‘(:)’, namely ‘last n’

This error is still not 100% clear, but I think with a bit of puzzling you can see what it's complaining about: since init n :: [Integer], and (:) :: [Integer] -> [[Integer]] -> [[Integer]], it's expecting last n :: [[Integer]] and therefore n :: [[[Integer]]]. But you explicitly said n :: [Integer], a conflict.

Now, what about the error it actually gave you in your case? Well, the clue is in the type of [1..10]:

> :t [1..10]
[1..10] :: (Enum t, Num t) => [t]

Notice that [1..10] is polymorphic. Moreover, it is used twice in your expression, and so it can be given separate monomorphic types in the two uses. So [1..10] is instantiated with two different types in the sequel!

Now I think you can start to see where the error you got comes in. It's trying to find a type a for which:

  • Enum a -- this is needed to do the .. part of init [1..10]
  • Num a -- this is needed to do the 1 and 10 parts of init [1..10]
  • Enum [[a]] -- if init n :: a, then to have init n : last n be well-typed, we must have last n :: [a] and hence the second occurrence of n must have n :: [[a]]; then the Enum constraint is needed for the .. part of last [1..10]
  • Num [[a]] -- by similar reasoning, this is needed to do the 1 and 10 parts of last [1..10]

But these constraints together are hard to satisfy -- certainly there's no instance of Enum and Num for lists in scope in the Prelude or Data.List. So it complains.

2

In larger programs there is more type information (implicit and explicit) allowing the compiler to do a better job inferring types than in one-line snippets provided to GHCi. As a result, the errors you see in GHCi are not representative of what is typically observed when building a full program.

That said, it's no excuse for the awfulness that is the error you posted:

Prelude> let n = [1..10] in (init n : last n)

<interactive>:8:1:
    Non type-variable argument in the constraint: Enum [[a]]
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Enum a, Enum [[a]], Num a, Num [[a]]) => [[a]]

So there is a lot of polymorphism involved in your own line. You have lists of Num a (Num a => [a]) where the type variable a must itself be alist since you've used : last n where last n :: [_] ~ a. So this, combined with the fact that .. in list comprehension implies an enum, is how we got to the horrible message.

Let's look at the simpler case where we've told GHCi our list is of type [Int]:

Prelude> let n = [1..10] :: [Int] in (init n : last n)

<interactive>:7:44:
    Couldn't match type ‘Int’ with ‘[[Int]]’
    Expected type: [[[Int]]]
      Actual type: [Int]
    In the first argument of ‘last’, namely ‘n’
    In the second argument of ‘(:)’, namely ‘last n’

Ahh, much better. Column 44 is the n in last n. It says that last n :: Int. So the type of init n : when init n :: [Int] means our cons function type is (:) :: [Int] -> [[Int]] -> [[Int]]. But wait! [[Int]] needed for the argument of init n : does not match Int provided by last n!

2
  • Nice answer! Thanks. However Daniel was 3min. faster. Have an upvote. :)
    – inktrap
    Jul 9, 2016 at 12:18
  • 1
    That's OK. I'll take a few upvotes off of Daniel's desk to make up for it. Jul 9, 2016 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.