Question

Suppose that we extend the language of arithmetic expressions with simple primitives for throwing and catching an exception, as follows: data Expr = Val Int | Add Expr Expr | Throw | Catch Expr Expr Informally, Catch $\mathrm{x} h$ behaves as the expression $\mathrm{x}$ unless evaluation of $x$ throws an exception, in which case the catch behaves as the handler expression $\mathrm{h}$. An exception is thrown if evaluation of Throw is attempted. To define a semantics for this extended language, we first recall the Maybe type: data Maybe a = Nothing | Just a That is, a value of type Maybe a is either Nothing, which we view here as an exceptional value, or has the form Just $x$, which we view as a normal value. Using this type, our original evaluation function for expressions can be rewritten to take account of exceptions as follows: eval :: Expr $\rightarrow$ Maybe Int eval (Val $\mathrm{n}) \quad=$ Just $\mathrm{n}$ eval (Add $\mathrm{x} \mathrm{y}$ ) = case eval $\mathrm{x}$ of Just $\mathrm{n} \rightarrow$ case eval $\mathrm{y}$ of Just $m \rightarrow$ Just $(n+m)$ Nothing $\rightarrow$ Nothing Nothing $\rightarrow$ Nothing eval Throw = Nothing eval (Catch $\times$ h) $=$ case eval $\mathrm{x}$ of Just $n \rightarrow$ Just $n$ Nothing $\rightarrow$ eval $\mathrm{h}$ Using the approach described in this chapter, calculate a compiler for this language. Hint: this is a challenging exercise!

   Suppose that we extend the language of arithmetic expressions with simple primitives for throwing and catching an exception, as follows:
data Expr = Val Int
| Add Expr Expr
| Throw
| Catch Expr Expr
Informally, Catch $\mathrm{x} h$ behaves as the expression $\mathrm{x}$ unless evaluation of $x$ throws an exception, in which case the catch behaves as the handler expression $\mathrm{h}$. An exception is thrown if evaluation of Throw is attempted. To define a semantics for this extended language, we first recall the Maybe type:
data Maybe a = Nothing | Just a

That is, a value of type Maybe a is either Nothing, which we view here as an exceptional value, or has the form Just $x$, which we view as a normal value. Using this type, our original evaluation function for expressions can be rewritten to take account of exceptions as follows:
eval :: Expr $\rightarrow$ Maybe Int
eval (Val $\mathrm{n}) \quad=$ Just $\mathrm{n}$
eval (Add $\mathrm{x} \mathrm{y}$ ) = case eval $\mathrm{x}$ of
Just $\mathrm{n} \rightarrow$ case eval $\mathrm{y}$ of
Just $m \rightarrow$ Just $(n+m)$
Nothing $\rightarrow$ Nothing
Nothing $\rightarrow$ Nothing
eval Throw = Nothing
eval (Catch $\times$ h) $=$ case eval $\mathrm{x}$ of
Just $n \rightarrow$ Just $n$
Nothing $\rightarrow$ eval $\mathrm{h}$ Using the approach described in this chapter, calculate a compiler for this language. Hint: this is a challenging exercise!
Show more…
Programming in Haskell
Programming in Haskell
Graham Hutton 2nd Edition
Chapter 17, Problem 1 ↓

Instant Answer

verified

Step 1

The AST can be defined as follows: data Expr = Val Int | Add Expr Expr | Throw | Catch Expr Expr  Show more…

Show all steps

lock
AceChat toggle button
Close icon
Ace pointing down

Please give Ace some feedback

Your feedback will help us improve your experience

Thumb up icon Thumb down icon
Thanks for your feedback!
Profile picture
Suppose that we extend the language of arithmetic expressions with simple primitives for throwing and catching an exception, as follows: data Expr = Val Int | Add Expr Expr | Throw | Catch Expr Expr Informally, Catch $\mathrm{x} h$ behaves as the expression $\mathrm{x}$ unless evaluation of $x$ throws an exception, in which case the catch behaves as the handler expression $\mathrm{h}$. An exception is thrown if evaluation of Throw is attempted. To define a semantics for this extended language, we first recall the Maybe type: data Maybe a = Nothing | Just a That is, a value of type Maybe a is either Nothing, which we view here as an exceptional value, or has the form Just $x$, which we view as a normal value. Using this type, our original evaluation function for expressions can be rewritten to take account of exceptions as follows: eval :: Expr $\rightarrow$ Maybe Int eval (Val $\mathrm{n}) \quad=$ Just $\mathrm{n}$ eval (Add $\mathrm{x} \mathrm{y}$ ) = case eval $\mathrm{x}$ of Just $\mathrm{n} \rightarrow$ case eval $\mathrm{y}$ of Just $m \rightarrow$ Just $(n+m)$ Nothing $\rightarrow$ Nothing Nothing $\rightarrow$ Nothing eval Throw = Nothing eval (Catch $\times$ h) $=$ case eval $\mathrm{x}$ of Just $n \rightarrow$ Just $n$ Nothing $\rightarrow$ eval $\mathrm{h}$ Using the approach described in this chapter, calculate a compiler for this language. Hint: this is a challenging exercise!
Close icon
Play audio
Feedback
Powered by NumerAI
Need help? Use Ace
Ace is your personal tutor. It breaks down any question with clear steps so you can learn.
Start Using Ace
Ace is your personal tutor for learning
Step-by-step explanations
Instant summaries
Summarize YouTube videos
Understand textbook images or PDFs
Study tools like quizzes and flashcards
Listen to your notes as a podcast
Continue solving this problem
Create a free account to:
  • View full step-by-step solution
  • Ask follow-up questions with Ace AI
  • Save progress and study later
Continue Free
Numerade

Get step-by-step video solution
from top educators

Continue with Clever
or



By creating an account, you agree to the Terms of Service and Privacy Policy
Already have an account? Log In

A free answer
just for you

Watch the video solution with this free unlock.

Numerade

Log in to watch this video
...and 100,000,000 more!


EMAIL

PASSWORD

OR
Continue with Clever