Generic Programming Constructs and Applications in Object ... · programming w.r.t. mainstream...

Post on 05-Jul-2020

14 views 0 download

Transcript of Generic Programming Constructs and Applications in Object ... · programming w.r.t. mainstream...

Overview Reification Usability P@J References

Generic Programming Constructs andApplications in Object-Oriented Languages

Maurizio Cimadamoremaurizio.cimadamore@[unibo.it|sun.com]

aliCE research groupAlma Mater Studiorum—Universita di Bologna

Sun Microsystems Ireland Ltd.

tutors: Antonio Natali, Andrea Omicini, Mirko ViroliPh.D final seminar - XXII cycle

13, January 2010

Overview Reification Usability P@J References

Outline

1 OverviewGeneric Programming in Java

2 Reification

3 UsabilityDiamond OperatorError Messages

4 P@J

5 References

Overview Reification Usability P@J References

Outline

1 OverviewGeneric Programming in Java

2 Reification

3 Usability

4 P@J

5 References

Overview Reification Usability P@J References

Overview

Goal

Evaluation of advanced constructs and application of genericprogramming w.r.t. mainstream Object-Oriented programminglanguages

Areas

• Foundations — design of new features for improving genericprogramming

• Usability — making generic programming available to themainstream

• Applications — design of a framework exploiting advancedgeneric programming concepts

Overview Reification Usability P@J References

Overview

Case study: Java

• 6000000 developers

• 48000000 download/update per month

• 6 bln of Java-enabled devices

• available on 91% PC (> Windows!)

• open-source

Generic Programming in Java

Generics programming support since JDK 5.0 (2004)

• List<String>, HashMap<String, Integer>

Overview Reification Usability P@J References

Java Generics

History

• Java 1.0 announced in 1995

• Generics considered, but omitted from final language

• Proposals for adding generics (JSR14) starting in 1997

• Generics included in Java 5 in 2004

Generic Idiom (JDK 1.4)

• Java collection classes defined to hold Object:• List, Stack, Queue, Set, SortedSet, ...

• No restrictions on what can be added

• Casts required when retrieving elements

Overview Reification Usability P@J References

Java Generics

Generic Types (JDK 5.0)

• Java collection classes have been generified:• List<E>, Stack<E>, Queue<E>, Set<E>, SortedSet<E>, ...

• Only element of type E can be added

• No casts required when retrieving elements

Type-erasure

Generic types are turned into non-generic types during compilation

• near 100% backward compatibility

• no performance hit

Overview Reification Usability P@J References

Outline

1 Overview

2 Reification

3 Usability

4 P@J

5 References

Overview Reification Usability P@J References

Reification of generic types

Problem

Generic types disallowed in type-dependent operations

Goal

Develop a JVM with builtin support for generic types

• Low performance overahed

• No new bytecode instructions

• Completeness (generic classes, methods, wildcards...)

Case study

• CVM 1.0.2 (J2ME CDC)

• JVM Hotspot (OpenJDK)

Overview Reification Usability P@J References

Reification: Overview

Compile-time

The compiler stores additional generic type-info in custom classfileattributes

Run-time

The generic JVM leverages such information when performingtype-dependent operations

Overview Reification Usability P@J References

Reification — Conclusions

Benchmarks

• Execution-time overhead: < 2%

• Memory footprint: < 3%

• Classfile size: < 4%

Open Issues

• Backward-compatibility w.r.t. raw cast semantics

• Indecidable type-system problematic when performing runtimetype-tests

Overview Reification Usability P@J References

Outline

1 Overview

2 Reification

3 UsabilityDiamond OperatorError Messages

4 P@J

5 References

Overview Reification Usability P@J References

Usability of Java Generics

Goals

Simplify and improve generic programming experience in the JavaProgramming Language

• New syntax for less verbose creation of generic instances

• Provide better support for error messages involvinggenerics/wildcards

Widespread distribution

The results of this work will be included in the next official releaseof the Java Development Kit (JDK 7)

Overview Reification Usability P@J References

Usability — Diamond Operator

Problem

Generics can easily lead to verbose declarations:

• List<String> l = new ArrayLisy<String>();

• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<List<String>, List<Integer>> m = new HashMap<List<String>, List<Integer>>();

Goal

Minimize the amount of explicit types in generic declarations in themost frequent use-cases

Case study

javac/OpenJDK (JDK 7)

Overview Reification Usability P@J References

Usability — Diamond Operator

Problem

Generics can easily lead to verbose declarations:

• List<String> l = new ArrayLisy<String>();• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<List<String>, List<Integer>> m = new HashMap<List<String>, List<Integer>>();

Goal

Minimize the amount of explicit types in generic declarations in themost frequent use-cases

Case study

javac/OpenJDK (JDK 7)

Overview Reification Usability P@J References

Usability — Diamond Operator

Problem

Generics can easily lead to verbose declarations:

