Compilation 2012 Static Type Checking

Size: px
Start display at page:

Download "Compilation 2012 Static Type Checking"

Transcription

1 Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University

2 The Type Checker The type checker has several tasks: determine the types of all expressions check that values and variables are used correctly resolve certain ambiguities by transformations Some languages have no type checker no compile-time guarantees runtime tagging and checking become necessary 2

3 Types Describe Possible Values The Joos types are: void (the empty set of values) byte short int char boolean C (objects of class C or any subclass) null (the polymorphic null constant) σ[] (for any type σ) 3

4 Type Annotations Type annotations: int x; Foo y; specify invariants about the runtime behavior: x will always contain an integer value y will always contain null or an object of class Foo or any subclass thereof Java-like Type annotations are (arguably) not very expressive viewed as invariants Other type systems are more expressive 4

5 Type Correctness A program is type correct if the type annotations are valid invariants Type correctness is trivially undecidable: int x = 0; TM(j); x = false; The program is type correct iff the j'th Turing machine does not halt on empty input 5

6 Static Type Correctness A program is statically type correct if it satisfies some type rules The rules are chosen to be: simple to understand efficient to decide conservative with respect to type correctness Type systems are not canonical They are designed like the rest of the language 6

7 Slack Static type systems are necessarily flawed: type correct statically type correct slack There is always slack: programs that are unfairly rejected by the type checker 7

8 Examples of Slack Trivial example: x = 87; if (false) x = true; Useful example: Map m = new Hashmap(); m.put("bar","foo"); String x = m.get("bar"); Java 1.5 was designed to pick up some slack 8

9 Specifying Type Rules Prose: "The argument of the sqrt function must be of type int, the result is of type double" Logical rules: C - x: int C - sqrt(x): double 9

10 The Shortcomings of Prose The first expression must be of type boolean, or a compile-time error occurs. The conditional operator may be used to choose between second and third operands of numeric type, or second and third operands of type boolean, or second and third operands that are each of either reference type or the null type. All other cases result in a compile-time error. Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear. The type of a conditional expression is determined as follows: If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression. Otherwise, if the second and third operands have numeric type, then there are several cases:if one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short. If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T. Otherwise, binary numeric promotion is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs value set conversion. If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type. If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion; the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type. 10

11 Three Kinds of Type Rules Declarations: this variable is declared to have this type Propagations: if the argument is of this type, then the result is of that type Restrictions: the argument must be of this type or that type The logical notation handles all three uniformly 11

12 Judging Expressions The judgment: C, L, X, σ - E: τ means that the expression E is statically type correct and has type τ in a context where: C is the current class L is the current local environment X is the current set of exceptions σ is the return type of the current method 12

13 Judging Statements The judgment: C, L, X, σ - S means that the statement S is statically type correct in a context where: C is the current class L is the current local environment X is the current set of exceptions σ is the return type of the current method 13

14 Class Environments The class environment is implicitly available The notation D C means that D is a subclass of C We can query a class C: C - extends D (super class) C - σ f (fields) C - static σ f (static fields) C - (σ 1,..., σ k ) X (constructors) C - σ m (σ 1,..., σ k ) X (methods) C - static σ m (σ 1,..., σ k ) X (static methods) return type name argument types thrown exceptions 14

15 Assignment Compatibility The relation σ := τ means that values of type τ may be assigned to variables of type σ: σ := σ short := byte σ :- σ int := byte int := short σ[] :- τ[], if σ :- τ int := char C := null java.lang.cloneable :- σ[] σ[] := null Object := σ[] java.io.serializable :- σ[] java.lang.cloneable := σ[] java.io.serializable := σ[] σ[] := τ[], if σ :- τ C := D, if D C Object :- σ[] C :- D, if D C It is reflexive, transitive, and anti-symmetric 15

16 Statements C,L,X,σ - S 1 C,L,X,σ - S 2 C, L, X, σ - S 1 S 2 C, L[n τ], X, σ - S C, L, X, σ - { τ n; S } C, L, X, σ - E: τ C, L, X, σ - E; 16

17 Control Statements C,L,X,σ - E: boolean C, L, X, σ - if (E) S C,L,X,σ - S C,L,X,σ - E: boolean C,L,X,σ - S i C, L, X, σ - if (E) S 1 else S 2 C,L,X,σ - E: boolean C, L, X, σ - while (E) S C,L,X,σ - S 17

