Type Checking and Inference
Static Type Checking from Declared Types
Static type checking tests whether a program obeys specified type rules without executing it. You use it to catch incompatible operands, incorrect function arguments, and expressions that violate declared types. Here, every variable, parameter, and function result has an explicit annotation. Your task is to verify those declarations and the expressions that use them.
Expressing a type check
A typing judgment states that an expression has a particular type under given declarations. Use Γ for the declarations and write:
Read this as: under declarations Γ, expression e has type T. The declarations provide information about names; the expression rules determine whether their use is permitted.
For the examples, use a small language with two distinct types. Int represents integers, and Bool represents true or false. Integer literals have type Int; Boolean literals have type Bool. Types must match exactly: there are no implicit conversions. In particular, an integer does not become a Boolean because it is nonzero or appears where a Boolean is required.
A signature such as eligible: Int -> Bool declares an integer parameter and a Boolean result. The signature adjust: (Int, Bool) -> Int declares two parameters, in that order, and an integer result.
Checking one construct at a time
Each construct imposes requirements on its immediate child expressions. For addition, the governing rule is:
The requirements appear above the line. When both hold, the conclusion below the line follows. An annotation on the surrounding expression does not change this rule.
Construct | Required checks | Result type |
|---|---|---|
a + b | Both operands are Int | Int |
a < b | Both operands are Int | Bool |
if c then a else b | Condition is Bool; both branches have the same type T | T |
Function application | Every argument matches its declared parameter type | Declared result type |
For a function with several parameters, compare arguments position by position. Having the right collection of types is insufficient if the arguments appear in the wrong order.
Start with literals and declared names, then check the constructs containing them. For if ready then 6 + 2 else 0, assume ready: Bool.
An annotated binding adds one final comparison: the initializer must have the declared variable type. An annotated function similarly requires its body to have the declared result type.
Example: let count: Int = 13 + 6 passes because addition produces Int, matching the annotation. Changing only the annotation to Bool makes the binding fail; it does not change the addition result's type.
What acceptance establishes
The conditional rule checks both branches, including when its condition is a literal such as true. This checker applies the stated rules to program structure rather than executing the condition to discard a branch.
However, static acceptance does not promise successful execution. Suppose divide: (Int, Int) -> Int performs integer division and raises a runtime error when its second argument is zero. The call divide(12, 0) satisfies the argument-type requirements. Under a checker with no additional value analysis, it passes despite the runtime failure.
Other analyses can detect additional faults, but you should not assume checks beyond those specified. Type acceptance also does not establish that a calculation implements the intended algorithm or that a function terminates.
What to remember
- Begin with declared names and literal types, then check enclosing constructs.
- Compare operands, arguments, initializers, and function bodies with their required types.
- Check both conditional branches under the stated rules.
- Separate type acceptance from runtime success and intended behaviour.
Example: Checking a Dispatch Calculation
Example: A warehouse dashboard calculates the number of parcels scheduled for dispatch. When dispatch is enabled, it adds eight staged parcels to the packed count. Otherwise, it schedules zero parcels.
The program uses distinct Int and Bool types with no implicit conversions. Integer literals have type Int, Boolean literals have type Bool, and addition requires two Int operands and returns Int. A conditional requires a Bool condition and two branches of the same type. That common branch type is the conditional's type. Function arguments and annotated results must match exactly.
fun dispatch(packed: Int, ready: Bool): Int =
if ready then packed + 8 else 0Assume integer arithmetic has no overflow in this example.
a) Check the original function body against its declared result type.
The parameter declarations give ready type ________ and packed type ________. Therefore, the condition has the required Boolean type.
The literal 8 has type Int. Since both operands of packed + 8 are integers, the addition rule gives:
The alternative branch, 0, also has type Int. Both branches agree, so the conditional body has type Int.
Finally, compare this body type with the function's declared result type, also Int. The original function passes static type checking. No parcel count needs to be calculated to establish this result.
b) A programmer changes only else 0 to else false. Does the revised definition pass? If it fails, repair that branch without changing the signature or the intended zero-parcel behaviour when dispatch is disabled.
The condition still has type Bool, and packed + 8 still has type Int. Consequently, the other branch must have type ________ for the conditional rule to succeed.
However, false has type Bool. A type mismatch occurs when an expression supplies a different type from the one required. Here, the conditional's branches disagree, and the Boolean branch also conflicts with the function's integer result requirement.
Both branches must be checked, regardless of which one a particular call selects. The programmer cannot justify this definition by saying dispatch is usually enabled.
The revised definition is rejected. Replace else false with else 0 to restore matching integer branches and preserve the required disabled-dispatch behaviour.
c) Using the original definition, check and evaluate dispatch(21 + 4, true). State the number of scheduled parcels.
First check the arguments. Both operands of 21 + 4 are integer literals, so the argument has type Int. The literal true has type Bool. These match the parameter types (Int, Bool), and part a established that the original body satisfies its result annotation. The call therefore passes static type checking.
Now evaluate the call. The first argument gives a packed count of ________, and ready receives true. The conditional selects packed + 8, giving:
The dashboard schedules 33 parcels. Checking established that the types fit; evaluating the selected branch produced the parcel count.
A compiler uses a language in which integer literals have type Int and addition takes two Int operands and returns Int. Consider these two declarations:
let total = 14 + 9
let limit: Int = 20 + 5Complete the descriptions using inference and checking, each exactly once. Focus on where the required type comes from.
Deriving the missing type of total from its initializer is type . Verifying that the initializer of limit satisfies its supplied annotation is type .
A stock-monitoring program has the declarations below. Check each table expression independently. Integer literals have type Int; true and false have type Bool. Addition requires two Int operands and returns Int. Function arguments must match their declared parameter types exactly. A conditional requires a Bool condition and two branches of the same type, which becomes its result type. There are no implicit conversions.
count: Int
active: Bool
flag: Int -> BoolEnter accepted or rejected in Status. For an accepted expression, enter Int or Bool in Result type. For a rejected expression, enter none in Result type.
| Expression | Status | Result type |
| count + 5 | ||
| if active then 16 else 3 | ||
| if active then count else false | ||
| flag(count) | ||
| flag(active) |