From Frege to Gosling: 19th Century Logic and 21st Century - - PowerPoint PPT Presentation

from frege to gosling 19 th century logic and 21 st
SMART_READER_LITE
LIVE PREVIEW

From Frege to Gosling: 19th Century Logic and 21st Century - - PowerPoint PPT Presentation

From Frege to Gosling: 19th Century Logic and 21st Century Programming Languages Philip Wadler Avaya Labs Gottlob Frege, Begriffsschrift , 1879 Part I The Curry-Howard Isomorphism Modus ponens Frege, 1879 Gentzen, 1934 B A


slide-1
SLIDE 1

From Frege to Gosling: 19’th Century Logic and 21’st Century Programming Languages

Philip Wadler Avaya Labs

slide-2
SLIDE 2

Gottlob Frege, Begriffsschrift, 1879

slide-3
SLIDE 3

Part I

The Curry-Howard Isomorphism

slide-4
SLIDE 4

Modus ponens Frege, 1879

Gentzen, 1934 ⊢ B → A ⊢ B →-I ⊢ A

slide-5
SLIDE 5

Inference rules

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λx:A. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s(t) : B Γ ⊢ u : B ∀-I (X ∈ Γ) Γ ⊢ ΛX. u : ∀X. B Γ ⊢ s : ∀X. B ∀-E Γ ⊢ sA : B[A/X]

slide-6
SLIDE 6

Inference rules

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λx:A. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s(t) : B Γ ⊢ u : B ∀-I (X ∈ Γ) Γ ⊢ ΛX. u : ∀X. B Γ ⊢ s : ∀X. B ∀-E Γ ⊢ sA : B[A/X]

slide-7
SLIDE 7

A proof

Id f : A → X ⊢ f : A → X Γ ⊢ t : A →-E Γ, f : A → X ⊢ f(t) : X →-I Γ ⊢ λf:A → X. f(t) : (A → X) → X ∀-I Γ ⊢ ΛX. λf:A → X. f(t) : ∀X. (A → X) → X

slide-8
SLIDE 8

A proof

Id f : A → X ⊢ f : A → X Γ ⊢ t : A →-E Γ, f : A → X ⊢ f(t) : X →-I Γ ⊢ λf:A → X. f(t) : (A → X) → X ∀-I Γ ⊢ ΛX. λf:A → X. f(t) : ∀X. (A → X) → X

slide-9
SLIDE 9

Another proof

Γ ⊢ s : ∀X. (A → X) → X ∀-E Γ ⊢ sA : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ sA(λx:A. x) : A

slide-10
SLIDE 10

Another proof

Γ ⊢ s : ∀X. (A → X) → X ∀-E Γ ⊢ sA : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ sA(λx:A. x) : A

slide-11
SLIDE 11

A combined proof

Id f : A → X ⊢ f : A → X Γ ⊢ t : A →-E Γ, f : A → X ⊢ f(t) : X →-I Γ ⊢ λf:A → X. f(t) : (A → X) → X ∀-I Γ ⊢ ΛX. λf:A → X. f(t) : ∀X. (A → X) → X ∀-E Γ ⊢ (ΛX. λf:A → X. f(t))A : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ (ΛX. λf:A → X. f(t))A(λx:A. x) : A

slide-12
SLIDE 12

A combined proof

Id f : A → X ⊢ f : A → X Γ ⊢ t : A →-E Γ, f : A → X ⊢ f(t) : X →-I Γ ⊢ λf:A → X. f(t) : (A → X) → X ∀-I Γ ⊢ ΛX. λf:A → X. f(t) : ∀X. (A → X) → X ∀-E Γ ⊢ (ΛX. λf:A → X. f(t))A : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ (ΛX. λf:A → X. f(t))A(λx:A. x) : A

slide-13
SLIDE 13

Reductions

Γ, x : A ⊢ u : B →-I Γ ⊢ λx:A. u : A → B Γ ⊢ t : A →-E ⇒ Γ ⊢ u[t/x] : B Γ ⊢ (λx:A. u)(t) : B Γ ⊢ u : B ∀-I Γ ⊢ ΛX. u : ∀X. B ∀-E ⇒ Γ ⊢ u[A/X] : B[A/X] Γ ⊢ (ΛX. u)A : B[A/X]

slide-14
SLIDE 14

Reductions

