An interpreter evaluates integer expression trees. Num returns its integer, Var looks up a name, and Add and Mul perform ordinary arithmetic. A Let evaluates its initializer in the incoming environment, copies that environment, installs the local binding in the copy, and evaluates its body there.
The caller's environment initially binds p to 7. Evaluate this tree:
Let("p", Num(10), Add(Let("p", Num(4), Mul(Var("p"), Num(3))), Var("p")))
Report the inner Let result, the whole expression result, and the caller's value of p after evaluation. All three are dimensionless integer values; enter exact integers with zero decimal places.
Unlock full course notes by signing up
Distinguish skipped and required evaluations
An integer interpreter starts with an empty environment for each expression. Num returns its integer, and evaluating Var("missing") raises an unbound-name error. Add evaluates both operands from left to right. Let evaluates its initializer before its body. IfZero(test, yes, no) evaluates the test and then only yes when the test is zero, or only no otherwise.
Which supplied trees return an integer without an error? Select all that apply.
Check whether expanding a construct preserves effects
A language for a sensor controller includes next(), which consumes and returns the next integer from a queue of readings. The queue initially contains 4, followed by 9.
The specified operation Square(E) evaluates E once, stores the resulting integer, and multiplies that stored integer by itself. A proposed translation replaces Square(E) with Mul(E, E). Multiplication evaluates its operands once each, from left to right, then multiplies their results. Each occurrence of next() makes a separate call.
Run the original expression Square(next()) and the translated expression Mul(next(), next()) separately, resetting the queue before each run. Find each returned integer and the number of calls to next() during each run. Enter all four answers as exact integers; returned values and call counts are dimensionless.
Expose a binding leak across tests
Two integer interpreters support Num, Var, Add, and Let. Literals, lookups, and addition behave identically in both. Each Let(name, initializer, body) first evaluates its initializer in the incoming environment.
The reference interpreter then copies that environment, installs the binding in the copy, and evaluates the body in the copy. The flawed interpreter instead overwrites the binding directly in the incoming dictionary, evaluates the body there, and never restores the previous binding.
Each interpreter starts with its own dictionary containing x = 2. Run tests P, Q, and R in that order. Within each interpreter, reuse the same dictionary across all three tests.
Detect a checker that optimizes too early
A language implementation assigns each accepted expression a static type, a classification determined without running the program. Its specification requires type checking before optimization and defines these rules:
Integer literals have type Int; true and false have type Bool.
Add(a,b) requires two Int operands and has type Int.