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.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Chapter Three: Closure Properties for Regular Languages
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Discrete Mathematics Math 6A Homework 9 Solution.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
This Week Finish relational semantics Hoare logic Interlude on one-point rule Building formulas from programs.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Type Checking.
Compiler Construction
Introduction to Computability Theory
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Semantics Q S EMANTICS (Q1,’07) Week 4 Jacob Andersen PhD student
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
C++ fundamentals.
Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero)
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
MATH 224 – Discrete Mathematics
Reading and Writing Mathematical Proofs
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Exercise Determine the output of the following program assuming static and dynamic scoping. Explain the difference, if there is any. object MyClass { val.
Types in programming languages1 What are types, and why do we need them?
Hazırlayan DISCRETE COMPUTATIONAL STRUCTURES Propositional Logic PROF. DR. YUSUF OYSAL.
Chapter 3 Part II Describing Syntax and Semantics.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
ECSE Software Engineering 1I HO 4 © HY 2012 Lecture 4 Formal Methods A Library System Specification (Continued) From Specification to Design.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
1 Melikyan/DM/Fall09 Discrete Mathematics Ch. 7 Functions Instructor: Hayk Melikyan Today we will review sections 7.3, 7.4 and 7.5.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CS104:Discrete Structures Chapter 2: Proof Techniques.
CSE 311: Foundations of Computing Fall 2013 Lecture 8: Proofs and Set theory.
Soundness of Types Ensuring that a type system is not broken.
CMSC 330: Organization of Programming Languages Operational Semantics.
Type soundness In a more formal way. Proving Soundness of Type Systems Goal of a sound type system: –if the program type checks, then it never “crashes”
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Process Algebra (2IF45) Basic Process Algebra Dr. Suzana Andova.
Arrays Using array as an expression, on the right-hand side Assigning to an array.
Reasoning About Code.
Revisiting Predicate Logic LN chapters 3,4
User-Defined Functions
Axiomatic semantics Points to discuss: The assignment statement
Presentation transcript:

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:

Overloading of Operators Not a problem for type checking from leaves to root Int £ Int  Int String £ String  String

Arrays Using array as an expression, on the right-hand side Assigning to an array

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

Type Rules (1) variable constant function application plus if assignment while

Type Rules (2) array use array assignment block

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

DIVING DEEPER INTO TYPES

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 }

Types as Sets Sets so far were disjoint Sets can overlap Boolean true, false String “Richard” “cat” Int  Int Int  Pos Int Pos (1 2) Neg (-1) 16 bit class C class F class D class E F extends D, D extends C C ED F

SUBTYPING

Subtyping Subtyping corresponds to subset Systems with subtyping have non-disjoint sets T 1 <: T 2 means T 1 is a subtype of T 2 –corresponds to T 1 µ T 2 in sets of values Main rule for subtyping ¼ corresponds to

Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero) Pos <: Int Neg <: Int Pos µ Int Neg µ Int (y not zero) (x/y well defined)

More Rules More rules for division?

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

Subtyping Example def f(x:Int) : Pos = { if (x < 0) –x else x+1 } var p : Pos var q : Int q = f(p) Pos <: Int ¡ : f: Int  Pos Does this statement type check?

Using Subtyping def f(x:Pos) : Pos = { if (x < 0) –x else x+1 } var p : Int var q : Int q = f(p) - does not type check Pos <: Int ¡ : f: Pos  Pos

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.

Subtyping and Product Types

Using Subtyping def f(x:Pos) : Pos = { if (x < 0) –x else x+1 } var p : Int var q : Int q = f(p) - does not type check Pos <: Int ¡ : f: Pos  Pos

Subtyping for Products T 1 <: T 2 implies for all e: So, we might as well add: covariant subtyping for pairs Pair [ T 1, T 2 ] Type for a tuple:

Analogy with Cartesian Product A £ B = { (a, b) | a 2 A, b 2 B}

Subtyping and Function Types

