HOT Programming in Java COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Advertisements

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Dynamic Typing COS 441 Princeton University Fall 2004.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Standard ML- Part I Compiler Baojian Hua
CSE341: Programming Languages Lecture 23 OO vs. Functional Decomposition; Adding Operations & Variants; Double-Dispatch Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 27 Generics vs. Subtyping; Bounded Polymorphism Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
Foundations of Programming Languages: Introduction to Lambda Calculus
Quick Review Get paper and pencil out. Not graded just for review.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
Data Abstraction COS 441 Princeton University Fall 2004.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Aggregate Data Structures COS 441 Princeton University Fall 2004.
Feather-Weight Java COS 441 Princeton University Fall 2004.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Semantics for MinML COS 441 Princeton University Fall 2004.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
The Kotlin Programming Language
Language Evaluation Criteria
Chapter 1 - Introduction
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
1 Programming Language History and Evolution In Text: Chapter 2.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Implications of Inheritance COMP204, Bernhard Pfahringer.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch Dan Grossman Spring 2013.
Types in programming languages1 What are types, and why do we need them?
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Introduction to Functional Programming Part 1 – The Basic Concepts Winter Young
Object Oriented Programming Some Interesting Genes.
Programming Language History and Evolution
C11, Implications of Inheritance
Functional Programming
Type Checking and Type Inference
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
A lightening tour in 45 minutes
ML Programming Language Design and Implementation (4th Edition)
CSE341: Programming Languages Lecture 22 OOP vs
Multi-Dispatch in the Java™ Virtual Machine
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Focus of the Course Object-Oriented Software Development
CSE341: Programming Languages Lecture 22 OOP vs
6.001 SICP Interpretation Parts of an interpreter
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 22 OOP vs
Drew Wyborski Programming Languages
CSE341: Programming Languages Lecture 22 OOP vs
Presentation transcript:

HOT Programming in Java COS 441 Princeton University Fall 2004

What’s HOT Term coined by Bob Harper Higher-Order and Typed Some HOT languages SML, Haskell, Java, C#

Features of HOT Languages Type-Safe Lexical Closures Garbage Collected

A HOT Programming Style SML is arguably “HOTer” than most language –Immutable by default –Supports inductive datatypes and pattern matching –Advanced module system –Second-Class Polymorphism with Type Inference How does Java stack up?

Object Orientated vs HOT Programming Mitchell asked the question (10.2.5) “Can you program in an Object Oriented way in ML?” Today’s question is “Can you program in a HOT way in and Object Oriented language?” Alternatively How can I use what I learned about programming in SML and use it in a “real language”