18 Return Statements σ = void C, L, X, σ - return C, L, X, σ - E: τ σ := τ C, L, X, σ - return E 18

19 Throw Statement C,L,X, σ - E: τ throwok({τ},x) C, L, X, σ - throw E throwok(a,b) α A: Error:= α RuntimeException:= α β B: β:=α 19

20 Locals L(n) = τ C, L, X, σ - n: τ 20

21 Fields C,L,X,σ - this: C D - static τ f C,L,X,σ - D.f: τ C,L,X,σ - E: D C, L, X, σ - E.f: τ D - τ f 21

22 Assignments C,L,X,σ - E: τ 2 L(n)=τ 1 τ 1 :=τ 2 C, L, X, σ - n = E: τ 1 C,L,X,σ - E: τ 2 D - static τ 1 f τ 1 :=τ 2 C, L, X, σ - D.f = E: τ 1 C,L,X,σ - E 2 : τ 2 C,L,X,σ - E 1 : D D - τ 1 f τ 1 :=τ 2 C, L, X, σ - E 1.f = E 2 : τ 1 22

23 Arrays C,L,X,σ - E 1 : τ 1 [] C,L,X,σ - E 2 : τ 2 num(τ 2 ) C, L, X, σ - E 1 [E 2 ]: τ 1 C, L, X, σ - E 1 : τ 1 [] C, L, X, σ - E 2 : τ 2 C, L, X, σ - E 3 : τ 3 num(τ 2 ) τ 1 := τ 3 C, L, X, σ - E 1 [E 2 ]= E 3 : τ 1 num(σ) σ { byte, short, int, char } 23

24 Array Operations C,L,X,σ - E: τ[] C,L,X,σ - E.clone(): Object C,L,X,σ - E: τ[] C,L,X,σ - E.length: int C,L,X,σ - E: τ 2 num(τ 2 ) C,L,X,σ - new τ 1 [E]: τ 1 [] 24

25 Operators C,L,X,σ - E: boolean C,L,X,σ -!E: boolean C,L,X,σ - E 1 : τ 1 C,L,X,σ - E 2 : τ 2 num(τ 1 ) num(τ 2 ) C, L, X, σ - E 1 *E 2 : int 25

26 Literals C,L,X,σ - true: boolean C,L,X,σ - 42: int C,L,X,σ - "abc": String C,L,X,σ - null: null C,L,X,σ - '@': char 26

27 Plus C,L,X,σ - E 1 : τ 1 C,L,X,σ - E 2 : τ 2 num(τ 1 ) num(τ 2 ) C, L, X, σ - E 1 +E 2 : int C,L,X,σ - E 1 : String C,L,X,σ - E 2 : τ 2 τ 2 void C, L, X, σ - E 1 +E 2 : String C,L,X,σ - E 1 : τ 1 C,L,X,σ - E 2 : String τ 1 void C, L, X, σ - E 1 +E 2 : String 27

28 Equality C,L,X,σ - E 1 : τ 1 C,L,X,σ - E 2 : τ 2 (num(τ 1 ) num(τ 2 )) τ 1 :=τ 2 τ 2 :=τ 1 C, L, X, σ - E 1 ==E 2 : boolean 28

29 Casts and Instanceof C,L,X,σ - E: τ 1 (num(τ 1 ) num(τ 2 )) τ 1 :=τ 2 τ 2 :=τ 1 C, L, X, σ - (τ 2 )E: τ 2 C,L,X,σ - E: τ τ:=d D:=τ C, L, X, σ - E instanceof D: boolean 29

30 Why Restrict Casts? τ 2 τ 1 Alway succeeds, but upcasting is useful for selecting different methods τ 2 := τ 1 τ 1 τ 2 Really useful, the object may or may not by a subclass of τ 2 τ 1 := τ 2 τ 2 τ 1 Always fails, don't bother to execute τ 1 : τ 2 τ 1 : τ 2 30

31 Method Invocation in Joos1 C,L,X,σ - E: D C,L,X,σ - E i : τ i D - τ m(τ 1,...,τ k ) Y C, L, X, σ - E.m(E 1,...,E k ): τ C,L,X,σ - E i : τ i D - static τ m(τ 1,...,τ k ) Y C, L, X, σ - D.m(E 1,...,E k ): τ (we add exception handling machinery later) 31

