230 likes | 404 Vues
Pizza. Java on the move?. Java super-set. Already people are adding features to Java. Pizza, Martin Odersky, Univ. of Karlsruhe Phillip Wadler, Univ of Glasgow is one that is academically interesting. Pizza. Adds to Java Parametric polymorphism
E N D
Pizza Java on the move?
Java super-set • Already people are adding features to Java. • Pizza, • Martin Odersky, Univ. of Karlsruhe • Phillip Wadler, Univ of Glasgow is one that is academically interesting.
Pizza • Adds to Java • Parametric polymorphism • superficially similar to C++ templates, but based on Hindley/Milner typing system of ML and Haskell • First-class functions • similar to the blocks of Smalltalk. Functions and procedures may be passed as parameters, stored in variables, and returned from methods. • Case classes (algebraic types) and pattern matching.
Pizza references • wadler@research.bell-labs.com • http://cm.bell-labs.com/cm/cs/who/wadler/pizza
Pizza • Pizza is a superset of Java • Programs that are written in Pizza are translated into Java, thus nothing new is required of the Java Language • A tool, ExpressoGrinder translates Pizza into Java.
Parametric Polymorphism • Parametric polymorphism means types can be parameters. • Parametric Polymorphism in Pizza class Pair<elem> { elem x; elem y; Pair(elem x, elem y) { this.x = x; this.y = y; } }
Using Parametric Polymorphism class Pair<elem> { elem x; elem y; Pair(elem x, elem y) { this.x = x; this.y = y; } } Pair<String> p = new Pair(“Hello”,”World”); Pair<int> z = new Pair(22,33);
Implementation • A macro approach Pair<int> z = new Pair(22,33); causes the translator to generate class Pair_int { int x, int y; Pair_int(int x, int y) {this.x = x; this.y = y } } Pair_int z = new Pair_int(22,33);
Implementation • An inheritance approach Pair<int> z = new Pair(22,33); causes the translator to generate class Pair { Object x, Object y; Pair(Object x, Object y) {this.x = x; this.y = y } } Pair z = new Pair( (Object)new Integer(22), (Object)new Integer(33));
Interesting issues • The interesting issues are with inheritance • In Java single inheritance by extension, multiple inheritance by interface. • Pizza allows a type variable to take on any class that is a subclass of a given class, or any class that implements an interface. • This is called “bounded polymorphism”
Bounded polymorphism interface Ord<elem> { boolean less(elem o); } ; class Pair<elem implements Ord<elem>> { elem x; elem y; Pair(elem x, elem y) {. . .} elem min() { if (x.less(y))return x; else return y; } }
Problems with bounded polymorphism • Pizza had problems with arrays and subtyping, and would have to do some extra run-time checking to pull it off. • Java 1.2 Reflection classes could have made this easier • Design of Pizza for now excludes polymorphic arrays.
Higher order functions • Treating functions as data can be convenient. Higher order functions or first class functions refer to the ability to treat functions as objects. • for example f().integral(2,5) is sending the function f the message “what is your integral from 2 to 5?
Higher order functions & oop • OO style partially supports higher order functions. • Functions are methods, methods are part of objects, objects are data. So functions may be treated as data. Pizza just makes it more appealing as to the syntax.
Implementation • Functions have • formal parameters • free variables (belong to enclosing scope) • local variables • When passing functions as data, formal parameters and local variables are not a problem. But free what about free variables?
Implementation of free variables • At the time a function is made data the free variables should have values. • They could be passed • as references, • as values (immutable) providing design alternatives. Pizza uses references.
No more higher order functions? • Reflection added the ability to treat a method of a class as an object, but at the time of Pizza this was not the case. • Inner classes in Java 1.1 diminishes the need for higher order functions in Java. Inner classes hide a class in the scope of another class supporting free variables. • Serialization also allows one to treat a method as data.
Algebraic Types in Pizza • Algebraic types are a language capability usually unfamiliar to imperative programmers. • Algebraic types may be viewed as variant records with a recursive definition.
Algebraic Types in Pizza class List { case Nil; case Cons(char head, List tail); List append(List ys) { switch(this) { case Nil: return ys; case Cons(Char x, List xs): return Cons(x,xs.append(ys)); } } }
Algebraic Types in Pizza List as = Cons(‘c’,Nil); List zs = Cons(‘a’,Cons(‘b’,Nil)).append(as);
Implementation class List { final int Nil_tag = 0; final int Cons_tag = 1; int tag; List append(list ys) { switch(this.tag) { case Nil_tag: return ys; case Cons_tag: char x = ((Cons)this).head; List ys = ((Cons)this).tail; return new Cons(x,xs.append(ys)); }}}
Implementation class Nill extends List { Nil() {this.tag = Nil_tag;} } class Cons extends List { char head; List tail; Cons(char head, List tail) { this.tag = Cons_tag; this.head = head, this.tail = tail;} }
Watch for parametric polymorphism <templates> • Wadler at Bell labs has left academics. He is now working on GJ which omits algebraic types and higher order functions from Pizza, but translates to the VM. • This work is being done in collaboration with Sun Microsystems - which suggests its inclusion soon. I will say “I told you so”