example to type check object world { var z : boolean var u : int def f(y : boolean) : int { z = y if...
TRANSCRIPT
![Page 1: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/1.jpg)
Example to Type Check
object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z } else { 0 } }}
¡0 = { (z, Boolean), (u, Int), (f, Boolean Int) }
¡1 = ¡0 © {(y, Boolean)}
Exercise:
![Page 2: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/2.jpg)
Overloading of Operators
Not a problem for type checking from leaves to root
Int £ Int Int
String £ String String
![Page 3: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/3.jpg)
Arrays
Using array as an expression, on the right-hand side
Assigning to an array
![Page 4: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/4.jpg)
Example with Arrays
def next(a : Array[Int], k : Int) : Int = { a[k] = a[a[k]]}
Given ¡ = {(a, Array(Int)), (k, Int)}, check ¡ ` a[k] = a[a[k]]: Int
![Page 5: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/5.jpg)
Type Rules (1)
variable constant
function application
plus
if
assignmentwhile
![Page 6: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/6.jpg)
Type Rules (2)
array use
arrayassignment
block
![Page 7: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/7.jpg)
Type Rules (3)
field use
field assignment
¡c - top-level environment of class C
class C { var x: Int; def m(p: Int): Boolean = {…}
}
¡c = { (x, Int), (m, C £ Int Boolean)}
method invocation
![Page 8: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/8.jpg)
DIVING DEEPER INTO TYPES
![Page 9: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/9.jpg)
Meaning of Types
• Types can be viewed as named entities– explicitly declared classes, traits– their meaning is given by methods they have– constructs such as inheritance establish
relationships between classes• Types can be viewed as sets of values
– Int = { ..., -2, -1, 0, 1, 2, ... }– Boolean = { false, true }– Int Int = { f : Int -> Int | f is computable }
![Page 10: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/10.jpg)
Types as Sets• Sets so far were disjoint
• Sets can overlap
Booleantrue, false
String“Richard” “cat”
Int Int
Int Pos
Int
Pos (1 2)
Neg (-1)
16 bit class C
class F
class Dclass E
F extends D, D extends C
C
ED
F
![Page 11: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/11.jpg)
SUBTYPING
![Page 12: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/12.jpg)
Subtyping
• Subtyping corresponds to subset• Systems with subtyping have non-disjoint sets• T1 <: T2 means T1 is a subtype of T2
– corresponds to T1 µ T2 in sets of values
• Main rule for subtyping ¼ corresponds to
![Page 13: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/13.jpg)
Types for Positive and Negative IntsInt = { ... , -2, -1, 0, 1, 2, ... }Pos = { 1, 2, ... } (not including zero)
Neg = { ..., -2, -1 } (not including zero)
Pos <: IntNeg <: Int
Pos µ IntNeg µ Int
(y not zero)
(x/y well defined)
![Page 14: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/14.jpg)
More Rules
More rules for division?
![Page 15: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/15.jpg)
Making Rules Useful
• Let x be a variable
if (y > 0) { if (x > 0) { var z : Pos = x * y res = 10 / z} } type system proves: no division by zero
![Page 16: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/16.jpg)
Subtyping Example
def f(x:Int) : Pos = { if (x < 0) –x else x+1}
var p : Posvar q : Int
q = f(p)
Pos <: Int
¡ : f: Int Pos
Does this statement type check?
![Page 17: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/17.jpg)
Using Subtyping
def f(x:Pos) : Pos = { if (x < 0) –x else x+1}
var p : Intvar q : Int
q = f(p)
- does not type check
Pos <: Int
¡ : f: Pos Pos
![Page 18: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/18.jpg)
What Pos/Neg Types Can Do
def multiplyFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q1, q1*q2)}def addFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q2 + p2*q1, q1*q2)}def printApproxValue(p : Int, q : Pos) = { print(p/q) // no division by zero}
More sophisticated types can track intervals of numbers and ensure that a program does not crash with an array out of bounds error.
![Page 19: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/19.jpg)
Subtyping and Product Types
![Page 20: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/20.jpg)
Using Subtyping
def f(x:Pos) : Pos = { if (x < 0) –x else x+1}
var p : Intvar q : Int
q = f(p)
- does not type check
Pos <: Int
¡ : f: Pos Pos
![Page 21: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/21.jpg)
Subtyping for Products
T1 <: T2 implies for all e:
So, we might as well add:
covariant subtyping for pairsPair [T1, T2]
Type for a tuple:
![Page 22: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/22.jpg)
Analogy with Cartesian Product
A £ B = { (a, b) | a 2 A, b 2 B}
![Page 23: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/23.jpg)
Subtyping and Function Types
![Page 24: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/24.jpg)
Subtyping for Function Types
Consequence:
contravariance covariance
T1 <: T2 implies for all e:
Function [T1, T]
contra- co-
as if ¡ ` m: T1’ £ … £ Tn’ \to T’
![Page 25: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/25.jpg)
To get the appropriate behavior we need to assign sets to function types like this:
T1 T2 = { f| 8 x. (x 2 T1 f(x) 2 T2)}
We can prove
Function Space as Set
contravariance becausex 2 T1 is left of implication
T1 £ T2
(: x 2 T1) Ç f(x) 2 T2
![Page 26: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/26.jpg)
Proof T1 T2 = { f | 8 x 2 T1 f(x) 2 T2 }
• Let T1’ µ T1 and T2 µ T2’ and f 2 T1 T2
• 8 x. x 2 T1 f(x) 2 T2
• Let x 2 T1’. From T1’ µ T2, also x 2 T1
• f(x) 2 T2. By T2 µ T2’, also f(x) 2 Ts’
• 8 x. x 2 T1’ f(x) 2 T2’
• Therefore, f 2 t1’ T2’
• Thus, T1 T2 µ T1’ T2’
![Page 27: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/27.jpg)
Subtyping for Classes• Class C contains a collection of methods• We view field var f: T as two methods
– getF(this:C): T C T– setF(this:C, x:T): void C x T void
• For val f: T (immutable): we have only getF• Class has all functionality of a pair of method• We must require (at least) that methods
named the same are subtypes• If type T is generic, it must be invariant
– as for mutable arrays
![Page 28: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/28.jpg)
Example
class C { def m(x : T1) : T2 = {...}}class D extends C { override def m(x : T’1) : T’2 = {...}}
D <: C Therefore, we need to have:
T1 <: T’1 (argument behaves opposite)
T’2 <: T2 (result behaves like class)
![Page 29: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/29.jpg)
Today• More Subtyping Rules
– product types (pairs)– function types– classes
• Soundness– motivating example– idea of proving soundness– operational semantics– a soundness proof
• Subtyping and generics
![Page 30: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/30.jpg)
Example: Tootool 0.1 Language
Tootool is a rural community in the central east part of the Riverina [New South Wales, Australia]. It is situated by road, about 4 kilometres east from French Park and 16 kilometres west from The Rock.Tootool Post Office opened on 1 August 1901 and closed in 1966. [Wikipedia]
![Page 31: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/31.jpg)
Type System for Tootool 0.1Pos <: IntNeg <: Int
does it type check?def intSqrt(x:Pos) : Pos = { ...}var p : Posvar q : Negvar r : Posq = -5p = qr = intSqrt(p)
Runtime error: intSqrt invoked with a negative argument!
unsound
¡ = {(p, Pos), (q, Neg), (r, Pos), (intSqrt, Pos Pos)}
assignment
subtyping
![Page 32: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/32.jpg)
What went wrong in Tootool 0.1 ?Pos <: IntNeg <: Int
does it type check? – yes def intSqrt(x:Pos) : Pos = { ...}var p : Posvar q : Negvar r : Posq = -5p = qr = intSqrt(p)
assignment
subtyping
¡ = {(p, Pos), (q, Neg), (r, Pos), (intSqrt, Pos Pos)}
x must be able to store any value from T
e can have any value from T
Cannot use ¡ ` to mean “x promises it can store any e 2 T”
Runtime error: intSqrt invoked with a negative argument!
![Page 33: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/33.jpg)
Recall Our Type DerivationPos <: IntNeg <: Int
does it type check? – yes def intSqrt(x:Pos) : Pos = { ...}var p : Posvar q : Negvar r : Posq = -5p = qr = intSqrt(p)
assignment
subtyping
¡ = {(p, Pos), (q, Neg), (r, Pos), (intSqrt, Pos Pos)}
Values from p are integers. But p did not promise to store all kinds of integers/ Only positive ones!
Runtime error: intSqrt invoked with a negative argument!
![Page 34: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/34.jpg)
Corrected Type Rule for AssignmentPos <: IntNeg <: Int
does it type check? – yes def intSqrt(x:Pos) : Pos = { ...}var p : Posvar q : Negvar r : Posq = -5p = qr = intSqrt(p)
assignment
subtyping
¡ = {(p, Pos), (q, Neg), (r, Pos), (intSqrt, Pos Pos)}
x must be able to store any value from T
e can have any value from T
¡ stores declarations (promises)
does not type check
![Page 35: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/35.jpg)
How could we ensure that some other programs will not break?
Type System Soundness
![Page 36: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/36.jpg)
Today• More Subtyping Rules
– product types (pairs)– function types– classes
• Soundness– motivating example– idea of proving soundness– operational semantics– a soundness proof
• Subtyping and generics
![Page 37: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/37.jpg)
Proving Soundness of Type Systems
• Goal of a sound type system:– if the program type checks, then it never “crashes”– crash = some precisely specified bad behavior e.g. invoking an operation with a wrong type
• dividing one string by another string “cat” / “frog• trying to multiply a Window object by a File object
e.g. not dividing an integer by zero• Never crashes: no matter how long it executes
– proof is done by induction on program execution
![Page 38: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/38.jpg)
Proving Soundness by Induction
• Program moves from state to state• Bad state = state where program is about to exhibit a bad
operation ( “cat” / “frog” )• Good state = state that is not bad• To prove:
program type checks states in all executions are good• Usually need a stronger inductive hypothesis;
some notion of very good (VG) state such that: program type checks program’s initial state is very good state is very good next state is also very good state is very good state is good (not about to crash)
VG VG VG VG VG VGVG VG VG VG VG GoodVG
![Page 39: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/39.jpg)
A Simple Programming Language
![Page 40: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/40.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 1 y = 1 z = 1
position in source
Initially, all variables have value 1
![Page 41: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/41.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 3 y = 1 z = 1
position in source
![Page 42: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/42.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 3 y = -5 z = 1
position in source
![Page 43: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/43.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 3 y = -5 z = 4position in source
![Page 44: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/44.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 7 y = -5 z = 4
position in source
![Page 45: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/45.jpg)
Program State
var x : Posvar y : Intvar z : Posx = 3y = -5z = 4x = x + zy = x / zz = z + x
values of variables: x = 7 y = 1 z = 4
position in source
formal description of such program execution is called operational semantics
![Page 46: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/46.jpg)
Definition of Simple Language
var x1 : Posvar x2 : Int...var xn : Pos
xi = xj
xp = xq + xr
xa = xb / xc
...xp = xq + xr
Programs:Type rules:¡ = { (x1, Pos), (x2, Int), … (xn, Pos)}Pos <: int
variable declarations var x: Posor var x: Int
followed by
statements of one of 3 forms1) xi = xj
2) xi = xj / xk
3) xi = xj + xk
(No complex expressions)
![Page 47: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/47.jpg)
Bad State: About to Divide by Zero (Crash)
var x : Posvar y : Intvar z : Posx = 1y = -1z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -1 z = 0
Definition: state is bad if the next instruction is of the form xi = xj / xk and xk has value 0 in the current state.
position in source
![Page 48: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/48.jpg)
Good State: Not (Yet) About to Divide by Zero
var x : Posvar y : Intvar z : Posx = 1y = -1z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -1 z = 1
Definition: state is good if it is not bad.
Good
Definition: state is bad if the next instruction is of the form xi = xj / xk and xk has value 0 in the current state.
position in source
![Page 49: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/49.jpg)
Good State: Not (Yet) About to Divide by Zero
var x : Posvar y : Intvar z : Posx = 1y = -1z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -1 z = 0
Definition: state is good if it is not bad.
Good
Definition: state is bad if the next instruction is of the form xi = xj / xk and xk has value 0 in the current state.
position in source
![Page 50: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/50.jpg)
Moved from Good to Bad in One Step!
var x : Posvar y : Intvar z : Posx = 1y = -1z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -1 z = 0
Bad
Definition: state is good if it is not bad.
Being good is not preserved by one step, not inductive!It is very local property, does not take future into account.
Definition: state is bad if the next instruction is of the form xi = xj / xk and xk has value 0 in the current state.
position in source
![Page 51: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/51.jpg)
Being Very Good: A Stronger Inductive Property
var x : Posvar y : Intvar z : Posx = 1y = -1z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -1 z = 0
Definition: state is good if it is not about to divide by zero.Definition: state is very good if each variable belongs to the domain determined by its type (if z:Pos, then z is strictly positive).
This state is already not very good.We took future into account.
Pos = { 1, 2, 3, ... }
position in source ∉ Pos
![Page 52: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/52.jpg)
If you are a little typed program,what will your parents teach you?
• If you type check and succeed:– you will be very good from the start.– if you are very good, then you will remain
very good in the next step– If you are very good, you will not crash.
Hence, type check and you will never crash!
Soundnes proof = defining “very good” and checking the properties above.
![Page 53: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/53.jpg)
Definition of Simple Language
var x1 : Posvar x2 : Int...var xn : Pos
xi = xj
xp = xq + xr
xa = xb / xc
...xp = xq + xr
Programs:Type rules:¡ = { (x1, Pos), (x2, Int), … (xn, Pos)}Pos <: int
variable declarations var x: Posor var x: Int
followed by
statements of one of 3 forms1) xi = xj
2) xi = xj / xk
3) xi = xj + xk
(No complex expressions)
![Page 54: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/54.jpg)
Checking Properties in Our Case
Definition: state is very good if each variable belongs to the domain determined by its type (if z:Pos, then z is strictly positive).
Holds: in initial state, variables are =1• If you type check and succeed:
– you will be very good from the start.– if you are very good, then you will remain
very good in the next step– If you are very good, you will not crash.
If next state is x / z, type rule ensures z has type PosBecause state is very good, it means z 2 Posso z is not 0, and there will be no crash.
1 2 Pos1 2 Int
![Page 55: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/55.jpg)
Example Case 1
var x : Posvar y : Posvar z : Posy = 3z = 2z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = 3 z = 2
the next statement is: z=x+ywhere x,y,z are declared Pos.
Goal: prove that again each variable belongs to its type.• variables other than z did not change, so belong to their type• z is sum of two positive values, so it will have positive value
Assume each variable belongs to its type.
position in source
![Page 56: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/56.jpg)
Example Case 2
var x : Posvar y : Intvar z : Posy = -5z = 2z = x + yx = x + zy = x / zz = z + x
values of variables: x = 1 y = -5 z = 2
the next statement is: z=x+ywhere x,z declared Pos, y declared Int
Goal: prove that again each variable belongs to its type.this case is impossible, because z=x+y would not type check
How do we know it could not type check?
Assume each variable belongs to its type.
position in source
![Page 57: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/57.jpg)
Must Carefully Check Our Type Rules
var x : Posvar y : Intvar z : Posy = -5z = 2z = x + yx = x + zy = x / zz = z + x
Conclude that the only types we can derive are: x : Pos, x : Int y : Int x + y : Int
Cannot type check z = x + y in this environment.
Type rules:¡ = { (x1, Pos), (x2, Int), … (xn, Pos)}Pos <: int
![Page 58: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/58.jpg)
We would need to check all cases(there are many, but they are easy)
![Page 59: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/59.jpg)
Remark
• We used in examples Pos <: Int
• Same examples work if we have
class Int { ... }class Pos extends Int { ... }
and is therefore relevant for OO languages
![Page 60: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/60.jpg)
Today• More Subtyping Rules
– product types (pairs)– function types– classes
• Soundness– motivating example– idea of proving soundness– operational semantics– a soundness proof
• Subtyping and generics
![Page 61: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/61.jpg)
Simple Parametric Class
class Ref[T](var content : T)Can we use the subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1y = xy.content = 0z = z / x.content
¡
type checks
![Page 62: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/62.jpg)
Simple Parametric Class
class Ref[T](var content : T)Can we use the subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1y = xy.content = 0z = z / x.content
1
-1
Ref[Pos]
Ref[Int]
x
y
![Page 63: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/63.jpg)
Simple Parametric Class
class Ref[T](var content : T)Can we use the subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1y = xy.content = 0z = z / x.content
1
-1
Ref[Pos]
Ref[Int]
x
y
![Page 64: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/64.jpg)
Simple Parametric Class
class Ref[T](var content : T)Can we use the subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1y = xy.content = 0z = z / x.content
0
-1
Ref[Pos]
Ref[Int]
x
y
CRASHES
![Page 65: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/65.jpg)
Analogously
class Ref[T](var content : T)Can we use the converse subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1x = yy.content = 0z = z / x.content
1
0
Ref[Pos]
Ref[Int]
x
y
CRASHES
![Page 66: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/66.jpg)
Mutable Classes do not Preserve Subtyping
class Ref[T](var content : T)Even if T <: T’,
Ref[T] and Ref[T’] are unrelated types
var x : Ref[T]var y : Ref[T’]...x = y...
type checks only if T=T’
![Page 67: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/67.jpg)
Same Holds for Arrays, Vectors, all mutable containers
var x : Array[Pos](1)var y : Array[Int](1)var z : Intx[0] = 1y[0] = -1y = xy[0] = 0z = z / x[0]
Even if T <: T’, Array[T] and Array[T’] are unrelated types
![Page 68: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/68.jpg)
Case in Soundness Proof Attempt
class Ref[T](var content : T)Can we use the subtyping rule
var x : Ref[Pos]var y : Ref[Int]var z : Intx.content = 1y.content = -1y = xy.content = 0z = z / x.content
prove each variable belongs to its type:variables other than y did not change.. (?!)
1
-1
Ref[Pos]
Ref[Int]
x
y
![Page 69: Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z](https://reader036.vdocument.in/reader036/viewer/2022062517/56649f1c5503460f94c326b3/html5/thumbnails/69.jpg)
Mutable vs Immutable Containers
• Immutable container, Coll[T]– has methods of form e.g. get(x:A) : T– if T <: T’, then Coll[T’] hasget(x:A) : T’– we have (A T) <: (A T’)
covariant rule for functions, so Coll[T] <: Coll[T’]• Write-only data structure have
– setter-like methods, set(v:T) : B– if T <: T’, then Container[T’] has set(v:T) : B– would need (T B) <: (T’ B)
contravariance for arguments, so Coll[T’] <: Coll[T]• Read-Write data structure need both,
so they are invariant, no subtype on Coll if T <: T’