
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























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
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”
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)
Type Judgements^ ^ Type judgment notation: A
b: bool » b: bool, x: int
if (b) 2 else x : int »^
2 + 2 : int
General Rule^ ^ For any environment A, expression E,statements S1 and S2, the judgement:» A^
if (E) S1 else S2 : T
Inference Rules^ A^ E : bool
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
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
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
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
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)
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
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)