type checking - cornell university · type checking: if e1has type booland e2has type tand e3has...

18
Type Checking Today’s music: Check Yo Self by Ice Cube Nate Foster Spring 2019

Upload: others

Post on 23-May-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Type Checking

Today’s music: Check Yo Self by Ice Cube

Nate FosterSpring 2019

Page 2: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Review

Previously in 3110: formal semantics• Dynamic semantics: small-step relation• Substitution operation

Today:• Formal static semantics

Page 3: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

REVIEW: SUBSTITUTION MODEL

Demo

Page 4: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

FORMAL STATIC SEMANTICS

Page 5: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Question

What do we get when we evaluate the following?step Add(Bool false, Int 42)

A.Int 42B.Int 43C.Add(Bool 42, Int 42)D.It goes into an infinite loopE.None of the above

Page 6: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static semantics

We can have nonsensical expressions:5 + falseif 5 then true else 0

Need to rule those out...

Page 7: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

if expressions [from lec 2]

Syntax:if e1 then e2 else e3

Type checking:if e1 has type bool and e2 has type t and e3 has type tthen if e1 then e2 else e3 has type t

Page 8: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static semantics

Defined as a ternary relation:T |- e : t

• Read as in typing context T, expression e has type t• Turnstile |- can be read as "proves" or "shows"• You're already used to e : t, because utop uses that

notation• Typing context is a dictionary mapping variable names

to types

Page 9: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Types

type typ = | Tint | TBool

Page 10: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static Semantics: Constants

T |- i : Int

T |- b : Bool

Page 11: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static Semantics: Integers

T |- i : Int

Page 12: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static Semantics: Variables

T |- x : tif T(x) = t

Page 13: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static Semantics: Conditionals

T |- if e1 then e2 else e3 : tif T |- e1 : bool

and T |- e2 : t and T |- e3: t

Page 14: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static Semantics: Let Expressions

T |- let x = e1 in e2 : tif T |- e1 : t1

and T,x:t1 |- e2 : t

Page 15: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static semantics

e.g., x:int |- x + 2 : intx:int,y:int |- x < y : bool|- 5 + 2 : int

Demo

Page 16: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Static semantics

e.g., x:int |- x + 2 : intx:int,y:int |- x < y : bool|- 5 + 2 : int

Demo

Page 17: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Purpose of type systemEnsure type safety: well-typed programs don't get stuck:• haven't reached a value, and• unable to evaluate further

Lemmas:Progress: if e:t, then either e is a value or e can take a step.Preservation: if e:t, and if e takes a step to e', then e':t.

Type safety = progress + preservationProving type safety is a fun part of CS 4110

Page 18: Type Checking - Cornell University · Type checking: if e1has type booland e2has type tand e3has type t then if e1 then e2 else e3has type t. Static semantics Defined as a ternary

Upcoming events

• [Wednesday/Thursday] Beta demos• [Thursday] Guest Lecture by Yaron Minsky

This is not a substitute.

THIS IS 3110