CSE411

CSE411: Introduction to Compilers

Type Checking

Jooyong Yi (UNIST)

2026 Fall
CSE411

Example

void foo() {
    int x;
    int y;
    y = x;
}
  • Is the above program type-safe?
2026 Fall
CSE411

Example

void foo() {
    int i;
    char *s;
    i = s;
}
  • Is the above program type-safe?
2026 Fall
CSE411

Can you give me a proof?

2026 Fall
CSE411

Is your proof machine-checkable?

2026 Fall
CSE411

Proof Tree

------------------------   ------------------------
 [x:int][y:int] ⊢ y:int     [x:int][y:int] ⊢ x:int
-------------------------------------------------------
                 [x:int][y:int] ⊢ (y = x;): unit
               ----------------------------------
                 [x:int] ⊢ (int y; y = x;): unit
               -----------------------------------
                   ⊢ (int x; int y; y = x;): unit
2026 Fall
CSE411

Judgement

  • An assertion about the typing of programs
  • Syntax: Γ ⊢ J (we read this as "Γ entails J")
    • Γ: A typing environment
    • J: An assertion
[x:int][y:int] ⊢ x:int
               ⊢ (int x; int y; y = x;): unit
2026 Fall
CSE411

Revisiting Proof Tree

------------------------   ------------------------
 [x:int][y:int] ⊢ y:int     [x:int][y:int] ⊢ x:int
-------------------------------------------------------
                 [x:int][y:int] ⊢ (y = x;): unit
               ----------------------------------
                 [x:int] ⊢ (int y; y = x;): unit
               -----------------------------------
                   ⊢ (int x; int y; y = x;): unit
2026 Fall
CSE411

Inference Rule

      Γ[I:int] ⊢ (S): unit        # premises
---------------------------
      Γ ⊢ (int I; S): unit        # conclusion  
2026 Fall
CSE411

Other Inference Rules

   Γ ⊢ I:τ  Γ ⊢ E:τ            # premises
-------------------------
   Γ ⊢ (I = E): unit           # conclusion  
2026 Fall
CSE411

Other Inference Rules

 Γ ⊢ E: boolean   Γ ⊢ S: unit
-------------------------------
  Γ ⊢ (if (E) S): unit
2026 Fall
CSE411

Other Inference Rules

 Γ ⊢ E: boolean   Γ ⊢ S: unit
--------------------------------
  Γ ⊢ (while (E) S): unit
2026 Fall
CSE411

Other Inference Rules

 Γ ⊢ S1: unit   Γ ⊢ S2: unit
-----------------------------
  Γ ⊢ (S1; S2): unit
2026 Fall
CSE411

Other Inference Rules

   Γ ⊢ E1: int   Γ ⊢ E2: int
------------------------------
      Γ ⊢ E1 + E2: int
2026 Fall
CSE411

Other Inference Rules

   Γ ⊢ E1: int   Γ ⊢ E2: int
------------------------------
      Γ ⊢ E1 + E2: int
    Γ ⊢ E1: τ   Γ ⊢ E2: τ
------------------------------
      Γ ⊢ E1 == E2: boolean
2026 Fall
CSE411

Other Inference Rules

   Γ ⊢ P: τ_1 × τ_2 × ... × τ_n → τ   Γ ⊢ E_k: τ_k (for k = 1, ..., n)
 -----------------------------------------------------------------------
           Γ ⊢ P(E1, E2, ..., En): τ
2026 Fall
CSE411

Revisiting Proof Tree

------------------------   ------------------------
 [x:int][y:int] ⊢ y:int     [x:int][y:int] ⊢ x:int
-------------------------------------------------------
                 [x:int][y:int] ⊢ (y = x;): unit
               ----------------------------------
                 [x:int] ⊢ (int y; y = x;): unit
               -----------------------------------
                   ⊢ (int x; int y; y = x;): unit
2026 Fall
CSE411

Axioms

  • Facts that are accepted as true without requiring any further justification.
--------------
 Γ[x:τ] ⊢ x:τ
-----------------
  Γ ⊢ n: int        # n is an integer constant
2026 Fall
CSE411

How can we generate a proof tree mechanically?

------------------------   ------------------------
 [x:int][y:int] ⊢ y:int     [x:int][y:int] ⊢ x:int
