Docsity

Static Semantics: Type Judgments & Inference Rules in EECS 483 at UMich, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Static Semantics: Type Judgments & Inference Rules in EECS 483 at UMich and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis III Static Semantics^ EECS 483 – Lecture 14^ University of Michigan^ Wednesday, October 25, 2006^ Guest Speaker: Simon Chen

  • 1 -

Announcements^ ™^ Exam 1 review^ » Monday (10/30) in class^ ™^ Exam 1^ » Wednes (11/1) in class^ ™^ Project 2^ » Due Monday (10/30) midnight^ ™^ Reading - None^ » Static semantics is not covered in the book

  • 3 -

Type Judgments or Relations ™^ Static semantics = formal notation whichdescribes type judgments:^ » E : T^ » means “E is a well-typed expression of type T”^ » E is typable if there is some type T such that E : T ™^ Type judgment examples:^ » 2 : int^ » true : bool^ » 2 * (3 + 4) : int^ » “Hello” : string

  • 4 -

Type Judgments for Statements^ ™^ Statements may be expressions (i.e., representvalues)^ ™^ Use type judgments for statements:^ » if (b) 2 else 3 : int^ » x == 10 : bool^ » b = true, y = 2 : int

(result of comma operator is the value of the rightmost expression) ™ For statements which are not expressions: use aspecial unit type (void or empty type) » S : unit » means “S is a well-typed statement with no result type”

  • 6 -

Deriving a Judgment^ ™^ Consider the judgment^ » if (b) 2 else 3 : int^ ™^ What do we need to decide that this is awell-typed expression of type int?^ » b must be a bool (b : bool)^ » 2 must be an int (2 : int)^ » 3 must be an int (3 : int)

  • 7 -

Type Judgements^ ™^ Type judgment notation: A

E : T

» Means “In the context A, the expression E is awell-typed expression with type T” ™ Type context is a set of type bindings: id : T» (i.e. type context = symbol table)» b: bool, x: int

b: bool » b: bool, x: int

if (b) 2 else x : int »^

2 + 2 : int

  • 9 -

General Rule^ ™^ For any environment A, expression E,statements S1 and S2, the judgement:» A^

if (E) S1 else S2 : T

™^ Is true if:» A^

⊥ ⊥ E : bool⊥» A S1 : T⊥» A S2 : T

  • 10 -

Inference Rules^ A^ E : bool

A^

S1 : T^

A^ S2 : T

if-rule^ ⊥^ A^ if (E) S1 else S2 : T

premises conclusion

-^ Read as, “if we have established the statements in the premises listed above the line, then we may derive the conclusion below the line”^ • Holds for any choice of E, S1, S2, T

  • 12 -

Meaning of Inference Rule^ ™^ Inference rule says:^ » Given the premises are true (with somesubstitutions for A, E1, E2)^ » Then, the conclusion is true (with consistentsubstitution)^ A^ ⊥ ⊥E1 : intA E2 : int

(+) ⊥ A E1 + E2 : int

E^ :int^ :intE2^ +^ :int E1^ E

  • 13 -

Proof Tree^ ™^ Expression is well-typed if there exists atype derivation for a type judgment^ ™^ Type derivation is a proof tree^ ™^ Example: if A1 = b : bool, x : int, then:^ ⊥ ⊥^ A1^ b : boolA1^ !b : bool

⊥ A1 2 : int

⊥A1 3 : int ⊥ A1 2 + 3 : int

⊥A1 x : int

b : bool, x : int

⊥ if (!b) 2 + 3 else x : int

  • 15 -

Class Problem Given the following syntax for arithmetic expressions: t ::=^ true^ false^ if t then t else t^0 succ t^ pred t^ iszero t

And the following typing rulesfor the language:true : boolfalse : boolt1: bool^ t2: T

t3 : T if t1 then t2 else t3 : Tt1 : intsucc t1 : intt1 : intpred t1 : intt1 : intiszero t1 : bool Construct a type derivations to show(1) if iszero 0 then 0 else pred 0 : int(2) pred(succ(iszero(succ(pred(0)))) : int

  • 16 -

Assignment Statements^ id : T

∈^ A ⊥A E : T ⊥ A id = E : T

(variable-assign) ⊥ A E3 : T ⊥A E2 : int⊥A E1 : array[T] ⊥ A E1[E2] = E3 : T

(array-assign)

  • 18 -

Class Problem 1.^ Show the inference rule for a while statement, while (E) S 2.^ Show the inference rule for a variable declaration^ with initializer, Type id = E 3.^ Show the inference rule for a question mark/colon operator,^ E1? S1 : S

  • 19 -

Sequence Statements^ ™^ Rule: A sequence of statements is well-typed if the first statement is well-typed,and the remaining are well-typed as well:

⊥ A S1 : T1 ⊥A (S2; .... ; Sn) : Tn^ ⊥ A (S1; S2; .... ; Sn) : Tn^

(sequence)