Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

LO6 Describe how individual pay rates are set.

Answered: 1 week ago