Why can't I map a function that multiplies by a Fractional onto a list of Nums?

haskell, lambda, types

Solution

You've discovered the "dreaded monomorphism restriction". Basically GHC will infer the type of `indices` to be a monotype like `[Integer]` instead of `Num a => a`. You can either provide an annotation like `indices :: [Float]`, or rework your definitions to avoid the restriction.

For example (not a suggestion), if you make `indices` a function: `let indices a = [-1500, -1499..1500]`, the inferred type is now `(Enum t, Num t) => a -> [t]`. The `a` parameter is unused but defeats the restriction. Then you can then do `map f (indices whatever)`. See much more information in the Haskell Wiki about the Monomorphism Restriction.

Problem

I want to make a list of numbers every `0.1` from `-150` to `150`. To do this, I created a list, and then tried to map a Fractional multiplication lambda onto it, like so: ``` let indices = [-1500,-1499..1500] let grid = map (\x -> 0.1 *x) indices ``` This makes ghci spit out an error. On the other hand, both of these work fine: ``` let a = 0.1*2 ``` and ``` let grid = map (\x -> 2 *x) indices ``` What's going on here? Why does multiplication of a Num by a Fractional only fail when applied to a list with map? EDIT: The error I get is: ``` No instance for (Fractional Integer) arising from the literal `0.1' Possible fix: add an instance declaration for (Fractional Integer) In the first argument of `(*)', namely `0.1' In the expression: 0.1 * x In the first argument of `map', namely `(\ x -> 0.1 * x)' ```

Original source

Related problems