Questions: Type Inference Algorithms

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

When type inference infers that `fun x -> x` has type `α → α`, why doesn't it infer the more specific type `int → int`?

AThe inference algorithm only infers types for expressions applied to integer arguments it has already seen
BThe algorithm finds the most general unifier — if no constraint forces α to be int, it leaves α as a free type variable, yielding a polymorphic type
CThe algorithm cannot infer numeric types and defaults to generic type variables for all primitives
DThe identity function is a special case hard-coded as polymorphic in the type system
Question 2 Multiple Choice

During constraint generation for a function application `f(a)`, what constraint does the compiler produce?

AThe types of f and a must be equal
BThe type of f must be a function type α → β, where α equals the type of a, and β is a fresh type variable for the result
CThe type of a must be a subtype of the domain type expected by f
Df must have a concrete, fully-known function type at compile time
Question 3 True / False

Type inference eliminates compile-time type safety, because without explicit annotations the compiler can seldom fully verify that types are used correctly.

TTrue
FFalse
Question 4 True / False

The 'occurs check' in type inference catches situations where solving constraints would require a type variable to equal a type expression that contains that same variable.

TTrue
FFalse
Question 5 Short Answer

Describe the two main phases of type inference and explain what role unification plays in the second phase.

Think about your answer, then reveal below.