1 / 23

Pizza

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

nedra
Télécharger la présentation

Pizza

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pizza Java on the move?

  2. 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.

  3. 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.

  4. Pizza references • wadler@research.bell-labs.com • http://cm.bell-labs.com/cm/cs/who/wadler/pizza

  5. 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.

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

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

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

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

  10. 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”

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

  12. 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.

  13. 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?

  14. 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.

  15. 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?

  16. 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.

  17. 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.

  18. 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.

  19. 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)); } } }

  20. Algebraic Types in Pizza List as = Cons(‘c’,Nil); List zs = Cons(‘a’,Cons(‘b’,Nil)).append(as);

  21. 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)); }}}

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

  23. 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”

More Related