Public Group

# Haskell's type system is driving me insane...

This topic is 2806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to increase my understanding and appreciation of formal logic and reasoning by trying, for what will be my third time, to learn Haskell. The problem I have had each time is more or less the same: in trying to do trivial things while learning the basics, I come up against errors dealing with things that I have no knowledge of at my level.

I knew when I started again that Monads can be tricky and I didn't really know much about them, so I decided to forgo input and output in a compiled program in favor of loading code into ghci and testing it interactively. I decided to start with Project Euler problems to get practice, because Haskell's Integer type would allow for easy calculations with arbitrarily large numbers.

Everything was going fine until I tried something like this in the interpreter(it's for Project Euler problem #12):

-- Create a list of the first 1000 triangle numbers
let triangle n = (n *n + n)/2
let a = [1..1000]
let b = map (triangle) a

It explodes with this bizarre error:
<interactive>:1:13:
No instance for (Fractional Integer)
arising from a use of triangle' at <interactive>:1:13-20
Possible fix: add an instance declaration for (Fractional Integer)
In the first argument of map', namely (triangle)'
In the expression: map (triangle) a
In the definition of b': b = map (triangle) a

What does this mean? A similar error was given when I tried using the square root function in a lambda function with filter.

I'm also confused as to why :t triangle gives me triangle :: (Fractional a) => a -> a. What does that mean?

##### Share on other sites
Haskell tries to infer the most general type for every function. For triangle it infers that it takes a parameter of any fractional type and returns a result of the same type (the function uses division, the compiler cannot see that the result of the division is always an integer).

You can read the type signature as "For every type a in the class Fractional, the function has the type takes an a and returns an a." This means that triangle is a polymorphic function, i.e. you can think of it as a family of functions, one per Fractional type.

When you call triangle with an Integer arg, the compiler complains that Integer is not a type in the Fractional class.

You can change the triangle function to use the div operator, which performs integer division discarding the remainder (the type of triangle should change to Integral a => a -> a) or by calling it with a list of e.g. float values.

1. 1
2. 2
Rutin
24
3. 3
JoeJ
19
4. 4
5. 5

• 17
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631729
• Total Posts
3001918
×