A Simple Example datatype nat = Zero | Succ of nat fun add(m,Zero) = m | add(m,Succ(n)) = Succ(add(m,n)) fun fib(Zero) = Succ(Zero) | fib(Succ(Zero)) = Succ(Zero) | fib(Succ(Succ(n))) = add(fib(Succ(n), fib(n))

Encoding a Datatype datatype nat = Zero | Succ of nat abstract class Nat { } class Zero extends Nat { Zero(){ /* nop */ } } class Succ extends Nat { final Nat n; Nat(Nat n) { this.n = n; } }

Creating a Value val one = Succ(Zero) final Nat one = new Succ (new Zero ());

Creating a Value val one = Succ(Zero) import static Nat.*; final Nat one = Succ (Zero); abstract class Nat { static Nat Zero = new Zero(); static Nat Succ(Nat n) { return new Succ(n); }

The General Pattern When encoding a type of the form t = A + B + C + D Create an abstract class of type “t” and have each case inherit from the abstract base class

Encoding a Function Several ways to do this Will try obvious pure OO way first Later approach using type casts Finally Visitor Pattern fun add(m,Zero) = m | add(m,Succ(n)) = Succ(add(m,n))

Encoding a Function Swap order of arguments so that add is inductively defined on its first argument Now think of add(n,m) as n.add(m) where n is either a Zero or Succ object Define methods appropriately fun add(Zero,m) = m | add(Succ(n),m) = Succ(add(n,m))

Encoding a Function fun add(Zero,m) = m | add(Succ(n),m) = Succ(add(n,m)) abstract class Nat { abstract Nat add(Nat n); } class Zero extends Nat { … Nat add(Nat m) { return m; } } class Succ extends Nat { … Nat add(Nat m) { return new Succ(this.n.add(m)); }

A General Pattern? Technique we described was not a general solution relies on having simple inductive definition of first argument How do we handle binary methods like “eq” fun eq(Zero,Zero) = true | eq(Succ(n),Succ(m)) = eq(n,m) | eq(_,_) = false

Double Dispatch fun eq(Zero,Zero) = true | eq(Succ(n),Succ(m)) = eq(n,m) | eq(_,_) = false abstract class Nat { … abstract boolean eq(Nat m); abstract boolean zeroEq(); abstract boolean succEq(Nat n); }

Double Dispatch fun eq(Zero,Zero) = true | eq(Zero,_) = false class Zero extends Nat { … boolean eq(Nat m){return m.zeroEq();} boolean zeroEq() {return true; } boolean succEq(Nat n) {return false;} }

Double Dispatch fun eq(Succ(n),Succ(m)) = eq(n,m) | eq(_,_) = false class Succ extends Nat { … boolean eq(Nat m){ return m.succEq(this.n); } boolean zeroEq() {return false; } boolean succEq(Nat n){ return n.eq(this.n); }

What about Fib? Encoding Fib not easy to encode for other reasons besides double dispatch! fun fib(Zero) = Succ(Zero) | fib(Succ(Zero)) = Succ(Zero) | fib(Succ(Succ(n))) = add(fib(Succ(n), fib(n))

What about Fib? fun fib(Zero) = Succ(Zero) | fib(Succ(Zero)) = Succ(Zero) | fib(Succ(Succ(n))) = add(fib(Succ(n), fib(n)) Nat fib(Nat n) { if(n instanceof Zero) { return new Succ(new Zero()); } Succ m = (Succ) n; … }

InstanceOf Java provides instanceof operator to test for class at runtime and dynamic type casts to cast appropiately Requires runtime type info (not available in C++) Error Prone and Hard to read

Reducing to Primitive Matching fun fib(Zero) = Succ(Zero) | fib(Succ(Zero)) = Succ(Zero) | fib(Succ(Succ(n))) = add(fib(Succ(n), fib(n)) fun fib(Zero) = Succ(Zero) | fib(Succ(m)) = fibSucc(m) and fibSucc(Zero) = Succ(Zero) | fibSucc(Succ(n)) = add(fib(Succ(n), fib(n))

Visitor Design Pattern abstract class Nat { abstract Nat accept(NatVisitor v); } interface NatVisitor { Nat vZero(); Nat vSucc(Nat n); }

Visitor Design Pattern class Zero extends Nat { … Nat accept(NatVisitor v) { return v.vZero(); } class Succ extends Nat { … final Nat n; Nat accept(NatVisitor v) { return v.vSucc(this.n); }

Visitor for Computing Fib class Fib implements NatVisitor { static Nat fib(Nat n) { return n.accept(new Fib()); } public vZero() { return new Succ(new Zero()); } public vSucc(Nat n) { return FibSucc.fibSucc(n); }

class FibSucc implements NatVisitor { static Nat fibSucc(Nat n) { return n.accept(new FibSucc()); } public vZero() { return new Succ(new Zero()); } public vSucc(Nat n) { return Fib.fib(new Succ(n)). add(Fib.fib(n)); }

Reducing to Primitive Matching fun fib(Zero) = Succ(Zero) | fib(Succ(Zero)) = Succ(Zero) | fib(Succ(Succ(n))) = add(fib(Succ(n), fib(n)) fun fib(Zero) = Succ(Zero) | fib(Succ(n)) = (case n of Zero => Succ(Zero) | Succ(m) => fib(n,m)) Alternate way that avoids extra function

Using An Anonymous Inner Class class Fib implements NatVisitor { … public vSucc(final Nat n) { return n.accept(new NatVisitor() { public vZero { return …;} public vSucc(Nat m) { return fib(n,m); } ); }

A Generic Visitor abstract class Nat { abstract accept(Visitor v); } interface Visitor { T vZero(); T vSucc(Nat n); }

How Painful Is This? Lines of SML Lines of Java Java/ SML Name Library MinML Syntax Static Semantics Dynamic Semantics Shape Library Ratio is ~2.5 Lines of Java for Every Line of SML

How Fast Is This? 20 Loops of fib(25) SMLJava Native0.25 sec0.12 sec MinML Interp33 sec60 sec Java is about 2x slower but our scheme is 4x more expensive when compare to SML/NJ

Dimensions of Feature Extensions AddMulFactFib… Zero…………… Succ…………… Extensible number of functions or attributes Fixed set of Objects

Dimensions of Feature Extensions WeightDesnsityCost Bricks……… Stone……… Wood……… Glass……… … Fixed set of attributes or functions Extensible number kinds of objects

I Want Both We have been extending MinML interpreter in both dimensions! Different operational semantics M-Machine, C-Machine, E-Machine, … Different expressions and types Pairs, Sums, Recursive, Polymorphic, …

Language Design Challenge A good language should support extensions in both dimensions ML clunky in the object extension direction Java is clunky in the functionality extension dimension New languages being designed as we speak to support both well –Nice, Scala, MultiJava, …

An Old Saying A FORTRAN programmer can program FORTRAN in any language! i.e. Program in any language as if it were FORTRAN!

Summary A HOT programmer can program HOTly in any language! i.e. Never let your programming language stop you from being too clever!