-- Module declaration module EvalIntExpr ( evalIntExpr, testEvalIntExpr, IntExpr(..) )…
- User
-- Module declaration module EvalIntExpr ( evalIntExpr, testEvalIntExpr, IntExpr(..) ) where
import Test.QuickCheck
-- IntExpr data type definition data IntExpr = Leaf Int | Add IntExpr IntExpr | Sub IntExpr IntExpr | Mul IntExpr IntExpr | Uminus IntExpr deriving (Eq, Show)
-- evalIntExpr function definition evalIntExpr :: IntExpr -> Int evalIntExpr (Leaf x) = x 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) = - (evalIntExpr expr)
-- testEvalIntExpr definition (unchanged) testEvalIntExpr :: IO () 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)
- Assistant
This Haskell module defines the
EvalIntExprmodule, which provides functionality for evaluating integer expressions represented using theIntExprdata type. TheIntExprdata type supports basic arithmetic operations: addition (Add), subtraction (Sub), multiplication (Mul), unary minus (Uminus), and integer literals (Leaf). TheevalIntExprfunction is responsible for evaluating expressions of typeIntExprto their integer results.The module also includes function, which utilizes QuickCheck, a library for random testing of program properties, to verify the correctness of . It specifically checks: