Programming Languages Featherweight Java David Walker



Similar documents
CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Type-Safe Feature-Oriented Product Lines

Integrating Typed and Untyped Code in a Scripting Language

Inheritance, overloading and overriding

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Java Programming Language

Pluggable Type Systems. Gilad Bracha

Taming Wildcards in Java s Type System

The Interface Concept

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Description of Class Mutation Mutation Operators for Java

Language with a Pluggable Type System and Optional Runtime Monitoring of Type Errors

Introduction to type systems

Integrating Formal Models into the Programming Languages Course

Computing Concepts with Java Essentials

AP Computer Science Java Subset

Dart a modern web language

Stack Allocation. Run-Time Data Structures. Static Structures

Type Classes with Functional Dependencies

Java Interview Questions and Answers

Implementation Aspects of OO-Languages

Checking Access to Protected Members in the Java Virtual Machine

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Object-Oriented Programming Lecture 2: Classes and Objects

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Fundamentals of Java Programming

Part VI. Object-relational Data Models

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Pizza into Java: Translating theory into practice

OPERATIONAL TYPE THEORY by Adam Petcher Prepared under the direction of Professor Aaron Stump A thesis presented to the School of Engineering of

Types and Programming Languages

Semantic Analysis: Types and Type Checking

D06 PROGRAMMING with JAVA

CS 111 Classes I 1. Software Organization View to this point:

Page 331, 38.4 Suppose a is a positive integer and p is a prime. Prove that p a if and only if the prime factorization of a contains p.

Habanero Extreme Scale Software Research Project

Formal Engineering for Industrial Software Development

Chapter 1 Fundamentals of Java Programming

Lemma 5.2. Let S be a set. (1) Let f and g be two permutations of S. Then the composition of f and g is a permutation of S.

Java Application Developer Certificate Program Competencies

Deterministic Discrete Modeling

Data Abstraction and Hierarchy

Lab Manual: Using Rational Rose

Blame for All. Jeremy G. Siek. Amal Ahmed. Robert Bruce Findler. Philip Wadler. Abstract. 1. Introduction

Chapter 13 - Inheritance

Java (12 Weeks) Introduction to Java Programming Language

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Using Inheritance and Polymorphism

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

History OOP languages Year Language 1967 Simula Smalltalk

Specialized Programme on Web Application Development using Open Source Tools

Abstract. a

Regular Languages and Finite Automata

A Propositional Dynamic Logic for CCS Programs

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

Extending Classes with Services

Object Invariants in Dynamic Contexts

Chapter 8 The Enhanced Entity- Relationship (EER) Model

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Application-only Call Graph Construction

Lecture 9. Semantic Analysis Scoping and Symbol Table

An Introduction To UML Class Diagrams Classes

Semantics of UML class diagrams

Enforcing Secure Object Initialization in Java

Appendix B: Alloy Language Reference B.1 Lexical Issues

University of Ostrava. Reasoning in Description Logic with Semantic Tableau Binary Trees

Advanced OOP Concepts in Java

Regression Verification: Status Report

Systematically Refactoring Inheritance to Delegation in JAVA

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Java EE Web Development Course Program

Math 319 Problem Set #3 Solution 21 February 2002

Types, Polymorphism, and Type Reconstruction

Rigorous Software Development CSCI-GA

TypeScript. Language Specification. Version 1.4

Transcription:

Programming Languages Featherweight Java David Walker

Overview Featherweight Java (FJ), a minimal Javalike language. Models inheritance and subtyping. Immutable objects: no mutation of fields. Trivialized core language. 1

Abstract Syntax The abstract syntax of FJ is given by the following grammar: Classes C : : = class c extends c {c f; k d} Constructors k : : = c(c x) {super(x); this.f =x;} Methods d : : = c m(c x) {return e;} Types τ : : = c Expressions e : : = x e.f e.m(e) new c(e) (c) e Underlining indicates one or more. If e appears in an inference rule and e i does too, there is an implicit understandng that e i is one of the e s in e. And similarly with other underlined constructs. 2