-------------------------------------------------------
                 [x:int][y:int] ⊢ (y = x;): unit
               ----------------------------------
                 [x:int] ⊢ (int y; y = x;): unit
               -----------------------------------
                   ⊢ (int x; int y; y = x;): unit
2026 Fall
CSE411

Implementing an Inference Rule

   Γ ⊢ I:τ  Γ ⊢ E:τ            
-------------------------
   Γ ⊢ (I = E): unit             
public boolean visit(Assignment n) {
  n.getLeftHandSide().accept(this);
  Type l = getResult();
  n.getRightHandSide().accept(this);
  Type r = getResult();
  if (l != r)
    throw new Error(n, "Type mismatch in \"" + n + "\": " + l + " = " + r);
  return false;
}
2026 Fall
CSE411

Implementing an Inference Rule

   Γ ⊢ E1: int   Γ ⊢ E2: int
------------------------------
      Γ ⊢ E1 + E2: int
public boolean visit(InfixExpression n) {
  n.getLeftOperand().accept(this);
  Type l = getResult();
  n.getRightOperand().accept(this);
  Type r = getResult();
  InfixExpression.Operator o = n.getOperator();
  if (o == InfixExpression.Operator.PLUS) {
    if (l != tf.Int)
      throw new Error(n,
          "Expecting an int type expression as the left-hand operand of \""
              + o + "\" in \"" + n + "\"");
    if (r != tf.Int)
      throw new Error(n,
          "Expecting an int type expression as the right-hand operand of \""
              + o + "\" in \"" + n + "\"");
    setResult(n, tf.Int);
  }
  return false;                                                                                                          
2026 Fall
CSE411

Type Error

--------------------------      ----------------------------
 [i:int][s:char*] ⊢ i:int       [i:int][s:char*] ⊢ s:char*     
------------------------------------------------------------
                 [i:int][s:char*] ⊢ (i = s): unit              
               -------------------------------------
                 [i:int] ⊢ (char *s; i = s): unit
               ------------------------------------------------
                   ⊢ (int i; char *s; i = s): unit 
2026 Fall
CSE411

Benefits of Using Inference Rules

  • Modular and compositional reasoning
    • With a finite set of rules, we can prove the correctness of any program.
2026 Fall
CSE411

Logical System

  • A type-checking system is an example of a logical system.
    • A logical system consists of axioms and inference rules.
2026 Fall
CSE411

Two Fundamental Questions

  1. Soundness: P ⊢ Q ⇒ P ⊧ Q
  2. Completeness: P ⊧ Q ⇒ P ⊢ Q
2026 Fall
CSE411

Soundness

  • ⊢ (int x; int y; y = x;): unit ⇒ ⊧ (int x; int y; y = x;): unit
  • The compiler says the given program is type-safe. Is the program really type-safe?
2026 Fall
CSE411

Completeness

  • ⊧ (int x; int y; y = x;): unit ⇒ ⊢ (int x; int y; y = x;): unit
  • I know that the given program should be type-safe. Can the compiler prove that the program is type-safe?
2026 Fall
CSE411

Completeness

import java.util.List;
import java.util.ArrayList;

public class Incomplete {
    static class C {}
    static class D extends C {}

    public static void main(String args[]) {
        List<C> lst = new ArrayList<>();
        if (args[0].equals("1")) {
            lst.add(new D());
        } else {
            lst.add(new D());
        }
        D d = lst.get(0);
    }
}                                                                                                                   
  • Is the above Java program type-safe?
2026 Fall
CSE411

Completeness

import java.util.List;
import java.util.ArrayList;

public class Incomplete {
    static class C {}
    static class D extends C {}

    public static void main(String args[]) {
        List<C> lst = new ArrayList<>();
        if (args[0].equals("1")) {
            lst.add(new D());
        } else {
            lst.add(new D());
        }
        D d = lst.get(0);
    }
}                                                                                                                   
Incomplete.java:15: error: incompatible types: C cannot be converted to D
        D d = lst.get(0);
                     ^
1 error                                                                                                          
2026 Fall
CSE411

Sound Type System: Over-approximation

center

  • A sound type system may reject some programs that are type-safe.
2026 Fall