Subtyping for Function Types Consequence: contravariance covariance T 1 <: T 2 implies for all e: Function [ T 1, T] contra- co- as if ¡ ` m: T 1 ’ £ … £ T n ’ \to T’

To get the appropriate behavior we need to assign sets to function types like this: T 1  T 2 = { f| 8 x. (x 2 T 1  f(x) 2 T 2 )} We can prove Function Space as Set contravariance because x 2 T 1 is left of implication  T1 £ T2 T1 £ T2 (: x 2 T 1 ) Ç f(x) 2 T 2

Proof T 1  T 2 = { f | 8 x 2 T 1  f(x) 2 T 2 } Let T 1 ’ µ T 1 and T 2 µ T 2 ’ and f 2 T 1  T 2 8 x. x 2 T 1  f(x) 2 T 2 Let x 2 T 1 ’. From T 1 ’ µ T 2, also x 2 T 1 f(x) 2 T 2. By T 2 µ T 2 ’, also f(x) 2 T s ’ 8 x. x 2 T 1 ’  f(x) 2 T 2 ’ Therefore, f 2 t 1 ’  T 2 ’ Thus, T 1  T 2 µ T 1 ’  T 2 ’

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

Example class C { def m(x : T 1 ) : T 2 = {...} } class D extends C { override def m(x : T’ 1 ) : T’ 2 = {...} } D <: C Therefore, we need to have: T 1 <: T’ 1 (argument behaves opposite) T’ 2 <: T 2 (result behaves like class)

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

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 [Wikipedia]

Type System for Tootool 0.1 Pos <: Int Neg <: Int does it type check? def intSqrt(x:Pos) : Pos = {...} var p : Pos var q : Neg var r : Pos q = -5 p = q r = intSqrt(p) Runtime error: intSqrt invoked with a negative argument! unsound ¡ = {(p, Pos), (q, Neg), (r, Pos), (intSqrt, Pos  Pos)} assignment subtyping

What went wrong in Tootool 0.1 ? Pos <: Int Neg <: Int does it type check? – yes def intSqrt(x:Pos) : Pos = {...} var p : Pos var q : Neg var r : Pos q = -5 p = q r = 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!

Recall Our Type Derivation Pos <: Int Neg <: Int does it type check? – yes def intSqrt(x:Pos) : Pos = {...} var p : Pos var q : Neg var r : Pos q = -5 p = q r = 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!

Corrected Type Rule for Assignment Pos <: Int Neg <: Int does it type check? – yes def intSqrt(x:Pos) : Pos = {...} var p : Pos var q : Neg var r : Pos q = -5 p = q r = 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

How could we ensure that some other programs will not break? Type System Soundness

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

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

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 Good VG

A Simple Programming Language

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 1 y = 1 z = 1 position in source Initially, all variables have value 1

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 3 y = 1 z = 1 position in source

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 3 y = -5 z = 1 position in source

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 3 y = -5 z = 4 position in source

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 7 y = -5 z = 4 position in source

Program State var x : Pos var y : Int var z : Pos x = 3 y = -5 z = 4 x = x + z y = x / z z = z + x values of variables: x = 7 y = 1 z = 4 position in source formal description of such program execution is called operational semantics

Definition of Simple Language var x 1 : Pos var x 2 : Int... var x n : Pos x i = x j x p = x q + x r x a = x b / x c... x p = x q + x r Programs: Type rules: ¡ = { (x 1, Pos), (x 2, Int), … (x n, Pos)} Pos <: int variable declarations var x: Pos or var x: Int followed by statements of one of 3 forms 1)x i = x j 2)x i = x j / x k 3)x i = x j + x k (No complex expressions)

Bad State: About to Divide by Zero (Crash) var x : Pos var y : Int var z : Pos x = 1 y = -1 z = x + y x = x + z y = x / z z = z + x values of variables: x = 1 y = -1 z = 0 Definition: state is bad if the next instruction is of the form x i = x j / x k and x k has value 0 in the current state. position in source

Good State: Not (Yet) About to Divide by Zero var x : Pos var y : Int var z : Pos x = 1 y = -1 z = x + y x = x + z y = x / z z = 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 x i = x j / x k and x k has value 0 in the current state. position in source

Good State: Not (Yet) About to Divide by Zero var x : Pos var y : Int var z : Pos x = 1 y = -1 z = x + y x = x + z y = x / z z = 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 x i = x j / x k and x k has value 0 in the current state. position in source

Moved from Good to Bad in One Step! var x : Pos var y : Int var z : Pos x = 1 y = -1 z = x + y x = x + z y = x / z z = 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 x i = x j / x k and x k has value 0 in the current state. position in source

Being Very Good: A Stronger Inductive Property var x : Pos var y : Int var z : Pos x = 1 y = -1 z = x + y x = x + z y = x / z z = 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

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.

Definition of Simple Language var x 1 : Pos var x 2 : Int... var x n : Pos x i = x j x p = x q + x r x a = x b / x c... x p = x q + x r Programs: Type rules: ¡ = { (x 1, Pos), (x 2, Int), … (x n, Pos)} Pos <: int variable declarations var x: Pos or var x: Int followed by statements of one of 3 forms 1)x i = x j 2)x i = x j / x k 3)x i = x j + x k (No complex expressions)

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 Pos Because state is very good, it means z 2 Pos so z is not 0, and there will be no crash. 1 2 Pos 1 2 Int

Example Case 1 var x : Pos var y : Pos var z : Pos y = 3 z = 2 z = x + y x = x + z y = x / z z = z + x values of variables: x = 1 y = 3 z = 2 the next statement is: z=x+y where 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

Example Case 2 var x : Pos var y : Int var z : Pos y = -5 z = 2 z = x + y x = x + z y = x / z z = z + x values of variables: x = 1 y = -5 z = 2 the next statement is: z=x+y where 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

Must Carefully Check Our Type Rules var x : Pos var y : Int var z : Pos y = -5 z = 2 z = x + y x = x + z y = x / z z = 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: ¡ = { (x 1, Pos), (x 2, Int), … (x n, Pos)} Pos <: int

We would need to check all cases (there are many, but they are easy)

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

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

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 : Int x.content = 1 y.content = -1 y = x y.content = 0 z = z / x.content ¡ type checks

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 : Int x.content = 1 y.content = -1 y = x y.content = 0 z = z / x.content 1 Ref[Pos] Ref[Int] x y

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 : Int x.content = 1 y.content = -1 y = x y.content = 0 z = z / x.content 1 Ref[Pos] Ref[Int] x y

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 : Int x.content = 1 y.content = -1 y = x y.content = 0 z = z / x.content 0 Ref[Pos] Ref[Int] x y CRASHES

Analogously class Ref[T](var content : T) Can we use the converse subtyping rule var x : Ref[Pos] var y : Ref[Int] var z : Int x.content = 1 y.content = -1 x = y y.content = 0 z = z / x.content 1 0 Ref[Pos] Ref[Int] x y CRASHES

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’

Same Holds for Arrays, Vectors, all mutable containers var x : Array[Pos](1) var y : Array[Int](1) var z : Int x[0] = 1 y[0] = -1 y = x y[0] = 0 z = z / x[0] Even if T <: T’, Array[T] and Array[T’] are unrelated types

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 : Int x.content = 1 y.content = -1 y = x y.content = 0 z = z / x.content prove each variable belongs to its type: variables other than y did not change.. (?!) 1 Ref[Pos] Ref[Int] x y

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’] hasset(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’