Abstract Syntax Classes in FJ have the form: class c extends c {c f; k d} Class c is a sub-class of class c. Constructor k for instances of c. Fields c f. Methods d. 3

Abstract Syntax Constructor expressions have the form c(c x, c x) {super(x ); this.f=x;} Arguments correspond to super-class fields and sub-class fields. Initializes super-class. Initializes sub-class. 4

Abstract Syntax Methods have the form c m(c x) {return e;} Result class c. Argument class(es) c. Binds x and this in e. 5

Abstract Syntax Minimal set of expressions: Field selection: e.f. Message send: e.m(e). Instantiation: new c(e). Cast: (c) e. 6

FJ Example class Pt extends Object { int x; int y; } Pt (int x, int y) { super(); this.x = x; this.y = y; } int getx () { return this.x; } int gety () { return this.y; } 7

FJ Example class CPt extends Pt { color c; } CPt (int x, int y, color c) { super(x,y); this.c = c; } color getc () { return this.c; } 8

Class Tables and Programs A class table T is a finite function assigning classes to class names. A program is a pair (T, e) consisting of A class table T. An expression e. 9

Static Semantics Judgement forms: τ <: τ c c Γ e : τ d ok in c c ok T ok fields(c) = c f type(m, c) = c c subtyping subclassing expression typing well-formed method well-formed class well-formed class table field lookup method type 10

Static Semantics Variables: Γ(x) = τ Γ x : τ Must be declared, as usual. Introduced within method bodies. 11

Static Semantics Field selection: Γ e 0 : c 0 fields(c 0 ) = c f Γ e 0.f i : c i Field must be present. Type is specified in the class. 12

Static Semantics Message send: Γ e 0 : c 0 Γ e : c type(m, c 0 ) = c c c <: c Γ e 0.m(e) : c Method must be present. Argument types must be subtypes of parameters. 13

Static Semantics Instantiation: Γ e : c c <: c fields(c) = c f Γ new c(e) : c Initializers must have subtypes of fields. 14

Static Semantics Casting: Γ e 0 : d Γ (c) e 0 : c All casts are statically acceptable! Could try to detect casts that are guaranteed to fail at run-time. 15

Subclassing Sub-class relation is implicitly relative to a class table. T (c) = class c extends c {...;......} c c Reflexivity, transitivity of sub-classing: (T (c) defined) c c c c c c c c Sub-classing only by explicit declaration! 16

Subtyping Subtyping relation: τ <: τ. τ <: τ τ <: τ τ <: τ τ <: τ c c c <: c Subtyping is determined solely by subclassing. 17

Class Formation Well-formed classes: k = c(c x, c x) {super(x ); this.f=x;} fields(c ) = c f d i ok in c class c extends c {c f; k d} ok Constructor has arguments for each superand sub-class field. Constructor initializes super-class before subclass. Sub-class methods must be well-formed relative to the super-class. 18

Class Formation Method overriding, relative to a class: T (c) = class c extends c {...;......} type(m, c ) = c c 0 x : c, this:c e 0 : c 0 c 0 <: c 0 c 0 m(c x) {return e 0 ;} ok in c Sub-class method must return a subtype of the super-class method s result type. Argument types of the sub-class method must be exactly the same as those for the super-class. Need another case to cover method extension. 19

Program Formation A class table is well-formed iff all of its classes are well-formed: c dom(t ) T (c) ok T ok A program is well-formed iff its class table is well-formed and the expression is well-formed: T ok e : τ (T, e) ok 20

Method Typing The type of a method is defined as follows: T (c) = class c extends c {...;... d} d i = c i m(c i x) {return e;} type(m i, c) = c i c i T (c) = class c extends c {...;... d} m / d type(m i, c ) = c i c i type(m, c) = c i c i 21

