0

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.

3
  • 4
    Integer and Int are not the same type. You may want to pick one and stick with it. Commented Mar 31, 2013 at 14:01
  • @pigworker Which change do you think would be easier to get rid of the problem? Do I have to rewrite the code? Commented Mar 31, 2013 at 14:03
  • 6
    If you're happy with machine-issue fixed-width numbers, go with Int. Perhaps the sensible thing to do would be to use type synonyms like type Agent = (Int, Int) and then write Agent in types whenever you mean an Agent. That'll make it easier to keep your types consistent. Start by rewriting the types in your code so they clearly express what you mean. If there's still a problem with the equations, it should then at least be clearer what's amiss. Commented Mar 31, 2013 at 14:23

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.