• List<String> l = new ArrayLisy<String>();• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<List<String>, List<Integer>> m = new HashMap<List<String>, List<Integer>>();

Goal

Minimize the amount of explicit types in generic declarations in themost frequent use-cases

Case study

javac/OpenJDK (JDK 7)

Overview Reification Usability P@J References

Usability — Diamond Operator

Problem

Generics can easily lead to verbose declarations:

• List<String> l = new ArrayLisy<String>();• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<List<String>, List<Integer>> m = new HashMap<List<String>, List<Integer>>();

Goal

Minimize the amount of explicit types in generic declarations in themost frequent use-cases

Case study

javac/OpenJDK (JDK 7)

Overview Reification Usability P@J References

Diamond Operator: Overview

At a glance

• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<String, Integer> m = new HashMap<>();

Idea

Types in the RHS are inferred from types in the LHS

• Less verbose

• Interface/abstract classes allowed in the LHS

• Non ambiguous syntax (w.r.t. raw types)

Two variants

• Simple: only types in LHS are used

• Complex: uses both LHS and constructor argument types

Overview Reification Usability P@J References

Diamond Operator: Overview

At a glance

• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<String, Integer> m = new HashMap<>();

Idea

Types in the RHS are inferred from types in the LHS

• Less verbose

• Interface/abstract classes allowed in the LHS

• Non ambiguous syntax (w.r.t. raw types)

Two variants

• Simple: only types in LHS are used

• Complex: uses both LHS and constructor argument types

Overview Reification Usability P@J References

Diamond Operator: Overview

At a glance

• Map<String, Integer> m = new HashMap<String, Integer>();

• Map<String, Integer> m = new HashMap<>();

Idea

Types in the RHS are inferred from types in the LHS

• Less verbose

• Interface/abstract classes allowed in the LHS

• Non ambiguous syntax (w.r.t. raw types)

Two variants

• Simple: only types in LHS are used

• Complex: uses both LHS and constructor argument types

Overview Reification Usability P@J References

Diamond Operator: Conclusions

Benchmarks

• Simple: 89% sites inferred

• Complex: 93% sites inferred

Complex is better

• Allows for language evolution (esp. w.r.t. method inference intype-argument position)

• Part of JDK 7

Open Issues

• LHS diamond

Overview Reification Usability P@J References

Usability — Error Messages

Problem

Error messages involving generics are often obscure

• The ultimate cause of the error could be buried in the JLS

• More information often required in order to understand agiven message

Goal

Error message should be easy to understand even for programmerswith no generic skills

Case study

javac (JDK 7)

Overview Reification Usability P@J References

Error Messages: Overview

Solution

Brand new diagnostic subsystem

• Highly configurable (generates text, XML, ...)

• Tightly integrated with Java type-system

Before

incompatible typesfound : Object&I1&I2required: A

After

incompatible typesrequired: Afound: INT#1where INT#1 is an intersection type:INT#1 extends Object,I1,I2

Overview Reification Usability P@J References

Error Messages: Overview

Solution

Brand new diagnostic subsystem

• Highly configurable (generates text, XML, ...)

• Tightly integrated with Java type-system

Before

incompatible typesfound : Object&I1&I2required: A

After

incompatible typesrequired: Afound: INT#1where INT#1 is an intersection type:INT#1 extends Object,I1,I2

Overview Reification Usability P@J References

Error Messages: Overview

Solution

Brand new diagnostic subsystem

• Highly configurable (generates text, XML, ...)

• Tightly integrated with Java type-system

Before

incompatible typesfound : Object&I1&I2required: A

After

incompatible typesrequired: Afound: INT#1where INT#1 is an intersection type:INT#1 extends Object,I1,I2

Overview Reification Usability P@J References

Error Messages: J1 2009 Demo

Hyperlinks!

Overview Reification Usability P@J References

Error Messages: Conclusions

Integrated in JDK 7

Very positive feedback during J1 Demo 2009

Open Issues

• DTD for standardized XML error messages

• Integration with mainstream Java IDE (NetBeans, Eclipse,IntelliJ, ...)

Overview Reification Usability P@J References

Outline

1 Overview

2 Reification

3 Usability

4 P@J

5 References

Overview Reification Usability P@J References

Java-Prolog Integration

Problem

Existing approaches for using Prolog from Java (and vice-versa)are only half-baked solutions

• lack of true integration between host and target language

• usually lead to lot of boilerplate code

Goal

Develop a framework that allows true Java/Prolog interoperability

• Language integration achieved through existing constructs inthe Java programming language (esp. generics)

• Automatic marshalling from Java to Prolog (and back)

Case study

tuProlog engine

Overview Reification Usability P@J References

P@J : Key Ideas

Idea #1 : Bidirectionality through generics/wildcards

• X in Term<X> denotes the kind of a (concrete) Prolog term

