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.
  • Eq(a,b) requires two Int operands and has type Bool.
  • If(c,t,e) requires a Bool condition and two branches with the same type. Its type is that common branch type.
  • The required checker checks every subexpression. Any failed requirement makes the entire expression an error, including a failure in an unselected branch.

A developer proposes a shortcut checker. Before applying these type rules, it repeatedly replaces If(true,t,e) with t and If(false,t,e) with e, wherever they occur. It discards the other branch without checking it. It never evaluates Add or Eq during this simplification. After simplification, it applies all the required type rules to the remaining expression.


Complete both columns with exactly Int, Bool, or error. Judge the shortcut against the stated specification: checking before optimization is required.

ExpressionRequired checkerShortcut checker
If(true,7,false)
Eq(If(false,true,3),3)
If(Eq(2,2),6,false)
Add(If(false,false,4),2)
If(false,Eq(1,2),true)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Programming Language Design and Construction Questions: