Reconstructing interfaces across nested calls

A package-scoring program calls two external functions, measure and eligible, whose annotations are missing. Their bodies are unavailable, but each accepts one argument and has one fixed parameter type and one fixed result type throughout the program.


Use these language rules:

  • The available types are Int, Bool, and String.
  • Integer literals have type Int; quoted text has type String.
  • + requires two Int operands and returns Int.
  • An if condition must be Bool, and its branches must return the same type.
  • score also has one fixed parameter type and one fixed result type.
  • There are no implicit conversions or alternative signatures for a function name.


Plain text
function score(value):
    if eligible(measure(value)):
        return measure(value) + 1
    else:
        return 0

score("crate")
score(7)


First infer the missing types from the function body and the call score("crate"). Report the parameter and result types of measure, the parameter and result types of eligible, and the result type of score. Enter each type as Int, Bool, or String.


Then enter true if the entire program, including score(7), can type-check under those rules, or false otherwise. The second call cannot give an existing function a second signature.

Your answers

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 Type Checking and Inference Questions: