cs-4400-sp26

Lecture 7: Introduction to Types

To cover:

What are types?

Designing type systems

Desired properties:

We will define a typing judgment e : t that says when an expression e is in a type t. Note that this is a different activity from writing a type checker or type inferencer, which are algorithms for saying yes/no for a given expression/type pair, or for producing a type for a given expression, respectively. Writing e : t declaratively, with inference rules, is an easier task.

Formal statements of soundness and completeness:

Inference rules for a type system

Syntax: e ::= num n | tt | ff | e + e | if e then e else e

Values: e value

---------      ----------     ---------------
tt value        ff value       (num n) value

Types:

Types t ::= Number | Boolean

Typing rules: Judgment form: e : t

--------- ty/tt      ----------- ty/ff    ------------------ ty/n
tt : Bool             ff : Bool            (num n) : Number

e1 : Number     e2 : Number
--------------------------- ty/+
   (e1 + e2) : Number
   
e : Bool     e1 : t     e2 : t
------------------------------- ty/if
   if e then e1 else e2 : t

Example typing derivation:

Expression: (if ff then 1 else 0) + (2 + 3) : ?

Derivation:

--------- ty/ff  --------- ty/n  --------- ty/n      -------- ty/n  ----- ty/n
ff : Bool        1 : Number       0 : Number         2 : Number    3 : Number
-------------------------------------------- ty/if    ------------------- ty/+
(if ff then 1 else 0) : Number                          (2 + 3) : Number
---------------------------------------------------------------------------- ty/+
(if ff then 1 else 0) + (2 + 3) : Number

Example ill-typed terms:

Exercise: check (informally) that our inference rules cannot type these programs.

Writing a typechecker in Plait

Scope checking

If we have time, we will consider let-expressions and their binding structure in the context of type systems.