Question: Question about Haskell Language Extend the abstract machine to support the use of multiplication. Abstract Machine: data Expr = Val Int | Add Expr Expr

Question about Haskell Language

Question about Haskell Language Extend the abstract machine to support the useof multiplication. Abstract Machine: data Expr = Val Int | Add Expr

Extend the abstract machine to support the use of multiplication. Abstract Machine: data Expr = Val Int | Add Expr Expr type Cont = [Op] data Op = EVAL Expr | ADD Int eval :: Expr-> Cont -> Int eval (Val n) c = exec cn eval (Add x y) c = eval x (EVAL Y:C) {-1 eval evaluates an expression in the context of a control stack. That is, if the expression is an integer, it is already fully evaluated, and we begin executing the control stack. If the expression is an addition, we evaluate the first argument, x, placing the operation EVAL y on top of the control stack to indicate that the second argument, y, should be evaluated once evaluation of the first argument is completed. -} exec :: Cont -> Int -> Int exec [] n = n exec (EVAL Y: C) n = eval y (ADD n:c) exec (ADD n: c) m = exec c(n+m) {-1 exec executes a control stack in the context of an integer argument. That is, if the control stack is empty, we return the integer argument as the result of the execution. If the top of the control stack is an operation EVAL y, we evaluate the expression y, placing the operation ADD n on top of the remaining stack to indicate that the current integer argument, n, should be added together with the result of evaluating y once this is completed. And finally, if the top of the stack is an operation ADD n, evaluation of the two arguments of an addition expression is now complete, and we execute the remaining control stack in the context of the sum of the two resulting integer values. -} value :: Expr -> Int value e = eval e [] Example: *Main> value (Add (Val 3) (Val 4)) 7 Extend the abstract machine to support the use of multiplication: Extend the abstract machine to support the use of multiplication. Abstract Machine: data Expr = Val Int | Add Expr Expr type Cont = [Op] data Op = EVAL Expr | ADD Int eval :: Expr-> Cont -> Int eval (Val n) c = exec cn eval (Add x y) c = eval x (EVAL Y:C) {-1 eval evaluates an expression in the context of a control stack. That is, if the expression is an integer, it is already fully evaluated, and we begin executing the control stack. If the expression is an addition, we evaluate the first argument, x, placing the operation EVAL y on top of the control stack to indicate that the second argument, y, should be evaluated once evaluation of the first argument is completed. -} exec :: Cont -> Int -> Int exec [] n = n exec (EVAL Y: C) n = eval y (ADD n:c) exec (ADD n: c) m = exec c(n+m) {-1 exec executes a control stack in the context of an integer argument. That is, if the control stack is empty, we return the integer argument as the result of the execution. If the top of the control stack is an operation EVAL y, we evaluate the expression y, placing the operation ADD n on top of the remaining stack to indicate that the current integer argument, n, should be added together with the result of evaluating y once this is completed. And finally, if the top of the stack is an operation ADD n, evaluation of the two arguments of an addition expression is now complete, and we execute the remaining control stack in the context of the sum of the two resulting integer values. -} value :: Expr -> Int value e = eval e [] Example: *Main> value (Add (Val 3) (Val 4)) 7 Extend the abstract machine to support the use of multiplication

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!