I have a parser that I am working on. Without getting into the all the details, I want a function that will add two numeric values do:
add [VFloat a, VFloat b] = return $ VFloat (a + b)
add [VInt a, VFloat b] = return $ VFloat (fromInteger a + b)
add [VFloat a, VInt b] = return $ VFloat (a + fromInteger b)
add [VInt a, VInt b] = return $ VInt (a + b)
add [_,_] = throwError "Currently only adding numbers"
add _ = throwError "Arity Error: Add takes 2 arguments"
Cool, works great. Now I want the same function for -,*,/,<,>,==,etc...
So I factor out the + operator and go to pass in an operator op :: Num a => a->a->a right?
Well not quite. If I just replace the + with 'op' the type-checker tells me that op is actually Double -> Double -> Double based on the first three versions and therefore it cannot be applied to Integers in the fourth version.
Two questions:
- How do I write
binop :: Num a => (a->a->a) -> [Value]-> EvalM Valueso that it can handle both VInt and VFloat? - What is the proper name for the situation I am facing so I can Google the answer next time?