Oopsla - Introduction
Oopsla - Introduction
Languages
Philip Wadler
University of Edinburgh
Evolution
Multiculturalism
Static vs. Dynamic Typing
Part I. Church: The origins of faith
A B A&B A&B
&-I &-E0 &-E1
A&B A B
Simplifying a proof
[B & A]z [B & A]z
&-E1 &-E0
A B
&-I
A&B [B]y [A]x
⊃-Iz &-I
(B & A) ⊃ (A & B) B&A
⊃-E
A&B
Simplifying a proof
[B & A]z [B & A]z
&-E1 &-E0
A B
&-I y x
A&B [B] [A]
⊃-Iz &-I
(B & A) ⊃ (A & B) B&A
⊃-E
A&B
⇓
[B]y [A]x [B]y [A]x
&-I &-I
B&A B&A
&-E1 &-E0
A B
&-I
A&B
Simplifying a proof
[B & A]z [B & A]z
&-E1 &-E0
A B
&-I y x
A&B [B] [A]
⊃-Iz &-I
(B & A) ⊃ (A & B) B&A
⊃-E
A&B
⇓
[B]y [A]x [B]y [A]x
&-I &-I
B&A B&A
&-E1 &-E0
A B
&-I
A&B
⇓
[A]x [B]y
&-I
A&B
Alonzo Church (1903–1995)
Alonzo Church (1932) — Lambda calculus
Alonzo Church (1940) — Typed λ-calculus
[x : A]x
·· s:A⊃B t:A
· ⊃-E
u:B st : B
⊃-Ix
λx. u : A ⊃ B
Hindley-Milner
Girard-Reynolds
Part II
test :: Bool
test = maximum [0,1,2] == 2 &&
maximum "abc" == ’c’
Translation
data Ord a = Ord { less :: a -> a -> Bool }
test :: Bool
test = maximum ordInt [0,1,2] == 2 &&
maximum ordChar "abc" == ’c’
Type classes, continued
instance Ord a => Ord [a] where
[] < [] = False
[] < y:ys = True
x:xs < [] = False
x:xs < y:ys | x < y = True
| y < x = False
| otherwise = xs < ys
test’ :: Bool
test’ = maximum ["zero","one","two"] == "zero" &&
maximum [[[0],[1]],[[0,1]]] == [[0,1]]
Translation, continued
ordList :: Ord a -> Ord [a]
ordList d = Ord { less = lt }
where
lt d [] [] = False
lt d [] (y:ys) = True
lt d (x:xs) [] = False
lt d (x:xs) (y:ys) | x < y = True
| y < x = False
| otherwise = lt d xs ys
test’ :: Bool
test’ = maximum d0 ["zero","one","two"] == "zero" &&
maximum d1 [[[0],[1]],[[0,1]]] == [[0,1]]
where
d0 = ordList ordChar
d1 = ordList (ordList ordInt)
Part III
Java: Generics
Java generics
Lists in Java 4.0 and Java 5.0
class Client {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(new Integer(42));
int top = ((Integer)stack.pop()).intValue();
assert top == 42;
}
}
Generic library with generic client
class Stack<E> {
private List<E> list;
public Stack() { list = new ArrayList<E>(); }
public boolean empty() { return list.size() == 0; }
public void push(E elt) { list.add(elt); }
public E pop() {
E elt = list.remove(list.size()-1);
return elt;
}
}
class Client {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(42);
int top = stack.pop();
assert top == 42;
}
}
Generic library with legacy client
class Stack<E> {
private List<E> list;
public Stack() { list = new ArrayList<E>(); }
public boolean empty() { return list.size() == 0; }
public void push(E elt) { list.add(elt); }
public E pop() {
E elt = list.remove(list.size()-1);
return elt;
}
}
class Client {
public static void main(String[] args) {
Stack stack = new Stack(); // raw type
stack.push(new Integer(42));
int top = ((Integer)stack.pop()).intValue();
assert top == 42;
}
}
Legacy library with generic client—minimal changes
class Stack<E> {
private List list; // raw type
public Stack() { list = new ArrayList(); }
public boolean empty() { return list.size() == 0; }
public void push(E elt) { list.add(elt); }
public E pop() {
Object elt = list.remove(list.size()-1);
return (E)elt; // unchecked cast
}
}
class Client {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(42);
int top = stack.pop();
assert top == 42;
}
}
Legacy library with generic client—stubs
class Stack<E> {
public Stack() { throw new StubException(); }
public boolean empty() { throw new StubException(); }
public void push(E elt) { throw new StubException(); }
public E pop() { throw new StubException(); }
}
class Client {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(42);
int top = stack.pop();
assert top == 42;
}
}
Integer int0 = 0;
Integer int1 = 1;
assert int0.compareTo(int1) < 0;
Number num0 = 0;
Number num1 = 1.0;
assert num0.compareTo(num1) < 0; // compile-time error
Maximum of a list
public static <T extends Comparable<T>>
T max(List<T> elts)
{
T candidate = elts.get(0);
for (T elt : elts) {
if (candidate.compareTo(elt) < 0) candidate = elt;
}
return candidate;
}
Links: Reconciliation
Links: Web Programming without Tiers
Browser
(HTML,XML,JavaScript)
6
form response
?
Server
(Java,Perl,PHP,Python)
6
query response
?
Database
(SQL,XQuery)
Links: Web Programming without Tiers
Browser
(Links)
6
call return
?
Server
(Links)
6
call return
?
Database
(Links)
Hope — Burstall, MacQueen, and Sannella (1980)
1 + 1 == 2 :: Bool
-- unambiguous!