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
From the provided code, it's necessary to define the
evalIntExprfunction for evaluating expressions represented byIntExprdata type. Let's complete theevalIntExprfunction by implementing pattern matching for each constructor ofIntExpr.module EvalIntExpr ( evalIntExpr, testEvalIntExpr, IntExpr(..) ) where import Test.QuickCheck data IntExpr = Leaf Int | Add IntExpr IntExpr | Sub IntExpr IntExpr | Mul IntExpr IntExpr | Uminus IntExpr deriving (Eq, Show) -- evalIntExpr evaluates the IntExpr expression-tree. evalIntExpr :: IntExpr -> Int evalIntExpr (Leaf n) = n evalIntExpr (Add expr1 expr2) = evalIntExpr expr1 + evalIntExpr expr2 evalIntExpr (Sub expr1 expr2) = evalIntExpr expr1 - evalIntExpr expr2 evalIntExpr (Mul expr1 expr2) = evalIntExpr expr1 * evalIntExpr expr2 evalIntExpr (Uminus expr) = negate (evalIntExpr expr) testEvalIntExpr = do print "******* test evalIntExpr" quickCheck $ counterexample "Leaf" $ evalIntExpr (Leaf 42) == 42 quickCheck $ counterexample "Add" $ evalIntExpr (Add (Leaf 33) (Leaf 22)) == 55 quickCheck $ counterexample "Sub" $ evalIntExpr (Sub (Leaf 33) (Leaf 22)) == 11 quickCheck $ counterexample "Mul" $ evalIntExpr (Mul (Leaf 31) (Leaf 4)) == 124 quickCheck $ counterexample "Uminus" $ evalIntExpr (Uminus (Leaf 33)) == (-33) quickCheck $ counterexample "Complex" $ evalIntExpr (Mul (Leaf 4) (Sub (Add (Leaf 3) (Uminus (Leaf 33))) (Leaf 20))) == (-200) -- Property-based tests for commutativity, associativity, subtraction, and distributivity quickCheck $ counterexample "e1 + e2 == e2 + e1" $ (\ e1 e2 -> evalIntExpr (Add (Leaf e1) (Leaf e2)) == evalIntExpr (Add (Leaf e2) (Leaf e1))) quickCheck $ counterexample "e1 * e2 == e2 * e1" $ (\ e1 e2 -> evalIntExpr (Mul (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf e2) (Leaf e1))) quickCheck $ counterexample "(e1 + e2) + e3 == e1 + (e2 + e3)" $ (\ e1 e2 e3 -> evalIntExpr (Add (Add (Leaf e1) (Leaf e2)) (Leaf e3)) == evalIntExpr (Add (Leaf e1) (Add (Leaf e2) (Leaf e3)))) quickCheck $ counterexample "(e1 * e2) * e3 == e1 * (e2 * e3)" $ (\ e1 e2 e3 -> evalIntExpr (Mul (Mul (Leaf e1) (Leaf e2)) (Leaf e3)) == evalIntExpr (Mul (Leaf e1) (Mul (Leaf e2) (Leaf e3)))) quickCheck $ counterexample "e1 - e2 = -1*(e2 - e1)" $ (\ e1 e2 -> evalIntExpr (Sub (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf (-1)) (Sub (Leaf e2) (Leaf e1)))) quickCheck $ counterexample "e1 * (e2 + e3) == e1*e2 + e1*e3" $ (\ 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 $ counterexample "e1 * (e2 - e3) == e1*e2 - e1*e3" $ (\ e1 e2 e3 -> evalIntExpr (Mul (Leaf e1) (Sub (Leaf e2) (Leaf e3))) == evalIntExpr (Sub (Mul (Leaf e1) (Leaf e2)) (Mul (Leaf e1) (Leaf e3))))This implementation completes the definition of
evalIntExprand ensures it can now evaluate expressions defined byIntExprdata types, including straightforward arithmetic operations and unary minus. Furthermore, the module is equipped with unit and property-based testing using QuickCheck, ensuring that key algebraic properties (commutativity, associativity, subtractive identity, distributivity) hold for operations defined onIntExprstructures.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.