Dynamic Semantics Transitions: e T e. Transitions are indexed by a (well-formed) class table! Dynamic dispatch. Downcasting. We omit explicit mention of T in what follows. 22

Dynamic Semantics Object values have the form where new c(e, e) e are the values of the super-class fields. and e are the values of the sub-class fields. c indicates the true (dynamic) class of the instance. Use this judgement to affirm an expression is a value: new c(e, e) value Rules new Object value e i value e i value new c(e, e) value 23

Dynamic Semantics Field selection: fields(c) = c f, c f e value e value new c(e, e).f i e i fields(c) = c f, c f e value e value new c(e, e).f i e i Fields in sub-class must be disjoint from those in super-class. Selects appropriate field based on name. 24

Dynamic Semantics Message send: body(m, c) = x e 0 e value e value new c(e).m(e ) {e /x}{new c(e)/this}e 0 The identifier this stands for the object itself. Compare with recursive functions in MinML. 25

Dynamic Semantics Cast: c c e value (c ) new c(e) new c(e) No transition (stuck) if c is not a sub-class of c! Sh/could introduce error transitions for cast failure. 26

Dynamic Semantics Search rules (CBV): e 0 e 0 e 0.f e 0.f e 0 e 0 e 0.m(e) e 0.m(e) e 0 value e e e 0.m(e) e 0.m(e ) 27

Dynamic Semantics Search rules (CBV), cont d: e e new c(e) new c(e ) e 0 e 0 (c) e 0 (c) e 0 28

Dynamic Semantics Dynamic dispatch: T (c) = class c extends c {...;... d} d i = c i m(c i x) {return e;} body(m i, c) = x e T (c) = class c extends c {...;... d} m / d body(m, c ) = x e body(m, c) = x e Climbs the class hierarchy searching for the method. Static semantics ensures that the method must exist! 29

Type Safety Theorem 1 (Preservation) Assume that T is a well-formed class table. If e : τ and e e, then e : τ for some τ <: τ. Proved by induction on transition relation. Type may get smaller during execution due to casting! 30

Type Safety Lemma 2 (Canonical Forms) If e : c and e value, then e = new d(e 0 ) with d c and e 0 value. Values of class type are objects (instances). The dynamic class of an object may be lower in the subtype hierarchy than the static class. 31

Type Safety Theorem 3 (Progress) Assume that T is a well-formed class table. If e : τ then either 1. v value, or 2. e has the form (c) new d(e 0 ) with e 0 value and d c, or 3. there exists e such that e e. 32

Type Safety Comments on the progress theorem: Well-typed programs can get stuck! only because of a cast.... But Precludes message not understood error. Proof is by induction on typing. 33

Variations and Extensions A more flexible static semantics for override: Subclass result type is a subtype of the superclass result type. Subclass argument types are supertypes of the corresponding superclass argument types. 34

Variations and Extensions Java adds arrays and covariant array subtyping: τ <: τ τ [ ] <: τ [ ] What effect does this have? 35

Variations and Extensions Java adds array covariance: τ <: τ τ [ ] <: τ [ ] Perfectly OK for FJ, which does not support mutation and assignment. With assignment, might store a supertype value in an array of the subtype. Subsequent retrieval at subtype is unsound. Java inserts a per-assignment run-time check and exception raise to ensure safety. 36

Variations and Extensions Static fields: Must be initialized as part of the class definition (not by the constructor). In what order are initializers to be evaluated? Could require initialization to a constant. 37

Variations and Extensions Static methods: Essentially just recursive functions. No overriding. Static dispatch to the class, not the instance. 38

Variations and Extensions Final methods: Preclude override in a sub-class. Final fields: Sensible only in the presence of mutation! 39

Variations and Extensions Abstract methods: Some methods are undefined (but are declared). Cannot form an instance if any method is abstract. 40

Class Tables Type checking requires the entire program! Class table is a global property of the program and libraries. Cannot type check classes separately from one another. 41