Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight...
Transcript of Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight...
![Page 1: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/1.jpg)
Type-Checking
![Page 2: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/2.jpg)
Announcements
● Programming Project 2 due tonight at 11:59PM.● Office hours from 1:00PM – 3:00PM in Gates 160.
● Programming Project 3 out.● Scope checkpoint due Saturday, July 23 at 11:59PM.● This is a hard deadline, no late days allowed.● Final submission due Wednesday, July 27 at 11:59PM.● Start early; this assignment is significantly larger than the
previous two assignments.
![Page 3: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/3.jpg)
More Announcements
● Programming Assignment 1 graded and returned on paperless.stanford.edu.● Mean: 52.9 / 60● Stdev: 8
● Written Assignment 1 graded. Hard copies returned after class, electronic copies will be emailed later today.● Mean: 20.2 / 24● Stdev: 3
● Let us know ASAP if you haven't heard back from us by tomorrow morning.
![Page 4: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/4.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 5: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/5.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
![Page 6: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/6.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
Interface not declared
Wrong type
Variable not declared
Can't multiply strings
Can't redefine functions
Can't add voidNo main function
![Page 7: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/7.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
Wrong type
Variable not declared
Can't multiply strings
Can't redefine functions
Can't add voidNo main function
![Page 8: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/8.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
Wrong type
Variable not declared
Can't multiply strings
Can't add voidNo main function
![Page 9: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/9.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
Wrong typeCan't multiply
strings
Can't add voidNo main function
![Page 10: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/10.jpg)
Review from Last Timeclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x; x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }
Wrong typeCan't multiply
strings
Can't add void
![Page 11: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/11.jpg)
What Remains to Check?
● Type errors.● Today:
● What are types?● What is type-checking?● A type system for Decaf.
![Page 12: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/12.jpg)
What is a Type?
● This is the subject of some debate.● To quote Alex Aiken:
● “The notion varies from language to language.● The consensus:
– A set of values.– A set of operations on those values”
● Type errors arise when operations are performed on values that do not support that operation.
![Page 13: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/13.jpg)
Types of Type-Checking
● Static type checking.● Analyze the program during compile-time to prove the
absence of type errors.● Never let bad things happen at runtime.
● Dynamic type checking.● Check operations at runtime before performing them.● More precise than static type checking, but usually less
efficient.● (Why?)
● No type checking.● Throw caution to the wind!
![Page 14: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/14.jpg)
Type Systems
● The rules governing permissible operations on types forms a type system.
● Strong type systems are systems that never allow for a type error.● Java, Python, JavaScript, LISP, Haskell, etc.
● Weak type systems can allow type errors at runtime.● C, C++
![Page 15: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/15.jpg)
Type Wars
● Endless debate about what the “right” system is.
● Dynamic type systems make it easier to prototype; static type systems have fewer bugs.
● Strongly-typed languages are more robust, weakly-typed systems are often faster.
![Page 16: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/16.jpg)
Type Wars
● Endless debate about what the “right” system is.
● Dynamic type systems make it easier to prototype; static type systems have fewer bugs.
● Strongly-typed languages are more robust, weakly-typed systems are often faster.
● I'm staying out of this!
![Page 17: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/17.jpg)
Our Focus
● Decaf is typed statically and weakly:● Type-checking occurs at compile-time.● Runtime errors like dereferencing null or an
invalid object are disallowed.
● Decaf uses class-based inheritance.● Decaf distinguishes primitive types and classes.
![Page 18: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/18.jpg)
Typing in Decaf
![Page 19: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/19.jpg)
Static Typing in Decaf
● Static type checking in Decaf consists of two separate processes:● Inferring the type of each expression from the types
of its components.● Confirming that the types of expressions in certain
contexts matches what is expected.
● Logically two steps, but you will probably combine into one pass.
![Page 20: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/20.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
}
![Page 21: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/21.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
}
![Page 22: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/22.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
}
![Page 23: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/23.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
}
Well-typed expression with wrong type.
![Page 24: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/24.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
}
![Page 25: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/25.jpg)
An Example
while (numBitsSet(x + 5) <= 10) {
if (1.0 + 4.0) { /* … */ }
while (5 == null) { /* … */ }
} Expression with type error
![Page 26: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/26.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
![Page 27: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/27.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
+
IntConstant IntConstant
137 42
![Page 28: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/28.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
+
IntConstant IntConstant
137 42
int
![Page 29: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/29.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
+
IntConstant IntConstant
137 42
int int
![Page 30: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/30.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
+
IntConstant IntConstant
137 42
int int
int
![Page 31: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/31.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
![Page 32: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/32.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
=
Identifierx =
Identifiery BoolConstanttruebool bool
bool
![Page 33: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/33.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
=
Identifierx =
Identifiery BoolConstanttruebool bool
boolbool
![Page 34: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/34.jpg)
Inferring Expression Types
● How do we determine the type of an expression?
● Think of process as logical inference.
=
Identifierx =
Identifiery BoolConstanttruebool bool
boolbool
bool
![Page 35: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/35.jpg)
Sample Inference Rules
● “If x is an identifier that refers to an object of type t, the expression x has type t.”
● “If e is an integer constant, e has type int.”
● “If the operands e1 and e
2 of e
1 + e
2 are known to
have types int and int, then e1 + e
2 has type
int.”
![Page 36: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/36.jpg)
Type Checking as Proofs
● We can think of syntax analysis as proving claims about the types of expressions.
● We begin with a set of axioms, then apply our inference rules to determine the types of expressions.
● Many type systems can be thought of as proof systems.
![Page 37: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/37.jpg)
Postconditions
Preconditions
Formalizing our Notation
● We will encode our axioms and inference rules using this syntax:
● This is read “if preconditions are true, we can
infer postconditions.”
![Page 38: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/38.jpg)
Examples of Formal Notation
t FIRST(A)∈
A → Bv is a production.t FIRST(B)∈
ε FIRST(A)∈
A → ε is a production.
ε FIRST(A)∈
A → B1 … B
n is a production.
ε FIRST(B∈i) for 1 ≤ i ≤ n
t FIRST(A)∈
A → B1B
2...B
ntv is a production.
ε FIRST(B∈i) for 1 ≤ i ≤ n
![Page 39: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/39.jpg)
Formal Notation for Type Systems
● We write
⊢ e : Tif the expression e has type T.
● The symbol means “we can infer...”⊢
![Page 40: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/40.jpg)
Our Starting Axioms
![Page 41: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/41.jpg)
Our Starting Axioms
⊢ true : bool ⊢ false : bool
![Page 42: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/42.jpg)
Some Simple Inference Rules
![Page 43: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/43.jpg)
⊢ i : int
i is an integer constant
⊢ s : string
s is a string constant
Some Simple Inference Rules
⊢ d : double
d is a double constant
![Page 44: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/44.jpg)
More Complex Inference Rules
![Page 45: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/45.jpg)
More Complex Inference Rules
⊢ e1 + e
2 : int
⊢ e1 : int
⊢ e2 : int
⊢ e1 + e
2 : double
⊢ e1 : double
⊢ e2 : double
![Page 46: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/46.jpg)
More Complex Inference Rules
⊢ e1 + e
2 : int
⊢ e1 : int
⊢ e2 : int
⊢ e1 + e
2 : double
⊢ e1 : double
⊢ e2 : double
If we can show that e1 and e2 have type int…
![Page 47: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/47.jpg)
More Complex Inference Rules
⊢ e1 + e
2 : int
⊢ e1 : int
⊢ e2 : int
⊢ e1 + e
2 : double
⊢ e1 : double
⊢ e2 : double
If we can show that e1 and e2 have type int…
… then we can show that e1 + e2 has type int as well
![Page 48: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/48.jpg)
Even More Complex Inference Rules
![Page 49: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/49.jpg)
Even More Complex Inference Rules
⊢ e1 == e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
⊢ e1 != e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
![Page 50: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/50.jpg)
Why Specify Types this Way?
● Gives a rigorous definition of types independent of any particular implementation.● No need to say “you should have the same type rules as my
reference compiler.”
● Gives maximum flexibility in implementation.● Can implement type-checking however you want, as long as
you obey the rules.
● Allows formal verification of program properties.● Can do inductive proofs on the structure of the program.
● This is what's used in the literature.● Good practice if you want to study types.
![Page 51: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/51.jpg)
A Problem
![Page 52: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/52.jpg)
A Problem
⊢ x : ??
x is an identifier.
![Page 53: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/53.jpg)
A Problem
⊢ x : ??
x is an identifier.
How do we know the type of x if we don't know what it refers to?
![Page 54: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/54.jpg)
An Incorrect Solution
![Page 55: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/55.jpg)
An Incorrect Solution
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 56: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/56.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 57: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/57.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 58: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/58.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 59: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/59.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double ⊢ x : double
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 60: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/60.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double ⊢ x : double
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 61: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/61.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 62: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/62.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 63: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/63.jpg)
An Incorrect Solution
⊢ x : T
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
x is an identifier.x is in scope with type T.
⊢ d : double
d is a double constant
![Page 64: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/64.jpg)
An Incorrect Solution
⊢ x : T
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
x is an identifier.x is in scope with type T.
⊢ d : double
d is a double constant
⊢ 1.5 : double
![Page 65: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/65.jpg)
An Incorrect Solution
⊢ x : T
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
x is an identifier.x is in scope with type T.
⊢ 1.5 : double
![Page 66: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/66.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ 1.5 : double
⊢ x : T
x is an identifier.x is in scope with type T.
![Page 67: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/67.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ 1.5 : double
⊢ x : T
x is an identifier.x is in scope with type T.
⊢ e1 == e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
![Page 68: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/68.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ 1.5 : double
⊢ x : T
x is an identifier.x is in scope with type T.
⊢ e1 == e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
⊢ x == 1.5 : bool
![Page 69: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/69.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ 1.5 : double
⊢ x : T
x is an identifier.x is in scope with type T.
⊢ e1 == e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
⊢ x == 1.5 : bool
![Page 70: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/70.jpg)
An Incorrect Solution
int MyFunction(int x) { { double x; }
if (x == 1.5) { /* … */ }}
Facts ⊢ x : double
⊢ x : int
⊢ 1.5 : double
⊢ x : T
x is an identifier.x is in scope with type T.
⊢ e1 == e
2 : bool
⊢ e1 : T
⊢ e2 : T
T is a primitive type
⊢ x == 1.5 : bool
Problem?
![Page 71: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/71.jpg)
Strengthening our Inference Rules
● The facts we're proving have no context.● We need to strengthen our inference rules to
remember under what circumstances the results are valid.
![Page 72: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/72.jpg)
Adding Scope
● We write
S ⊢ e : Tif in scope S, the expression e has type T.
● Types are now proven relative to the scope they are in.
![Page 73: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/73.jpg)
Old Rules Revisited
S ⊢ true : bool S ⊢ false : bool
S ⊢ i : int
i is an integer constant
S ⊢ s : string
s is a string constant
S ⊢ d : double
d is a double constant
S ⊢ e1 + e
2 : int
S ⊢ e1 : int
S ⊢ e2 : int
S ⊢ e1 + e
2 : double
S ⊢ e1 : double
S ⊢ e2 : double
![Page 74: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/74.jpg)
A Correct Rule
S ⊢ x : T
x is an identifier.x is a variable in scope S with type T.
![Page 75: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/75.jpg)
A Correct Rule
S ⊢ x : T
x is an identifier.x is a variable in scope S with type T.
![Page 76: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/76.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : ??
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 77: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/77.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : ??
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 78: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/78.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : ??
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 79: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/79.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : ??
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 80: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/80.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : ??
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 81: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/81.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : U
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
![Page 82: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/82.jpg)
Rules for Functions
S ⊢ f(e1, ..., e
n) : U
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : T
i for 1 ≤ i ≤ n
Read rules like this
![Page 83: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/83.jpg)
Rules for Arrays
S e⊢1[e
2] : T
S e⊢1 : T[]
S e⊢2 : int
![Page 84: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/84.jpg)
Rule for Assignment
S e⊢1 = e
2 : T
S e⊢1 : T
S e⊢2 : T
![Page 85: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/85.jpg)
Rule for Assignment
S e⊢1 = e
2 : T
S e⊢1 : T
S e⊢2 : T
Why isn't this rule a problem for this statement?
5 = x;
![Page 86: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/86.jpg)
Rule for Assignment
S e⊢1 = e
2 : T
S e⊢1 : T
S e⊢2 : T
If Derived extends Base, will this rule work for this code?
Base myBase;Derived myDerived;
myBase = myDerived;
![Page 87: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/87.jpg)
Typing with Classes
● How do we factor inheritance into our inference rules?
● We need to consider the shape of class hierarchies.
![Page 88: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/88.jpg)
Single Inheritance
Instructor
LecturerProfessor TA
Keith RiddhiHrysoulaAlexAiken
Animal
Man Bear Pig
![Page 89: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/89.jpg)
Multiple Inheritance
Instructor
LecturerProfessor TA
Keith RiddhiHrysoulaAlexAiken
Animal
Man Bear Pig
ManBearPig
![Page 90: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/90.jpg)
Properties of Inheritance Structures
● Any class is convertible to itself. (Reflexivity)● If A is convertible to B and B is convertible to C,
then A is convertible to C. (Transitivity)● If A is convertible to B and B is convertible to A,
then A and B are the same type. (Antisymmetry)
● This defines a partial order over types.
![Page 91: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/91.jpg)
Types and Partial Orders
● We say that A ≤ B if A is convertible to B.● We have that
● A ≤ A● A ≤ B and B ≤ C implies A ≤ C● A ≤ B and B ≤ A implies A = B
![Page 92: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/92.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : ??
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 93: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/93.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : ??
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 94: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/94.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : ??
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 95: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/95.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : T
1
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 96: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/96.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : T
1
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
Can we do better than this?
![Page 97: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/97.jpg)
Updated Rule for Assignment
S e⊢1 = e
2 : T
2
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 98: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/98.jpg)
Not required in your semantic analyzer, but easy
extra credit!
Updated Rule for Assignment
S e⊢1 = e
2 : T
2
S e⊢1 : T
1
S e⊢2 : T
2
T2 ≤ T
1
![Page 99: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/99.jpg)
Updated Rule for Comparisons
![Page 100: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/100.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
![Page 101: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/101.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 and T
2 are of class type.
T1 ≤ T
2 or T
2 ≤ T
1
![Page 102: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/102.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 and T
2 are of class type.
T1 ≤ T
2 or T
2 ≤ T
1
Can we unify these rules?
![Page 103: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/103.jpg)
The Shape of Types
Engine
DieselEngineCarEngine
DieselCarEngine
![Page 104: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/104.jpg)
The Shape of Types
Engine
DieselEngineCarEngine
DieselCarEngine
bool string doubleint
![Page 105: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/105.jpg)
The Shape of Types
Engine
DieselEngineCarEngine
DieselCarEngine
bool string doubleintArrayTypes
![Page 106: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/106.jpg)
Extending Convertibility
● If A is a primitive or array type, A is only convertible to itself.
● More formally, if A and B are types and A is a primitive or array type:● A ≤ B implies A = B● B ≤ A implies A = B
![Page 107: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/107.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 and T
2 are of class type.
T1 ≤ T
2 or T
2 ≤ T
1
![Page 108: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/108.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 and T
2 are of class type.
T1 ≤ T
2 or T
2 ≤ T
1
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 109: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/109.jpg)
Updated Rule for Comparisons
S ⊢ e1 == e
2 : bool
S e⊢1 : T
S e⊢2 : T
T is a primitive type
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 and T
2 are of class type.
T1 ≤ T
2 or T
2 ≤ T
1
S ⊢ e1 == e
2 : bool
S e⊢1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 110: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/110.jpg)
Updated Rule for Function Calls
S ⊢ f(e1, ..., e
n) : U
f is an identifier.f is a non-member function in scope S.
f has type (T1, …, T
n) → U
S e⊢i : R
i for 1 ≤ i ≤ n
Ri ≤ T
i for 1 ≤ i ≤ n
![Page 111: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/111.jpg)
A Tricky Case
S ⊢ null : ??
![Page 112: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/112.jpg)
Back to the Drawing Board
Engine
DieselEngineCarEngine
DieselCarEngine
bool string doubleintArrayTypes
![Page 113: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/113.jpg)
Back to the Drawing Board
Engine
DieselEngineCarEngine
DieselCarEngine
bool string doubleintArrayTypes
null Type
![Page 114: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/114.jpg)
Handling null
● Define a new type corresponding to the type of the literal null; call it “null type.”
● Define null type ≤ A for any class type A.
● The null type is not accessible to programmers; it's only used internally inside the compiler.
● Many programming languages have types like these.
![Page 115: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/115.jpg)
A Tricky Case
S ⊢ null : ??
![Page 116: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/116.jpg)
A Tricky Case
S ⊢ null : null type
![Page 117: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/117.jpg)
A Tricky Case
S ⊢ null : null type
![Page 118: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/118.jpg)
Object-Oriented Considerations
S ⊢ new T : T
T is a class type.
S ⊢ NewArray(e, T) : T[]
S e : ⊢ int
S ⊢ this : T
S is in scope of class T.
![Page 119: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/119.jpg)
Object-Oriented Considerations
S ⊢ new T : T
T is a class type.
S ⊢ NewArray(e, T) : T[]
S e : ⊢ int
Why don't we need to check if
T is void?
S ⊢ this : T
S is in scope of class T.
![Page 120: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/120.jpg)
What's Left?
● We're missing a few language constructs:● Member functions.● Field accesses.● Miscellaneous operators.
● Good practice to fill these in on your own.
![Page 121: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/121.jpg)
Typing is Nuanced
● The ternary conditional operator ? : evaluates an expression, then produces one of two values.
● Works for primitive types:● int x = random()? 137 : 42;
● Works with inheritance:● Base b = isB? new Base : new Derived;
● What might the typing rules look like?
![Page 122: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/122.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : ??
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 123: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/123.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : ??
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 124: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/124.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : ??
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 125: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/125.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : ??
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 126: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/126.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 127: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/127.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
![Page 128: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/128.jpg)
A Proposed Rule
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
Is this really what we want?
![Page 129: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/129.jpg)
A Small Problem
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
Base
Derived1 Derived2
Super
![Page 130: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/130.jpg)
A Small Problem
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
Base
Derived1 Derived2
Base = random()? new Derived1 : new Derived2;
Super
![Page 131: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/131.jpg)
A Small Problem
S ⊢ cond ? e1 : e
2 : max(T
1, T
2)
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T1 ≤ T
2 or T
2 ≤ T
1
Base
Derived1 Derived2
Base = random()? new Derived1 : new Derived2;
Super
![Page 132: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/132.jpg)
Least Upper Bounds
● An upper bound of two types A and B is a type C such that A ≤ C and B ≤ C.
● The least upper bound of two types A and B is a type C such that:● C is an upper bound of A and B.● If C' is an upper bound of A and B, C ≤ C'.
● When the least upper bound of A and B exists, we denote it A B.∐● (When might it not exist?)
![Page 133: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/133.jpg)
A Better Rule
S ⊢ cond ? e1 : e
2 : T
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T = T1 ∐ T
2Base
Derived1 Derived2
Base = random()? new Derived1 : new Derived2;
Super
![Page 134: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/134.jpg)
… that still has problems
S ⊢ cond ? e1 : e
2 : T
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T = T1 ∐ T
2
Base1
Derived1 Derived2
Base2
Base1 = random()? new Derived1 : new Derived2;
![Page 135: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/135.jpg)
… that still has problems
S ⊢ cond ? e1 : e
2 : T
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T = T1 ∐ T
2
Base1
Derived1 Derived2
Base2
Base1 = random()? new Derived1 : new Derived2;
![Page 136: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/136.jpg)
Multiple Inheritance is Messy
● Type hierarchy is no longer a tree.● Two classes might not have a least upper
bound.● Occurs in Java due to interfaces.● Not a problem in Decaf; there is no ternary
conditional operator.● How to fix?
![Page 137: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/137.jpg)
Minimal Upper Bounds
● An upper bound of two types A and B is a type C such that A ≤ C and B ≤ C.
● A minimal upper bound of two types A and B is a type C such that:● C is an upper bound of A and B.● If C' is an upper bound of C, then it is not true that C' < C.
● Minimal upper bounds are not necessarily unique.● A least upper bound must be a minimal upper bound,
but not the other way around.
![Page 138: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/138.jpg)
A Correct Rule
S ⊢ cond ? e1 : e
2 : T
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T is a minimal upper bound of T1 and T
2
Base1
Derived1 Derived2
Base2
Base1 = random()? new Derived1 : new Derived2;
![Page 139: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/139.jpg)
A Correct Rule
S ⊢ cond ? e1 : e
2 : T
S ⊢ cond : boolS e⊢
1 : T
1
S e⊢2 : T
2
T is a minimal upper bound of T1 and T
2
Base1
Derived1 Derived2
Base2
Base1 = random()? new Derived1 : new Derived2;
Can prove both that expression has type Base1 and that expression has
type Base2.
![Page 140: Type- Checking - KeithSchwarz.com · Type-Checking. Announcements Programming Project 2 due tonight at 11:59PM. Office hours from 1:00PM – 3:00PM in Gates 160. Programming Project](https://reader033.fdocuments.in/reader033/viewer/2022042300/5ecaaa26691ccf233957ba84/html5/thumbnails/140.jpg)
So What?
● Type-checking can be tricky.● Strongly influenced by the choice of operators in the
language.● Strongly influenced by the legal type conversions in
a language.● In C++, the previous example doesn't compile.● In Java, the previous example does compile, but the
language spec is enormously complicated.● See §15.12.2.7 of the Java Language Specification.