module EvalIntExpr (evalIntExpr, testEvalIntExpr, IntExpr(..) ) where import…
- User
module EvalIntExpr (evalIntExpr, testEvalIntExpr, IntExpr(..) ) where
import Test.QuickCheck
-- an IntExpr is an expression-tree over integers data IntExpr = Leaf Int | Add IntExpr IntExpr | Sub IntExpr IntExpr | Mul IntExpr IntExpr | Uminus IntExpr deriving (Eq, Show)
-- evalIntExpr returns the Int result of evaluating the IntExpr -- expression-tree given by its argument. evalIntExpr :: IntExpr -> Int evalIntExpr _ = error "TODO"
testEvalIntExpr = do print "******* test evalIntExpr"
quickCheck evalIntExpr (Leaf 42) == 42 quickCheck evalIntExpr (Add (Leaf 33) (Leaf 22)) == 55 quickCheck evalIntExpr (Sub (Leaf 33) (Leaf 22)) == 11 quickCheck evalIntExpr (Mul (Leaf 31) (Leaf 4)) == 124 quickCheck evalIntExpr (Uminus (Leaf 33)) == (-33) quickCheck evalIntExpr (Mul (Leaf 4) (Sub (Add (Leaf 3) (Uminus (Leaf 33))) (Leaf 20))) == (-200)
-- property-based tests -- commutativity quickCheck (\ e1 e2 -> evalIntExpr (Add (Leaf e1) (Leaf e2)) == evalIntExpr (Add (Leaf e2) (Leaf e1))) quickCheck (\ e1 e2 -> evalIntExpr (Mul (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf e2) (Leaf e1))) -- associativity quickCheck (\ e1 e2 e3 -> evalIntExpr (Add (Add (Leaf e1) (Leaf e2)) (Leaf e3)) == evalIntExpr (Add (Leaf e1) (Add (Leaf e2) (Leaf e3)))) quickCheck (\ e1 e2 e3 -> evalIntExpr (Mul (Mul (Leaf e1) (Leaf e2)) (Leaf e3)) == evalIntExpr (Mul (Leaf e1) (Mul (Leaf e2) (Leaf e3))))
-- subtraction quickCheck (\ e1 e2 -> evalIntExpr (Sub (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf (-1)) (Sub (Leaf e2) (Leaf e1))))
-- distributivity quickCheck (\ e1 e2 e3 -> evalIntExpr (Mul (Leaf e1) (Add (Leaf e2) (Leaf e3))) == evalIntExpr (Add (Mul (Leaf e1) (Leaf e2)) (Mul (Leaf e1) (Leaf e3)))) quickCheck (\ e1 e2 e3 -> evalIntExpr (Mul ( Leaf e1) (Sub (Leaf e2) (Leaf e3))) == evalIntExpr (Sub (Mul (Leaf e1) (Leaf e2)) (Mul (Leaf e1) (Leaf e3))))
- Assistant
To make this Haskell module work as intended, we need to implement the
evalIntExprfunction. Let's fill in the missing implementation based on the structure of theIntExprdata type and the intended behavior of the function. Each case in theevalIntExprfunction should handle one of the constructors of theIntExprtype:Leaf,Add,Sub,Mul, andUminus.Implementing evalIntExpr:
evalIntExpr :: IntExpr -> Int evalIntExpr (Leaf n) = n evalIntExpr (Add e1 e2) = evalIntExpr e1 + evalIntExpr e2 evalIntExpr (Sub e1 e2) = evalIntExpr e1 - evalIntExpr e2 evalIntExpr (Mul e1 e2) = evalIntExpr e1 * evalIntExpr e2 evalIntExpr (Uminus e) = - evalIntExpr eExplanation:
- For a
Leaf n, we just return the integern. - For an
Add e1 e2, we recursively evaluatee1ande2and then add the results. - For a
Sub e1 e2, we recursively evaluatee1ande2and then subtract the result ofe2frome1. - For a
Mul e1 e2, we recursively evaluatee1ande2and then multiply the results. - For a
Uminus e, we recursively evaluateeand then negate the result.
With this implementation, the
evalIntExprfunction should now properly evaluateIntExprexpressions to integers, and the test cases provided, including both example-based and property-based tests using QuickCheck, should pass, checking the correctness of your implementation across a wide range of inputs. - For a