Γ, x : A ⊢ u : B →-I Γ ⊢ λx:A. u : A → B Γ ⊢ t : A →-E ⇒ Γ ⊢ u[t/x] : B Γ ⊢ (λx:A. u)(t) : B Γ ⊢ u : B ∀-I Γ ⊢ ΛX. u : ∀X. B ∀-E ⇒ Γ ⊢ u[A/X] : B[A/X] Γ ⊢ (ΛX. u)A : B[A/X]

slide-15
SLIDE 15

Simplifying a proof

Id f : A → X ⊢ f : A → X Γ ⊢ t : A →-E Γ, f : A → X ⊢ f(t) : X →-I Γ ⊢ λf:A → X. f(t) : (A → X) → X ∀-I Γ ⊢ ΛX. λf:A → X. f(t) : ∀X. (A → X) → X ∀-E Γ ⊢ (ΛX. λf:A → X. f(t))A : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ (ΛX. λf:A → X. f(t))A(λx:A. x) : A

slide-16
SLIDE 16

Simplifying a proof

Id f : A → A ⊢ f : A → A Γ ⊢ t : A →-E Γ, f : A → A ⊢ f(t) : A →-I Γ ⊢ λf:A → X. f(t) : (A → A) → A Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A →-E Γ ⊢ (λf:A → A. f(t))(λx:A. x) : A

slide-17
SLIDE 17

Simplifying a proof

Id x : A ⊢ x : A →-I ⊢ λx:A. x : A → A Γ ⊢ t : A →-E Γ ⊢ (λx:A. x)(t) : A

slide-18
SLIDE 18

Simplifying a proof

Γ ⊢ t : A

slide-19
SLIDE 19

Additional reductions

Γ ⊢ s : A → B Id x : A ⊢ x : A →-E Γ, x : A ⊢ s(x) : B →-I ⇒ Γ ⊢ s : A → B Γ ⊢ λx:A. s(x) : A → B Γ ⊢ s : ∀X. B ∀-E Γ ⊢ sX : B ∀-I ⇒ Γ ⊢ s : ∀X. B Γ ⊢ ΛX. sX : ∀X. B

slide-20
SLIDE 20

Summary

  • Gottlob Frege, 1879

logic

  • Alonzo Church, 1932, 1940

lambda calculus

  • Gerhard Gentzen, 1935

natural deduction, proof normalization

  • Haskell Curry, 1958

combinators correspond to logic

  • Dag Prawitz, 1965

proof normalization for natural deduction

  • W. A. Howard, 1980

lambda calculus corresponds to logic

slide-21
SLIDE 21

Part II

Parametricity

slide-22
SLIDE 22

Lists and map

map : ∀X. ∀Y. (X → Y ) → (listX → listY )

For example, if num : Int → Str then

mapIntStr(num)([1, 2, 3]) = ["i", "ii", "iii"]

slide-23
SLIDE 23

A magic trick

Think of a function with this type: r : ∀X. listX → listX

slide-24
SLIDE 24

Theorems for Free!

for all r : ∀X. listX → listX, for all A, B, f : A → B,

listA listA listB listB

rA rB

mapAB(f) mapAB(f)

slide-25
SLIDE 25

Theorems for Free! — an example

take r = rev : ∀X. listX → listX, take A = Int, B = Str, f = num : Int → Str,

listInt listInt listStr listStr revInt revStr mapIntStr(num) mapIntStr(num)

[1, 2, 3] [3, 2, 1] ["i", "ii", "iii"] ["iii", "ii", "i"]

slide-26
SLIDE 26

Part III

Generic Java

Gilad Bracha, JavaSoft, Sun Microsystems Martin Odersky, University of Lausanne David Stoutamire, JavaSoft, Sun Microsystems Philip Wadler, Avaya Labs

slide-27
SLIDE 27

Lists in Java and GJ

in Java in GJ integer list List List<Integer> string list List List<String> string list list List List<List<String>>

slide-28
SLIDE 28

Lists in Java and GJ

in Java in GJ integer list List List<Integer> string list List List<String> string list list List List<List<String>>

slide-29
SLIDE 29

Lists in Java and GJ

in Java in GJ integer list List List<Integer> string list List List<String> string list list List List<List<String>>

slide-30
SLIDE 30

Lists in Java and GJ

in Java in GJ integer list List List<Integer> string list List List<String> string list list List List<List<String>>

slide-31
SLIDE 31

Lists in Java and GJ

