Featherweight Java (FJ)

Similar documents
Featherweight Java. Lecture 12 CS 565 3/18/08

Lecture 13: Subtyping

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

Software Engineering

Subtyping. Lecture 13 CS 565 3/27/06

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

Softwaretechnik. Lecture 18: Featherweight Java. Peter Thiemann. University of Freiburg, Germany

Contents. Softwaretechnik. Type Safety of Java. Featherweight Java. Lecture 21: Featherweight Java

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

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

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Part III. Chapter 15: Subtyping

Type Hierarchy. Lecture 6: OOP, autumn 2003

Programming Languages and Techniques (CIS120)

Part VI. Imperative Functional Programming

Part III Chapter 15: Subtyping

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

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Programming Languages and Techniques (CIS120)

ITI Introduction to Computing II

Harvard School of Engineering and Applied Sciences Computer Science 152

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

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

ITI Introduction to Computing II

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Message Safety in Dart

Some instance messages and methods

CS4215 Programming Language Implementation. Martin Henz

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

A Type System for Functional Traversal-Based Aspects

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

CS 4110 Programming Languages & Logics. Lecture 30 Featherweight Java and Object Encodings

Simply-Typed Lambda Calculus

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

Object typing and subtypes

Programming Languages

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

Featherweight Wrap Java: wrapping objects and methods

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Lambda Calculi With Polymorphism

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

Dependent Object Types - A foundation for Scala s type system

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Coinductive big-step operational semantics for type soundness of Java-like languages

Inference, Targeting and Compatibility in a Type System for Java with SAM Typed Closures

Programming Languages and Techniques (CIS120)

Science of Computer Programming

The Untyped Lambda Calculus

Semantic Subtyping for Objects and Classes

type classes & locales

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

Subsumption. Principle of safe substitution

COS 320. Compiling Techniques

Foo A Minimal Modern OO Calculus

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Existential Types for Variance Java Wildcards and Ownership Types

Symmetry in Type Theory

Java Object Oriented Design. CSC207 Fall 2014

A Co-contextual Type Checker for Featherweight Java (incl. Proofs)

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Programming Languages Lecture 15: Recursive Types & Subtyping

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

Lambda Calculi With Polymorphism

Javari: Adding Reference Immutability to Java

Object Oriented Issues in VDM++

Recursive Types and Subtyping

Incremental Reasoning for Multiple Inheritance

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

Generic programming POLYMORPHISM 10/25/13

SCHOOL: a Small Chorded Object-Oriented Language

CS558 Programming Languages

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

301AA - Advanced Programming [AP-2017]

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

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

Lecture Notes on Programming Languages

Delegation by Object Composition

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Compilation 2012 Static Type Checking

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Inheritance and Polymorphism. CS180 Fall 2007

Self Type Constructors. Atsushi Igarashi Kyoto University Joint work with Chieri Saito

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

The Untyped Lambda Calculus

Exercise 12 Initialization December 15, 2017

Recursive Types and Subtyping

Programming Languages and Techniques (CIS120)

FJMIP: a calculus for a modular object initialization

Java: introduction to object-oriented features

λ calculus is inconsistent

Transcription:

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 University

Motivation What is the essence of OO? What expressiveness to include? Classes, interfaces? Static, instance fields?... What calculus to use as the foundation? Lambda calculus? 2

Overview Featherweight Java (FJ): a minimal Java-like language; models inheritance and subtyping; immutable objects: no mutation of fields; trivialized core language.

FJ -- Abstract syntax 4

Abstract Syntax Classes in FJ have the form: class c extends c {cf; kd} Class c is a sub-class of class c. Constructor k for instances of c. Fields cf. underlining indicates one or more. Methods d.

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

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

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

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; }

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; }

Abstract Syntax The abstract syntax of FJ is given by the following grammar: Classes C ::= class c extends c {cf; kd} Constructors k ::= c(c x) {super(x); this.f =x;} Methods d ::= cm(cx) {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.

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.

FJ -- Static semantics 13

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

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

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

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.

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

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.

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!

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

Class Formation Well-formed classes: k = c(c x, cx) {super(x ); this.f=x;} fields(c )=c f d i ok in c class c extends c {cf; kd} 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.

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(cx) {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.

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

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

FJ -- Dynamic semantics 26

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

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

Dynamic Semantics Field selection: fields(c) =c f,cf e value e value new c(e, e).f i e i fields(c) =c f,cf 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.

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.

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.

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 )

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

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!

Type safety = Preservation + Progress 35

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!

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.

Type Safety Theorem 3 (Progress) Assume that T is a well-formed class table. If e : τ then either e 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.

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.

Summary: The essence of OO Designated calculus (instead of lambda calculus) Operational semantics with type safety Subclasses / subtyping covered Immutable instead of imperative fields modeled Prepping: Types and Programming Languages Chapters 19 but also 15-18 Outlook: Process calculi Denotational semantics 40

Variations and extensions 41

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.

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

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.

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.

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

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

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