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

Hello, Type Systems! - Introduction to Featherweight Java

Aug 17, 2015

Download

Technology

y_taka_23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hello, Type Systems! - Introduction to Featherweight Java

Hello, Type Systems!Introduction to Featherweight Java

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

Page 2: Hello, Type Systems! - Introduction to Featherweight Java

Who Am I?

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

○ Formal methods○ Functional programming

Page 3: Hello, Type Systems! - Introduction to Featherweight Java

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

}

Page 4: Hello, Type Systems! - Introduction to Featherweight Java

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

}

Page 5: Hello, Type Systems! - Introduction to Featherweight Java

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

}

Page 6: Hello, Type Systems! - Introduction to Featherweight Java

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

}

Page 7: Hello, Type Systems! - Introduction to Featherweight Java

What is the “Type Safety”?

Page 8: Hello, Type Systems! - Introduction to Featherweight Java

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

Page 9: Hello, Type Systems! - Introduction to Featherweight 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”?

Page 10: Hello, Type Systems! - Introduction to Featherweight Java

Expressions

● Variables

● Constructors

● Field accesses

● Method invocations

● Casts

x1, x2, ...

new C(e1, ..., en)

e.f

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

(C)e

Page 11: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

Example: Rule for field accesses

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 12: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

Premise

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 13: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

Conclusion

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 14: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

Type env: Map from variable names to types

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 15: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

If e0 has type C0 under Gamma,

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 16: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

and fields of class C0 are

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 17: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

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

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 18: Hello, Type Systems! - Introduction to Featherweight Java

Typing Rules

then e0.fi has type Ci under Gamma

Gamma |- e0 : C0

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

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

Gamma |- e0.fi : Ci

Page 19: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

Example: Rule for field accesses

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

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

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

Page 20: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

Premise

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

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

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

Page 21: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

Conclusion

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

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

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

Page 22: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

If fields of class C are ...

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

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

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

Page 23: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

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

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

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

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

Page 24: Hello, Type Systems! - Introduction to Featherweight Java

Reduction Rules

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

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

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

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

Page 25: Hello, Type Systems! - Introduction to Featherweight Java

“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

Page 26: Hello, Type Systems! - Introduction to Featherweight Java

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

Page 27: Hello, Type Systems! - Introduction to Featherweight Java

Have a Nice Typing!

Presented byCheshire Cat (@y_taka_23)