in Java in GJ integer list List List<Integer> string list List List<String> string list list List List<List<String>>

slide-32
SLIDE 32

Support for reuse at multiple types

  • C++: templates
  • Ada: generics
  • Standard ML, Haskell: parametric polymorphism
  • Java: ???
slide-33
SLIDE 33

Lists in Java

interface List { public void add (Object x); public Iterator iterator (); } interface Iterator { public Object next (); public boolean hasNext (); }

slide-34
SLIDE 34

Lists in GJ

interface List<A> { public void add (A x); public Iterator<A> iterator (); } interface Iterator<A> { public A next (); public boolean hasNext (); }

slide-35
SLIDE 35

Translating lists from GJ to Java

interface List { public void add (Object x); public Iterator iterator (); } interface Iterator { public Object next (); public boolean hasNext (); }

slide-36
SLIDE 36

Accessing lists in Java

// integer list List xs = new LinkedList(); xs.add(new Integer(2)); Integer x = (Integer)xs.iterator().next(); space // string list list List ys = new LinkedList(); ys.add("ii"); List yss = new LinkedList(); yss.add(ys); String y = (String)((List)yss.iterator().next()).iterator().next(); space // string list treated as integer list Integer z = (Integer)ys.iterator().next(); // run-time exception

slide-37
SLIDE 37

Accessing lists in GJ

// integer list List<Integer> xs = new LinkedList<Integer>(); xs.add(new Integer(2)); Integer x = xs.iterator().next(); space // string list list List<String> ys = new LinkedList<String>(); ys.add("ii"); List<List<String>> yss = new LinkedList<List<String>>(); yss.add(ys); String y = yss.iterator().next().iterator().next(); space // string list treated as integer list Integer z = ys.iterator().next(); // compile-time error

slide-38
SLIDE 38

Implementing lists in Java

class LinkedList implements List { protected class Node { Object elt; Node next; Node (Object e) { elt=e; next=null; } } protected Node h, t; public LinkedList () { h=new Node(null); t=h; } public void add (Object elt) { t.next=new Node(elt); t=t.next; } public Iterator iterator () { return new Iterator () { protected Node p=h.next; public boolean hasNext () { return p!=null; } public Object next () { Object e=p.elt; p=p.next; return e; } }; } }

slide-39
SLIDE 39

Implementing lists in GJ

class LinkedList<A> implements List<A> { protected class Node { A elt; Node next; Node (A e) { elt=e; next=null; } } protected Node h, t; public LinkedList () { h=new Node(null); t=h; } public void add (A elt) { t.next=new Node(elt); t=t.next; } public Iterator<A> iterator () { return new Iterator<A> () { protected Node p=h.next; public boolean hasNext () { return p!=null; } public A next () { A e=p.elt; p=p.next; return e; } }; } }

slide-40
SLIDE 40

Bounds: Maximum in Java

interface Comparable { public int compareTo (Object o); } class Lists { public static Comparable max (List xs) { Iterator xi = xs.iterator(); Comparable w = (Comparable)xi.next(); while (xi.hasNext()) { Comparable x = (Comparable)xi.next(); if (w.compareTo(x) < 0) w = x; } return w; } } List xs = new LinkedList(); xs.add(new Byte(0)); Byte x = (Byte)Lists.max(xs); List ys = new LinkedList(); ys.add(new Boolean(false)); Boolean y = (Boolean)Lists.max(ys); // run-time exception

slide-41
SLIDE 41

Bounds: Maximum in GJ

interface Comparable<A> { public int compareTo (A o); } class Lists { public static <A implements Comparable<A>> A max (List<A> xs) { Iterator<A> xi = xs.iterator(); A w = xi.next(); while (xi.hasNext()) { A x = xi.next(); if (w.compareTo(x) < 0) w = x; } return w; } } List<Byte> xs = new LinkedList<Byte>(); xs.add(new Byte(0)); Byte x = Lists.max(xs); List<Boolean> ys = new LinkedList<Boolean>(); ys.add(new Boolean(false)); Boolean y = Lists.max(ys); // compile-time error

slide-42
SLIDE 42

Arrays

slide-43
SLIDE 43

Arrays and Subtyping

Covariant subtyping

Integer extends Object Integer[] extends Object[]

A good consequence

class Arrays { void sort (Object[] oa) { ... } } Integer[] ia = new Integer[n]; sort(ia) // simulates polymorphism

