For a function definition, I need to output a error when division by zero is detected.
The general case is as follows, op takes the (+), (-), (*), (/) function.
liftIntOp :: (Int->Int->Int)->Val->Val->Val
liftIntOp op (IntVal a) (IntVal b) =IntVal $ op a b
The use case is
Main>liftIntOp (+) (IntVal 3) (IntVal 5)
Main>IntVal 8
However I try to detect the special case of division by 0. My solution is
liftIntOp :: (Int->Int->Int)->Val->Val->Val
liftIntOp (/) _ (IntVal 0) =ErrorVal "Division by 0"
liftIntOp op (IntVal a) (IntVal b) =IntVal $ op a b
But the first branch seems to match any operators.
Main> liftIntOp (-) (IntVal 3) (IntVal 0)
Main> ErrorVal "Division by 0"
Main> liftIntOp (+) (IntVal 3) (IntVal 0)
Main> ErrorVal "Division by 0"
I wonder how to single out the case for division.