jason
jason

Reputation: 7164

Haskell - An error related to types

I have these functions in a Haskell file and they work fine :

func1 :: Integer -> (Integer,Integer) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (Integer,Integer)

func1 distance agent mymap moves = func5 (func3 agent (func2 distance agent mymap) moves)


func2 :: Integer -> (Integer,Integer) -> [[String]] -> [(Integer,Integer)]

func3 :: (Ord a, Ord b) => (b,a) -> [(b,a)] -> ([Char],[Char],[Char],[Char]) -> [(b,a)]

func4 :: (Int,Int) -> (Int,Int) -> ([Char],[Char],[Char],a) -> ([Char],[Char],[Char],[Char]) -> [[[Char]]] -> [[[Char]]]
func5 [(a,b)] = (a,b) 

But when I write this function :

func6 agent distance mymap moves moves2 = func4 agent (func1 distance agent mymap moves) moves moves2 mymap

I get this error :

*ERROR "play.hs":176 - Type error in application
* * * Expression     : moveWithFood agent (giveNearestCorrect distance agent mymap moves) moves moves2 mymap
* * * Term           : giveNearestCorrect distance agent mymap moves
* * * Type           : (Integer,Integer)
* * * Does not match : (Int,Int)*

Same error with ghci:

play.hs:176:93:
    Couldn't match expected type `Integer' against inferred type `Int'
      Expected type: (Integer, Integer)
      Inferred type: (Int, Int)
    In the second argument of `giveNearestCorrect', namely `agent'
    In the second argument of `moveWithFood', namely
        `(giveNearestCorrect distance agent mymap moves)'
Failed, modules loaded: none.*

I tried several things to solve it but I couldn't succeed. Can you tell me what I should do? Thanks.

Upvotes: 0

Views: 133

Answers (2)

dave4420
dave4420

Reputation: 47052

You are using agent as the second argument to func1/giveNearestCorrect (so it must be an (Integer, Integer), according to the type signature), and also as the first argument to func4/moveWithFood (so it must be an (Int, Int)).

agent cannot be both an (Integer, Integer) and an (Int, Int). Choose one, and stick to it: I would follow the advice in pigworker's comment.

Upvotes: 0

Landei
Landei

Reputation: 54584

As pigworker pointed out, Int and Integer are not the same type. If you have just a few points where you need a "translation", fromIntegral might be the way to go.

For common applications Int is often good enough (and faster than Integer), so I would suggest you try to use this exclusively.

Another possibility would be using the Num type-class. Here is an example for a function that works for both Int and Integer:

func1 :: Num a => a -> (a, a) -> [[String]] -> ([Char],[Char],[Char],[Char]) -> (a, a)

You might need to use some fromIntegral calls inside, depending on your original implementation.

Upvotes: 4

Related Questions