A bad consequence

Integer[] ia = new Integer[n]; Object[] oa = ia;

  • a[0] = "hello";

// run-time exception

slide-44
SLIDE 44

Arrays in Java

interface List { ... public Object[] toArray(); } class LinkedList implements List { ... public Object[] toArray() { Object[] xa = new Object[this.size()]; ... // copy list into xa return xa; } } List ys = new LinkedList(); ys.add("zero"); String[] ya = (String[])ys.toArray(); // run-time exception

slide-45
SLIDE 45

Arrays in GJ — the wrong way

interface List<A> { ... public A[] toArray(); } class LinkedList<A> implements List<A> { ... public A[] toArray() { A[] xa = new A[this.size()]; // compile-time unchecked warning ... // copy list into xa return xa; } } List<String> ys = new LinkedList<String>(); ys.add("zero"); String[] ya = ys.toArray(); // run-time exception

slide-46
SLIDE 46

Arrays in GJ — the right way

interface List<A> { ... public A[] toArray(A[] xa); } class LinkedList<A> implements List<A> { ... public A[] toArray(A[] xa) { if (xa.length < this.size()) xa = gj.lang.Array.newInstance(xa, this.size()); // no warning ... // copy list into xa return xa; } } List<String> ys = new LinkedList<String>(); ys.add("zero"); String[] ya = ys.toArray(new String[0]); // no exception

slide-47
SLIDE 47

Conclusion

slide-48
SLIDE 48

Generic Java

  • GJ supports generic types
  • GJ contains Java as a subset
  • GJ compiles to the Java Virtual Machine
  • GJ works with Java libraries
  • GJ compiler written by Martin Odersky

distributed by Sun as javac

slide-49
SLIDE 49

Higher-order functions in Java

Lambda calculus

λx:Int. x + 1 : Int → Int

ML

fn(x) => x+1 : int -> int

GJ

interface Function<A,B> { public B apply (A x); } new Function<Integer,Integer>() { public Integer apply (Integer x) { return new Integer(x.intValue()+1); } }

slide-50
SLIDE 50

GJ vs. C++

GJ C++ (erasure) (expansion) New GJ code works with old Java code

√ ×

Requirements on types explicit (bounds)

√ ×

Report errors at compile-time

√ ×

Avoid code bloat

√ ×

Easy to in-line operators

× √

Allow base types as type parameters

× √

slide-51
SLIDE 51

GJ is available from: www.research.avaya-labs.com/~wadler/gj/ Sun is adding generic types to Java: http://java.sun.com/people/gbracha/ generics-update.html

slide-52
SLIDE 52

Part IV

Featherweight Java

Benjamin Pierce, University of Pennsylvania Atsushi Igarashi, University of Kyoto Philip Wadler, Avya Labs

slide-53
SLIDE 53

Featherweight Java: Syntax

L ::= class C extends C {C f; K M} K ::= C(C f){super(f); this.f=f;} M ::= C m(C x){ return e; } e ::= x | e.f | e.m(e) | new C(e) | (C)e

slide-54
SLIDE 54

Typing rules

Γ ⊢ x : Γ(x) Γ ⊢ e0 : C0 fields(C0) = C f Γ ⊢ e0.fi : Ci Γ ⊢ e0 : C0 mtype(m, C0) = D→C Γ ⊢ e : C C <: D Γ ⊢ e0.m(e) : C fields(C) = D f Γ ⊢ e : C C <: D Γ ⊢ new C(e) : C Γ ⊢ e0 : D Γ ⊢ (C)e0 : C

slide-55
SLIDE 55

Reductions

fields(C) = C f (new C(e)).fi − → ei mbody(m, C) = (x, e0) (new C(e)).m(d) − → [d/x,new C(e)/this]e0 C <: D (D)(new C(e)) − → new C(e)

slide-56
SLIDE 56

Part V

Conclusions

slide-57
SLIDE 57

My career in a nutshell

  • Haskell
  • Java
  • XML
  • Logic
slide-58
SLIDE 58

My career in a nutshell

  • Theory into Practice

Generic Java

  • Practice into Theory

Featherweight Java

slide-59
SLIDE 59

My career in a nutshell

  • Theory into Practice

Generic Java

  • Practice into Theory

Featherweight Java

slide-60
SLIDE 60

My career in a nutshell

  • Theory into Practice

Generic Java

  • Practice into Theory

Featherweight Java