32 Constructor Invocation C,L,X,σ - E i : τ i D - (τ 1,...,τ k ) Y throwok(y,x) C, L, X, σ - new D(E 1,...,E k ): D C,L,X,σ - E i : τ i C - extends D D - (τ 1,...,τ k ) Y throwok(y,x) C, L, X, σ - super(e 1,...,E k ) 32

33 Method Invocation in Joos2 C,L,X,σ - E: D C,L,X,σ - E i : σ i D - τ m(τ 1,...,τ k ) Y τ i := σ i D - γ m(γ 1,..., γ k ) Z : ( i : γ i := σ i ) ( i : γ i := τ i ) C, L, X, σ - E.m(E 1,...,E k ): τ This rule does not describe access modifiers It supports overloading (closest match) and subtyping in the arguments 33

34 Checking Exceptions in Joos1 C,L,X,σ - E: D C,L,X,σ - E i : τ i D - τ m(τ 1,...,τ k ) Y throwok( {Y i D - τ m(τ 1,...,τ k ) Y i }, X) C, L, X, σ - E.m(E 1,...,E k ): τ where Y Z = Y Z Z Y Y Z = { y Y throwok({y},z) } Intuitively: check the intersection of all possible exceptions 34

35 Exceptions - why so complicated? class X extends Throwable {} class Y extends Throwable {} abstract class A { public abstract void m() throws X; } interface B { void m() throws Y; } class Test { void foo(c c) { // no throws clause required! c.m(); } } 35

36 Exceptions - why so complicated? class X extends Throwable {} class Y extends X {} abstract class A { public abstract void m() throws X; } interface B { void m() throws Y; } class Test { void foo(c c) throws Y { // no throws X clause required! c.m(); } } 36

37 Judging Methods and Constructors C, [a i σ i ], { X i }, σ - S Throwable := X i C - σ m(σ 1 a 1,...,σ k a k )throws X 1,...,X n { S } C, [a i σ i ], { X i }, void - S Throwable := X i C - C(σ 1 a 1,...,σ k a k )throws X 1,...,X n { S } 37

38 Different Kinds of Type Rules Axioms: C,L,X,σ - this: C Predicates: τ 1 :=τ 2 τ 2 :=τ 1 Inferences: C,L,X,σ - E: D C, L, X, σ - E.f: τ D - τ f 38

39 Type Proofs A type proof is a tree in which: nodes are inferences leaves are axioms or true predicates A judgment is valid it is the root of some type proof 39

40 A Type Proof [x A,y B](x)=A C,[x A,y B],X,σ - x:a A:=B B:=A [x A,y B](y)=B C,[x A,y B],X,σ - (B)x: B B:=B C,[x A,y B],X, σ - y=(b)x: B C,[x A,y B],X, σ - y=(b)x; C,[x A],X, σ - {B y; y=(b)x;} C,[],X,σ - {A x; {B y; y=(b)x;}} (Assuming that B is a subclass of A) Equivalent to a trace of a successful type checker run 40

41 Static Type Correctness A program is statically type correct all method and constructor judgments are valid A type system is sound static type correctness implies type correctness 41

42 Java's Type System is Unsound The following is a valid judgement: C, [b B,x A[],y B[],c C], X, σ - x=y; x[0]=c; b=y[0]; where B A, C A, and B and C are incomparable Afterward, b contains an object of class C But its declared type only allows B objects 42

43 Greed Java's type system is too greedy: type correct statically type correct unsound Runtime type checks catch the unsound cases 43

44 Fixed Assignment Compatibility By design, the rule for assignment of arrays has been chosen to be convenient but unsound It should have looked like: σ := σ short := byte σ :- σ int := byte int := short σ[] :- τ[], if σ :- τ int := char C := null java.lang.cloneable :- σ[] σ[] := null Object := σ[] java.io.serializable :- σ[] java.lang.cloneable := σ[] java.io.serializable := σ[] σ[] := τ[], if σ :- τ C := D, if D C C :- D, if D C But that would then introduce annoying slack... 44

45 Complexity of Inference Systems Deciding valid judgments of an inference system depends on its rules and axioms and may be: undecidable (hyper)exponential time (backtracking) linear time The Java type system is designed to be simple A type checker performs a single traversal of the parse tree in linear time 45

46 Type Checking in Java Type checking is performed bottom-up The rule to apply is uniquely determined by the types of subexpressions and the syntax The type proof also specifies how to transform the program with respect to: Autoboxing/unboxing implicit string conversions concat(...) code 46

47 Implicit Coercions Suppose we add a coercion type rule: C,L,X,σ - E: int C,L,X,σ - E: float corresponding to the JVM bytecode i2f Type proofs are now no longer unique! 47

48 Ambiguous Type Proofs L(i)=int L(j)=int C,L,X,σ -i: int C,L,X,σ - j: int C,L,X,σ -i: float C,L,X,σ - j: float L(f)=float C,L,X,σ - i+j: float C,L,X,σ - f: float C,L,X,σ - i+j==f: boolean L(i)=int L(j)=int C,L,X,σ -i: int C,L,X,σ - j: int C,L,X,σ -i+j: int L(f)=float C,L,X,σ - i+j: float C,L,X,σ - f: float C,L,X,σ - i+j==f: boolean 48

49 Semantic Coherence Different type proofs generate different transformed programs The type system is semantically coherent if they always have the same semantics The previous hypothetical type system is not: i = j = f = E9 Java chooses the proof with coercions as near the root as possible 49

Static Type Checking. Static Type Checking. The Type Checker. Type Annotations. Types Describe Possible Values

Static Type Checking. Static Type Checking. The Type Checker. Type Annotations. Types Describe Possible Values The Type Checker Compilation 2007 The type checker has several tasks: determine the types of all expressions check that values and variables are used correctly resolve certain ambiguities by transformations

More information

Type Checking. COMP 520: Compiler Design (4 credits) Alexander Krolik MWF 9:30-10:30, TR Bob, from Accounting.

Type Checking. COMP 520: Compiler Design (4 credits) Alexander Krolik MWF 9:30-10:30, TR Bob, from Accounting. COMP 520 Winter 2018 Type Checking (1) Type Checking COMP 520: Compiler Design (4 credits) Alexander Krolik alexander.krolik@mail.mcgill.ca MWF 9:30-10:30, TR 1080 http://www.cs.mcgill.ca/~cs520/2018/

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Subtyping. Lecture 13 CS 565 3/27/06

Subtyping. Lecture 13 CS 565 3/27/06 Subtyping Lecture 13 CS 565 3/27/06 Polymorphism Different varieties of polymorphism: Parametric (ML) type variables are abstract, and used to encode the fact that the same term can be used in many different

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler

More information

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

Introduction A Tiny Example Language Type Analysis Static Analysis 2009

Introduction A Tiny Example Language Type Analysis Static Analysis 2009 Introduction A Tiny Example Language Type Analysis 2009 Michael I. Schwartzbach Computer Science, University of Aarhus 1 Questions About Programs Does the program terminate? How large can the heap become

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

Static Program Analysis Part 1 the TIP language

Static Program Analysis Part 1 the TIP language Static Program Analysis Part 1 the TIP language http://cs.au.dk/~amoeller/spa/ Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University Questions about programs Does the program terminate

More information

Compilation 2012 Code Generation

Compilation 2012 Code Generation Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University Phases Computing resources, such as: layout of data structures offsets register allocation Generating an internal representation

More information

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness? Tradeoffs CSE 505: Programming Languages Lecture 15 Subtyping Zach Tatlock Autumn 2017 Desirable type system properties (desiderata): soundness - exclude all programs that get stuck completeness - include

More information

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich From IMP to Java Andreas Lochbihler ETH Zurich parts based on work by Gerwin Klein and Tobias Nipkow 2015-07-14 1 Subtyping 2 Objects and Inheritance 3 Multithreading 1 Subtyping 2 Objects and Inheritance

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

Static Checking and Type Systems

Static Checking and Type Systems 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

More information

Compilation 2012 Static Analysis

Compilation 2012 Static Analysis Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University Interesting Questions Is every statement reachable? Does every non-void method return a value? Will local variables definitely be

More information

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance (extends) Use relationship Inheritance 1..n Unidirectional association Inheritance ("extends") Use relationship Implementation ("implements") What is inherited public, protected methods public, proteced attributes What is not inherited

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Type Checking. Error Checking

Type Checking. Error Checking Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks

More information

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept F1 A Java program Ch 1 in PPIJ Introduction to the course The computer and its workings The algorithm concept The structure of a Java program Classes and methods Variables Program statements Comments Naming

More information

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result. Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

More information

Assignment 4: Semantics

Assignment 4: Semantics Assignment 4: Semantics 15-411: Compiler Design Jan Hoffmann Jonathan Burns, DeeDee Han, Anatol Liu, Alice Rao Due Thursday, November 3, 2016 (9:00am) Reminder: Assignments are individual assignments,

More information

Java Overview An introduction to the Java Programming Language

Java Overview An introduction to the Java Programming Language Java Overview An introduction to the Java Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhan Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/

More information

Exercise 12 Initialization December 15, 2017

Exercise 12 Initialization December 15, 2017 Concepts of Object-Oriented Programming AS 2017 Exercise 12 Initialization December 15, 2017 Task 1 Consider a Java class Vector, representing a 2 dimensional vector: public class Vector { public Number!

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Points To Remember for SCJP

Points To Remember for SCJP Points To Remember for SCJP www.techfaq360.com The datatype in a switch statement must be convertible to int, i.e., only byte, short, char and int can be used in a switch statement, and the range of the

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

4 CoffeeStrainer Virtues and Limitations

4 CoffeeStrainer Virtues and Limitations In this chapter, we explain CoffeeStrainer s virtues and limitations and the design decisions that led to them. To illustrate the points we want to make, we contrast CoffeeStrainer with a hypothetical,

More information

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Modern Programming Languages. Lecture Java Programming Language. An Introduction Modern Programming Languages Lecture 27-30 Java Programming Language An Introduction 107 Java was developed at Sun in the early 1990s and is based on C++. It looks very similar to C++ but it is significantly

More information

CLASS DESIGN. Objectives MODULE 4

CLASS DESIGN. Objectives MODULE 4 MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors

More information

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1 CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project

More information

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 1 Not all languages are regular So what happens to the languages which are not regular? Can we still come up with a language recognizer?

More information

Lecture 13: Subtyping

Lecture 13: Subtyping Lecture 13: Subtyping Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Subtyping CS546, 2018-2019 1 / 15 Subtyping Usually found

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Featherweight Java (FJ)

Featherweight Java (FJ) x = 1 let x = 1 in... x(1).!x(1) x.set(1) Programming Language Theory Featherweight Java (FJ) Ralf Lämmel This lecture is based on David Walker s lecture: Computer Science 441, Programming Languages, Princeton

More information

Subtyping (cont) Lecture 15 CS 565 4/3/08

Subtyping (cont) Lecture 15 CS 565 4/3/08 Subtyping (cont) Lecture 15 CS 565 4/3/08 Formalization of Subtyping Inversion of the subtype relation: If σ

More information

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

The Sun s Java Certification and its Possible Role in the Joint Teaching Material The Sun s Java Certification and its Possible Role in the Joint Teaching Material Nataša Ibrajter Faculty of Science Department of Mathematics and Informatics Novi Sad 1 Contents Kinds of Sun Certified

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake Assigning Values // Example 2.3(Mathematical operations in C++) float a; cout > a; cout

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Last name:... First name:... Department (if not D-INFK):...

Last name:... First name:... Department (if not D-INFK):... Concepts of Object-Oriented Programming AS 2017 Concepts of Object-Oriented Programming Midterm Examination 10.11.2017 Prof. Dr. Peter Müller Last name:................................. First name:.................................

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

1 Introduction. 3 Syntax

1 Introduction. 3 Syntax CS 6110 S18 Lecture 19 Typed λ-calculus 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic semantics,

More information

Compiler Construction I

Compiler Construction I TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter, Dr. Axel Simon SoSe 2014 1 / 30 Topic: Semantic Analysis 2 / 30 Semantic Analysis Chapter 1: Type Checking

More information

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction He am a driver might be syntactically correct but semantically wrong. Semantic

More information

First IS-A Relationship: Inheritance

First IS-A Relationship: Inheritance First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class

More information

User-Defined Algebraic Data Types

User-Defined Algebraic Data Types 72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper) Gradual Typing for Functional Languages Jeremy Siek and Walid Taha (presented by Lindsey Kuper) 1 Introduction 2 What we want Static and dynamic typing: both are useful! (If you re here, I assume you agree.)

More information

Java Programming. Atul Prakash

Java Programming. Atul Prakash Java Programming Atul Prakash Java Language Fundamentals The language syntax is similar to C/ C++ If you know C/C++, you will have no trouble understanding Java s syntax If you don't, it will be easier

More information

Mosig M1 - PLSCD Written exam

Mosig M1 - PLSCD Written exam Mosig M1 - PLSCD 0809 - Written exam 1 Exercise I : Operational semantics - a debugger In this exercise we consider program execution with the help of a debugger. This execution will be defined on an intermediate

More information

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control. Feb 23, 2009 CSE, 409/509 Mitigation of Bugs, Life of an exploit 1) Bug inserted into code 2) Bug passes testing 3) Attacker triggers bug 4) The Attacker gains control of the program 5) Attacker causes

