Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· OpenAIAi2 WildChat-4.8M (AI2)

module EvalIntExpr (evalIntExpr, testEvalIntExpr, IntExpr(..) ) where import…

  1. 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 counterexample"Leaf"counterexample "Leaf" evalIntExpr (Leaf 42) == 42 quickCheck counterexample"Add"counterexample "Add" evalIntExpr (Add (Leaf 33) (Leaf 22)) == 55 quickCheck counterexample"Sub"counterexample "Sub" evalIntExpr (Sub (Leaf 33) (Leaf 22)) == 11 quickCheck counterexample"Mul"counterexample "Mul" evalIntExpr (Mul (Leaf 31) (Leaf 4)) == 124 quickCheck counterexample"Uminus"counterexample "Uminus" evalIntExpr (Uminus (Leaf 33)) == (-33) quickCheck counterexample"Complex"counterexample "Complex" evalIntExpr (Mul (Leaf 4) (Sub (Add (Leaf 3) (Uminus (Leaf 33))) (Leaf 20))) == (-200)

    -- property-based tests -- commutativity quickCheck counterexample"e1+e2==e2+e1"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"counterexample "e1 * e2 == e2 * e1" (\ e1 e2 -> evalIntExpr (Mul (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf e2) (Leaf e1))) -- associativity quickCheck counterexample"(e1+e2)+e3==e1+(e2+e3)"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)"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))))

    -- subtraction quickCheck counterexample"e1−e2=−1∗(e2−e1)"counterexample "e1 - e2 = -1*(e2 - e1)" (\ e1 e2 -> evalIntExpr (Sub (Leaf e1) (Leaf e2)) == evalIntExpr (Mul (Leaf (-1)) (Sub (Leaf e2) (Leaf e1))))

    -- distributivity quickCheck counterexample"e1∗(e2+e3)==e1∗e2+e1∗e3"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"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))))

  2. Assistant

    To make this Haskell module work as intended, we need to implement the evalIntExpr function. Let's fill in the missing implementation based on the structure of the IntExpr data type and the intended behavior of the function. Each case in the evalIntExpr function should handle one of the constructors of the IntExpr type: Leaf, Add, Sub, Mul, and Uminus.

    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 e
    

    Explanation:

    • For a Leaf n, we just return the integer n.
    • For an Add e1 e2, we recursively evaluate e1 and e2 and then add the results.
    • For a Sub e1 e2, we recursively evaluate e1 and e2 and then subtract the result of e2 from e1.
    • For a Mul e1 e2, we recursively evaluate e1 and e2 and then multiply the results.
    • For a Uminus e, we recursively evaluate e and then negate the result.

    With this implementation, the evalIntExpr function should now properly evaluate IntExpr expressions 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.