Lec14 Type Checking
Lec14 Type Checking
Stephen Chong
https://www.seas.harvard.edu/courses/cs153
Contains content from lecture notes by Steve Zdancewic and Greg Morrisett
Announcements
•Type checking
•Judgments and inference rules
Parsing
Fro
nt Elaboration
en
d
Lowering
Ba
ck
end
Optimization
Code Generation
Target Code
Stephen Chong, Harvard University 4
Elaboration
type tipe =
Int_t
| Arrow_t of tipe*tipe
| Pair_t of tipe*tipe
type exp =
Var of var | Int of int
| Plus_i of exp*exp Note: function
| Lambda of var * tipe * exp arguments have
| App of exp*exp type annotation
| Pair of exp * exp
| Fst of exp | Snd of exp
•Adding booleans...
type tipe = ... | Bool_t
•Inference rule
•If the premises are true, then the conclusion is true
•An axiom is a rule with no premises
•Inference rules can be instantiated by replacing
metavariables (e, e1, e2, x, i, ...) with expressions, program
variables, integers, as appropriate.
Stephen Chong, Harvard University 19
Why Inference Rules?
•Compact, precise way of specifying language properties.
•E.g. ~20 pages for full Java vs. 100’s of pages of prose Java Language Spec.
•Inference rules correspond closely to the recursive AST traversal that
implements them
•Type checking (and type inference) is nothing more than attempting to prove
a different judgment ( E ⊢ e : t ) by searching backwards through the rules.
•Compiling in a context is nothing more than a collection of inference rules
specifying yet a different judgment ( E ⊢ src ⇒ target )
•Moreover, the compilation rules are very similar in structure to the typechecking rules
•Strong mathematical foundations
•The “Curry-Howard correspondence”: Programming Language ~ Logic,
Program ~ Proof, Type ~ Proposition
•See CS152 if you’re interested in type systems!
x :T ∈ E E ⊢ e1 : int E ⊢ e2 : int
E ⊢ i : int E ⊢ x :T E ⊢ e1 + e2 : int
FUN APP
E, x : T ⊢ e : S E ⊢ e1 : T -> S E ⊢ e2 : T
E, x : T ⊢ e : S E ⊢ e1 : T -> S E ⊢ e2 : T
E ⊢ fun (x:T) -> e : T -> S E ⊢ e1 e2 : S
Stephen Chong, Harvard University 23
Example Derivation Tree
x : int ∈ x : int
VAR INT
x : int ⊢ x : int x : int ⊢ 3 : int
ADD
x : int ⊢ x + 3 : int
FUN INT
⊢ (fun (x:int) -> x + 3) : int -> int ⊢ 5 : int
APP
⊢ (fun (x:int) -> x + 3) 5 : int
•Note: the OCaml function typecheck verifies the existence of this tree. The
structure of the recursive calls when running tc is same shape as this tree!
•Note that x : int ∈ E is implemented by the function lookup
INDEX
Note: These rules don’t
E ⊢ e1 : T[] E ⊢ e2 : int ensure that the array index is
E ⊢ e1[e2] : T in bounds – that should be
checked dynamically.
UPDATE
E ⊢ e1 : T[] E ⊢ e2 : int E ⊢ e3 : T
E ⊢ e1[e2] = e3 ok
Stephen Chong, Harvard University 26
Tuples
PROJ
E ⊢ e : T1 * … * Tn 1 ≤ i ≤ n
E ⊢ #i e : Ti
DEREF
E ⊢ e : T ref
E ⊢ !e : T
Note the similarity with the rules
ASSIGN for arrays…
E ⊢ e1 : T ref E ⊢ e2 : T
E ⊢ e1 := e2 : unit
Stephen Chong, Harvard University 28
Oat Type Checking
• C ⊢ 37 + 5 : int
•What is ⟦ C ⊢ 37 + 5 : int⟧ ?
⟦C ⊢ 37 + 5 : int⟧ = (i64, %tmp, [%tmp = add i64 (Const 37) (Const 5)])
x:t2L x : t 2 L G; L ` exp : t
typ var typ assn
<latexit sha1_base64="K5iBfmcfG0Ps8F071fnXZSt8v90=">AAACHnicbVBNS8NAFNzUrxq/qh69LBbBU0m0oOil6EEPHhSsCk0om+3GLt1swu5LaQn5I578KZ4EBfHgRf+N25qDtg4sDDNvePsmSATX4DhfVmlmdm5+obxoLy2vrK5V1jdudJwqypo0FrG6C4hmgkvWBA6C3SWKkSgQ7DbonY782z5TmsfyGoYJ8yNyL3nIKQEjtSt1L1SEZgN8hAF7XOKLPDs7vvD6HaK7eCznHrABaJqZvNfuE5W3K1Wn5oyBp4lbkCoqcNmufHidmKYRk0AF0brlOgn4GVHAqWC57aWaJYT2yD1rGSpJxLSfja/L8Y5ROjiMlXkS8Fj9nchIpPUwCsxkRKCrJ72R+J/XSiE89DMukxSYpD+LwlRgiPGoKtzhilEQQ0MIVdz8FdMuMXWBKdS2TQvu5M3T5Gav5u7X9q7q1cZJ0UcZbaFttItcdIAa6Bxdoiai6AE9oRf0aj1az9ab9f4zWrKKzCb6A+vzG3nxoeo=</latexit>
G; L ` x : t <latexit sha1_base64="njp3eviXzds4eDbNDhv3Lt/Bfc4=">AAACS3icbZDPaxNBFMdno9U2/or26OVhEDyF3SooBqHooT3kUItpC5kQ3s7OJkNnZ9eZtzVh2b/PkydP/hE9CRXEQ2fTPWjrg4Evn+9782a+caGVozD8EXRu3d64c3dzq3vv/oOHj3qPnxy5vLRCjkWuc3sSo5NaGTkmRVqeFFZiFmt5HJ9+aPzjM2mdys0nWhVymuHcqFQJJI9mPeSpRVEt4S0QcGVgBPxziQnsDUf8LEG3ALksGreuPBpaaukS3jXOEPihmi8Irc2/wKjmJJfkROV38Rk6Z+pZrx8OwnXBTRG1os/aOpj1vvMkF2UmDQntr5hEYUHTCi0poWXd5aWTBYpTnMuJlwYz6abVOooannuSQJpbfwzBmv49UWHm3CqLfWeGtHDXvQb+z5uUlL6ZVsoUJUkjrhalpQbKockVEmWlIL3yAoVV/q0gFuizJZ9+t+tTiK7/+aY42hlELwc7H1/1d9+3eWyyp+wZe8Ei9prtsn12wMZMsK/snF2wX8G34GfwO/hz1doJ2plt9k91Ni4BKJOyRA==</latexit>
G; L; rt ` x = exp; ) L
as expressions as addresses
(which denote values) (which can be assigned)