module EvalIdExpr ( evalIdExpr, testEvalIdExpr ) where
import Test.QuickCheck
import Data.List (lookup)
an IdExpr is an expression-tree over integers and string identifiers
data IdExpr =
Id String |
Leaf Int |
Add IdExpr IdExpr |
Sub IdExpr IdExpr |
Mul IdExpr IdExpr |
Uminus IdExpr
deriving (Eq, Show)
Assoc is a list of pairs mapping identifier strings to their values v.
type Assoc v = [ (String, v) ]
evalIdExpr returns evaluation of the IdExpr expression-tree given by its first argument, looking up the values of id's in the Assoc argument. If an id is not found in the Assoc argument, then its value should default to 0.
Hint: use Data.List.lookup imported above.
evalIdExpr :: IdExpr -> Assoc Int -> Int
evalIdExpr _ = error "TODO"
Below is an implementation for EvalIdExpr module, which includes the function evalIdExpr to evaluate the IdExpr expression trees. Also, it contains the tests as specified in your instructions using QuickCheck.
This code provides a complete implementation for evaluating expressions involving integers, string identifiers, and basic arithmetic operations. Additionally, it includes a mix of unit tests and property-based tests using QuickCheck to verify the correctness of the evalIdExpr function against a variety of test cases, including handling identifiers, arithmetic operations, and testing fundamental arithmetic properties such as commutativity and associativity.