• Term<Int> is a placeholder for both:• Int – as Int extends Term<Int>• Var<Int> – as Var<X> extends Term<X>

Idea #2 : Java/Prolog mapping through annotations

• Prolog code attached to abstract Java methods via annotation

• method’s type variables correspond to predicate logic variables

• predicate yielding multiple results → Iterable

Overview Reification Usability P@J References

P@J : ExampleA parser in P@J@PrologClass

abstract class ExprParserVal {

@PrologMethod (clauses={"parse_expr(E,L):-phrase(expr(E),L).",

"expr(E) --> term(T), expr2(T,E).",

"expr2(T,E) --> [’+’],term(T2),expr2(plus(T,T2),E).",

"expr2(T,E) --> [’-’],term(T2),expr2(minus(T,T2),E).",

"expr2(T,T) --> [].",

"term(T) --> fact(F), term2(F,T).",

"term2(F,T) --> [’*’],fact(F2),term2(times(F,F2),T).",

"term2(F,T) --> [’/’],fact(F2),term2(div(F,F2),T).",

"term2(F,F) --> [].",

"fact(E) --> [’(’],expr(E),[’)’].",

"fact(X) --> [X],{number(X)}."})

abstract <$L extends Term<?>, $E extends List<?>> $L parse($E expr);

public static void main(String[] args) throws Exception {

ExprParserVal ep = PJ.newInstance(ExprParserVal.class);

List<Object> tokenied_expr =

new List(Arrays.asList(new Object[] {1,"+",2, "*", 3});

Term<?> parsed_expr = ep.parse_expr(tokenized_expr);

}

}

Overview Reification Usability P@J References

P@J : Conclusions

Advanced Features

• Custom annotation processor for easy cross-language checking

• Stateful representation through instance theory

• Full backtracking support when calling P@J methods fromProlog code

• Custom Object values automatically turned into Prolog terms(P@J’s call-by-value)

Prototype available - www.alice.unibo.it/patj

Open Issues

• Performance tuning (goal: one tuProlog engine per P@Jframework!)

Overview Reification Usability P@J References

Outline

1 Overview

2 Reification

3 Usability

4 P@J

5 References

Overview Reification Usability P@J References

Riferimenti I

M. Cimadamore and M. Viroli.A Prolog-oriented extension of Java programming based ongenerics and annotations.In PPPJ ’07: Proceedings of the 5th international symposiumon Principles and practice of programming in Java, pages197–202, New York, NY, USA, 2007. ACM.

M. Cimadamore and M. Viroli.Integrating Java and Prolog using Java 5.0 generics andannotations.In MPOOL ’07: Proceedings of 6th International Workshop onMultiparadigm Programming with 6th workshop onMultiparadigm Programming with Object-Oriented Languages,2007.

Overview Reification Usability P@J References

Riferimenti II

M. Cimadamore and M. Viroli.Reifying wildcards in Java using the EGO approach.In SAC ’07: Proceedings of the 2007 ACM symposium onApplied computing, pages 1315–1322, New York, NY, USA,2007. ACM.

M. Cimadamore and M. Viroli.Integrating Java and Prolog through generic methods and typeinference.In SAC ’08: Proceedings of the 2008 ACM symposium onApplied computing, pages 198–205, New York, NY, USA,2008. ACM.

M. Cimadamore and M. Viroli.On Reification of Java Wildcards.Science of Computer Programming, 2008.

Overview Reification Usability P@J References

Riferimenti III

A. Ricci, M. Viroli, and M. Cimadamore.Prototyping Concurrent Systems with Agents and Artifacts:Framework and Core Calculus.In FOCLASA ’07: Proceedings of the 6th InternationalWorkshop on the Foundations of Coordination Languages andSoftware Architectures, 2007.

A. Ricci, M. Viroli, and M. Cimadamore.Prototyping Concurrent Systems with Agents and Artifacts:Framework and Core Calculus.Electron. Notes Theor. Comput. Sci., 194(4):111–132, 2008.

Overview Reification Usability P@J References

Generic Programming Constructs andApplications in Object-Oriented Languages

Maurizio Cimadamoremaurizio.cimadamore@[unibo.it|sun.com]

aliCE research groupAlma Mater Studiorum—Universita di Bologna

Sun Microsystems Ireland Ltd.

tutors: Antonio Natali, Andrea Omicini, Mirko ViroliPh.D final seminar - XXII cycle

13, January 2010

Overview Reification Usability P@J References

Sun Microsystems

Timeline

• 2003 — internship Mountain View, California, USA

• 2005-2007 — joint project DEIS - Sun Microsystems

• Dicembre 2007 - now — dipendente Sun Microsystem

Ricerca & Lavoro

• Ricerca su nuovi costrutti linguaggio Java (e.g. functiontypes)

• Generici e wildcards (bug & specifiche)

• Nuovi linguaggi (JavaFX)