hello, type systems! - introduction to featherweight java

27
Hello, Type Systems! Introduction to Featherweight Java Cheshire Cat (@y_taka_23) Shibuya Java #12 (Aug. 1, 2015)

Upload: ytaka23

Post on 17-Aug-2015

452 views

Category:

Technology


0 download

TRANSCRIPT

Hello, Type Systems!Introduction to Featherweight Java

Cheshire Cat (@y_taka_23)Shibuya Java #12 (Aug. 1, 2015)

Who Am I?

● HN: Cheshire Cat● Twitter: @y_taka_23● GitHub: y-taka-23● Favorites

○ Formal methods○ Functional programming

In the Era of Java 1.4

The following is compilable, but ...

public static void main(String[] args) {

List list = new ArrayList();

list.add(23);

String str = (String) list.get(0);

}

In the Era of Java 1.4

Implicite upcast from int to Object

public static void main(String[] args) {

List list = new ArrayList();

list.add(23);

String str = (String) list.get(0);

}

In the Era of Java 1.4

Downcast from Object to String

public static void main(String[] args) {

List list = new ArrayList();

list.add(23);

String str = (String) list.get(0);

}

In the Era of Java 1.4

Run-time exception!!

public static void main(String[] args) {

List list = new ArrayList();

list.add(23);

String str = (String) list.get(0);

}

What is the “Type Safety”?

Featherweight Java (FJ)

● Introduced by IGARASHI et al. (2002)● Formalized & minimalized model● Many features are omitted, e.g.

○ Assignment○ Interfaces○ Exceptions

● “Functional” fragment of Java

Three Points to Understand FJ

1. Expressions: What is the “programs”?

2. Typing: What is the “type check”?

3. Reduction: What is the “execution”?

Expressions

● Variables

● Constructors

● Field accesses

● Method invocations

● Casts

x1, x2, ...

new C(e1, ..., en)

e.f

e.m(x1, ..., xn)

(C)e

Typing Rules

Example: Rule for field accesses

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

Premise

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

Conclusion

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

Type env: Map from variable names to types

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

If e0 has type C0 under Gamma,

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

and fields of class C0 are

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

f1, ..., fn of classes C1, ..., Cn,

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Typing Rules

then e0.fi has type Ci under Gamma

Gamma |- e0 : C0

fields(C0) = C1 f1, ..., Cn fn

--------------------------------

Gamma |- e0.fi : Ci

Reduction Rules

Example: Rule for field accesses

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

Reduction Rules

Premise

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

Reduction Rules

Conclusion

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

Reduction Rules

If fields of class C are ...

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

Reduction Rules

f1, ..., fn of classes C1, ..., Cn,

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

Reduction Rules

then(new ...).fi reduces to ei

fields(C) = C1 f1, ..., Cn fn

--------------------------------

(new C(e1, ..., en)).fi -> ei

“Type Safety” in FJ

If an expression e satisfies:● e has a type without the two “impolite” rules● e -> ... -> e’● No expression e’’ such that e’ -> e’’

Then e’ must be “completely reduced”, i.e.● e consists of only constructors

Summary

● FJ is the “minimum” model of Java● “Type safety” is the consistency of

○ Typing rules○ Reduction rules

● We can discuss properties ofprogramming languages by formalization

Have a Nice Typing!

Presented byCheshire Cat (@y_taka_23)