Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell List of List Type Error

In GHCi, i type

let xs = [1, 'a']

it complains the error immediately:

<interactive>:28:11:
No instance for (Num Char) arising from the literal ‘1’
In the expression: 1
In the expression: [1, 'a']
In an equation for ‘xs’: xs = [1, 'a']

However, when I type

let xs = [1, [1, 1]]

It just passed. And it complains when I try to print xs:

<interactive>:5:1:
No instance for (Num [t0]) arising from a use of ‘it’
In a stmt of an interactive GHCi command: print it

I think Haskell is a static type language so any type error should be caught in compile time. I am wondering why the above 2 errors are caught at different time?

like image 902
Cherish Avatar asked Jun 04 '15 23:06

Cherish


1 Answers

1 is a polymorphic value of type Num a => a. So in [1, [2, 3]], we have [2, 3] :: Num a => [a]; since all list elements must have the same type, we conclude that we must have 1 :: Num a => [a]. That's a bit weird -- it's odd to think of 1 as having a list type -- but can be perfectly valid if somebody creates a sufficiently weird instance of Num. Checking whether an instance exists is pushed off until you try to use the instance; this gives you a chance to define the instance after you've defined the value using the instance. So it doesn't complain until you try to actually do something with the list [1, [2, 3]].

Just to illustrate what I mean, one may write:

instance Num a => Num [a] where
    fromInteger n = pure (fromInteger n)
    (+) = liftA2 (+)
    (-) = liftA2 (-)
    (*) = liftA2 (*)
    abs    = liftA abs
    signum = liftA signum

(Indeed, this instance works for any Applicative, and is occasionally even useful.) Then, in ghci:

> let xs = [1, [1, 1]]
> xs
[[1],[1,1]]

See ma, no errors!

like image 59
Daniel Wagner Avatar answered Oct 04 '22 17:10

Daniel Wagner