Title: Pizza into Java: Translating theory into practice
1Pizza into JavaTranslating theory into practice
- CS8803F Object-Oriented
- Systems and Languages
- Paper Presentation
- Nikitas Liogas
2Pizza a name that appeals to hackers!?
- Pizza strict superset of Java incorporates
three ideas from the academic community - parametric polymorphism
- higher-order functions
- algebraic data types
Pizza is defined by translation into Java
(details later) and compiles into the JVM
3A few words about translation
- They like translations so much that they have
- two of them!
- heterogenous translation produces a
specialized copy of code for each type - homogenous translation uses a single copy
with a universal representation
Remember GJ? Homogenous translation is used there
as well (called erasure) .
4Parametric polymorphism
- Pizza code (guess what this prints out!)
- class Pair elem x,yPair(elem x, elem y)
this.xx this.yy void swap() elem tx
xy yt -
- Pair p new Pair(world!, Hello)
- p.swap()
- System.out.println(p.x p.y)
5Parametric polymorphism heterogenous
translation
- class Pair_String // macro expand a version ?
typeString x,yPair_String(String x, String y)
- this.xx this.yy void swap() String
tx xy yt -
- Pair_String p new Pair_String(world!,
Hello) - p.swap()
- System.out.println(p.x p.y)
6Parametric polymorphism homogenous translation
- class Pair // replace the type variable with
ObjectObject x,yPair(Object x, Object y) - this.xx this.yy void swap() Object
tx xy yt -
- Pair p new Pair( (Object)world!,
(Object)Hello) - p.swap()
- System.out.println( (String)p.x (String)p.y)
NOTE If the type variable assumes a base type
value (e.g. int) a standard Java wrapper class is
used (e.g. Integer)
7Bounded and F-bounded polymorphism
- interface Ord
- boolean less(elem o)
- // bounded type variable appears recursively in
its own bound! - class Pair elem
x,y Pair(elem x, elem y) this.xx
this.yy elem min() if ( x.less(y) ) return
x else return y -
- class OrdInt implements Ord //
extension of Integer int jOrdInt(int j)
this.j j int intValue() return j
boolean less(OrdInt o) return j o.intValue() -
- Pair p new Pair( new OrdInt(10), new
OrdInt(20) ) - System.out.println( p.min().intValue() ) //
prints out 10
8Take a breath!
- Lets take a break away from code lets talk
higher-order (aka first-class) functions - Convenient to treat functions as data (pass as
arguments, return as results, store in
variables) - Syntax (argtype, , argtype)-resulttype or
(argtype, )throws exception-resulttype for
example (String, int)-void
9Here we go again! Higher-order function example
- class PrintSortedStrings void print(String
args, (String, String)-int compare) if
( compare(argsj, argsk) 0 ) - // print sorted strings here
-
-
- int myCompareNoCase(String s1, String s2)
return s1.toLowerCase().compareTo(
s2.toLowerCase() ) -
- String myStrings These, will, be,
sorted,!) - // Pass the arguments as well as the function to
apply to them! - PrintSortedStrings.print(myStrings,
myCompareNoCase)
10Algebraic data types
- Avoid the complicated abstract class/concrete
cases construction use switch statement
instead! - class List case Nilcase Cons(char head, List
tail) // these args are implicit instance
varsList append(List l) switch(this)
case Nil return l case Cons(char h,
List t) return Cons( h, t.append(l) ) -
-
-
- List list Cons( a, Cons(b, Nil)
).append(Cons(c, Nil))
11Pattern matching
- The switch statement is very powerful
- it selects the case of the right instance
- it assigns all variables in the constructed case
with the corresponding fields - We can replace parameters we dont want to
access with a single _ - Deep pattern matching allowed
- Also convenient for the simple case class
Pair case Pair(A fst, B snd)
12Rough edges
- Casting Pizza has a more sophisticated type
system than Java (fewer runtime tests)
translation incurs an extra cost - Dynamic loading fixed class loader used
having security troubles with user-defined
class loaders - Interfaces for built-in classes have to define
redundant classes, because some Java classes
are final - Arrays poor fit between polymorphic arrays in
Pizza and their homogenous translation into Java
13Current Status
- full blown compiler written in Pizza can be a
preprocessor by setting s during compilation - 2x faster compilation than javac on Solaris 5.5
- generated bytecode 0-20 slower (no code
optimization done until now) - cannot read .jar files !?
- Pizza development is now in a dormant state
focus is placed mainly on GJ
14 15Bonus slide
- Extra Pizza features
- Anonymous functions syntax fun(arguments)-resu
lttype - Enumerations define a class with constant
subclasses - Tail call recursion tell the compiler not to
use the stack for a deep recursive function