More information

Motivation for typed languages

Motivation for typed languages Motivation for typed languages Untyped Languages: perform any operation on any data. Example: Assembly movi 5 r0 // Move integer 5 (some representation) to r0 addf 3.6 r0 // Treat bit representation in

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time CS2110 Fall 2011 Lecture 25 Java program last time Java compiler Java bytecode (.class files) Compile for platform with JIT Interpret with JVM Under the Hood: The Java Virtual Machine, Part II 1 run native

More information

Polymorphism. return a.doublevalue() + b.doublevalue();

Polymorphism. return a.doublevalue() + b.doublevalue(); Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

DD2460 Software Safety and Security: Part III Exercises session 2: Type + Jif

DD2460 Software Safety and Security: Part III Exercises session 2: Type + Jif DD2460 Software Safety and Security: Part III Exercises session 2: Type + Jif Gurvan Le Guernic adapted from Aslan Askarov DD2460 (III, E2) February 22 st, 2012 1 Noninterference type systems challenge

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A. HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,

More information

Topic 9: Type Checking

Topic 9: Type Checking Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

More information

Topic 9: Type Checking

Topic 9: Type Checking Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

More information

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

Types, Expressions, and States

Types, Expressions, and States 8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties

More information

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println(The age is  + age); } Question No :1 What is the correct ordering for the import, class and package declarations when found in a Java class? 1. package, import, class 2. class, import, package 3. import, package, class 4. package,

More information

Assertions and Exceptions Lecture 11 Fall 2005

Assertions and Exceptions Lecture 11 Fall 2005 Assertions and Exceptions 6.170 Lecture 11 Fall 2005 10.1. Introduction In this lecture, we ll look at Java s exception mechanism. As always, we ll focus more on design issues than the details of the language,

More information

Subtyping and Objects

Subtyping and Objects Subtyping and Objects Massimo Merro 20 November 2017 Massimo Merro Data and Mutable Store 1 / 22 Polymorphism So far, our type systems are very rigid: there is little support to code reuse. Polymorphism

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages. Type System CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

More information

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17 List of Programs xxv List of Figures xxix List of Tables xxxiii Preface to second version xxxv PART 1 Structured Programming 1 1 Getting started 3 1.1 Programming 3 1.2 Editing source code 5 Source code

More information

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics SE3E03, 2006 1.59 61 Syntax and Semantics Syntax Shape of PL constructs What are the tokens of the language? Lexical syntax, word level How are programs built from tokens? Mostly use Context-Free Grammars

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Programming Lecture 3

Programming Lecture 3 Five-Minute Review 1. What is a variable? 2. What is a class? An object? 3. What is a package? 4. What is a method? A constructor? 5. What is an object variable? 1 Programming Lecture 3 Expressions etc.

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 14 Tuesday, March 24, 2015 1 Parametric polymorphism Polymorph means many forms. Polymorphism is the ability of

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Lecture 4. Types, Memory, Exceptions

Lecture 4. Types, Memory, Exceptions AY17/18 Sem 2 Lecture 4 Types, Memory, Exceptions input.java source files javac.class bytecode java output compile execute with Java Virtual Machine AY17/18 Sem 2 for (Printable o: objs) { o.print(); input.java

More information

Runtime Behavior of Conversion Interpretation of Subtyping

Runtime Behavior of Conversion Interpretation of Subtyping Runtime Behavior of Conversion Interpretation of Subtyping Yasuhiko Minamide Institute of Information Sciences and Electronics University of Tsukuba and PRESTO, JST minamide@is.tsukuba.ac.jp Abstract.

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information