The Great Type Hope Philip Wadler, Avaya Labs wadler@avaya.com - - PowerPoint PPT Presentation

the great type hope
SMART_READER_LITE
LIVE PREVIEW

The Great Type Hope Philip Wadler, Avaya Labs wadler@avaya.com - - PowerPoint PPT Presentation

The Great Type Hope Philip Wadler, Avaya Labs wadler@avaya.com Part I A logical coincidence Coincidences Coincidences Curry-Howard Hindley-Milner Girard-Reynolds Simply typed lambda calculus Id x 1 : A 1 , . . . , x n : A n x i : A i


slide-1
SLIDE 1

The Great Type Hope

Philip Wadler, Avaya Labs wadler@avaya.com

slide-2
SLIDE 2

Part I

A logical coincidence

slide-3
SLIDE 3

Coincidences

slide-4
SLIDE 4

Coincidences Curry-Howard Hindley-Milner Girard-Reynolds

slide-5
SLIDE 5

Simply typed lambda calculus

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λxA. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s t : B

slide-6
SLIDE 6

Simply typed lambda calculus

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λxA. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s t : B

slide-7
SLIDE 7

Polymorphic lambda calculus

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λxA. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s t : B Γ ⊢ u : B ∀2-I (X not free in Γ) Γ ⊢ ΛX. u : ∀X. B Γ ⊢ s : ∀X. B ∀2-E Γ ⊢ s A : B[X := A]

slide-8
SLIDE 8

Polymorphic lambda calculus

Id x1 : A1, . . . , xn : An ⊢ xi : Ai Γ, x : A ⊢ u : B →-I Γ ⊢ λxA. u : A → B Γ ⊢ s : A → B Γ ⊢ t : A →-E Γ ⊢ s t : B Γ ⊢ u : B ∀2-I (X not free in Γ) Γ ⊢ ΛX. u : ∀X. B Γ ⊢ s : ∀X. B ∀2-E Γ ⊢ s A : B[X := A]

slide-9
SLIDE 9

The Church numeral one

Γ ⊢ s : X → X Γ ⊢ z : X →-E s : X → X, z : X ⊢ s z : X →-I s : X → X ⊢ λzX. s z : X → X →-I ⊢ λsX→X. λzX. s z : (X → X) → X → X ∀2-I ⊢ ΛX. λsX→X. λzX. s z : ∀X. (X → X) → X → X Γ ≡ s : X → X, z : X

slide-10
SLIDE 10

The Church numeral one

Γ ⊢ s : X → X Γ ⊢ z : X →-E s : X → X, z : X ⊢ s z : X →-I s : X → X ⊢ λzX. s z : X → X →-I ⊢ λsX→X. λzX. s z : (X → X) → X → X ∀2-I ⊢ ΛX. λsX→X. λzX. s z : ∀X. (X → X) → X → X Γ ≡ s : X → X, z : X

slide-11
SLIDE 11

Products

Γ ⊢ t : A Γ ⊢ u : B ×-I Γ ⊢ (t, u) : A × B Γ ⊢ s : A × B ×-E Γ ⊢ fst s : A Γ ⊢ s : A × B Γ ⊢ snd s : B

slide-12
SLIDE 12

Products

Γ ⊢ t : A Γ ⊢ u : B ×-I Γ ⊢ (t, u) : A × B Γ ⊢ s : A × B ×-E Γ ⊢ fst s : A Γ ⊢ s : A × B Γ ⊢ snd s : B

slide-13
SLIDE 13

Products

Γ ⊢ t : A Γ ⊢ u : B ×-I Γ ⊢ (t, u) : A × B Γ ⊢ s : A × B ×-E Γ ⊢ fst s : A Γ ⊢ s : A × B Γ ⊢ snd s : B A × B ≡ ∀X. (A → B → X) → X (t, u) ≡ ΛX. λkA→B→X. k t u fst s ≡ s A (λxA. λyB. x) snd s ≡ s A (λxA. λyB. x)

slide-14
SLIDE 14

Sums

Γ ⊢ t : A +-I Γ ⊢ inl t : A + B Γ ⊢ u : B Γ ⊢ inr u : A + B Γ ⊢ s : A + B Γ, x : A ⊢ t : C Γ ⊢ y : B ⊢ u : C +-E Γ ⊢ case t of inl x → u; inr y → v : C

slide-15
SLIDE 15

Sums

Γ ⊢ t : A +-I Γ ⊢ inl t : A + B Γ ⊢ u : B Γ ⊢ inr u : A + B Γ ⊢ s : A + B Γ, x : A ⊢ t : C Γ ⊢ y : B ⊢ u : C +-E Γ ⊢ case t of inl x → u; inr y → v : C

slide-16
SLIDE 16

Sums

Γ ⊢ t : A +-I Γ ⊢ inl t : A + B Γ ⊢ u : B Γ ⊢ inr u : A + B Γ ⊢ s : A + B Γ, x : A ⊢ t : C Γ ⊢ y : B ⊢ u : C +-E Γ ⊢ case t of inl x → u; inr y → v : C A + B ≡ ∀X. (A → X) → (B → X) → X inl t ≡ ΛX. λjA→X. λkB→X. j t inr u ≡ ΛX. λjA→X. λkB→X. k u case s of inl x → t; inr y → u ≡ s C (λxA. t) (λyB. u)

slide-17
SLIDE 17

The Triumph of Type ML Haskell Java XML/XQuery Erlang?

slide-18
SLIDE 18

The Curry-Howard homeomorphism

LC'90

slide-19
SLIDE 19

Part II

Typed Erlang

slide-20
SLIDE 20

Typed Erlang

  • deftype tree(A,B) =

T when T = empty | {branch,A,B,T,T}.

  • type new() -> tree(0,0).

new() -> empty.

slide-21
SLIDE 21

Inferred type

new() -> A when empty <= A

slide-22
SLIDE 22

Simplified type

new() -> empty

slide-23
SLIDE 23

Typed Erlang

  • type insert(A,B,tree(A,B)) -> tree(A,B).

insert(K0,V0,empty) -> {branch,K0,V0,empty,empty}; insert(K0,V0,{branch,K,V,L,R}) -> if K0 < K -> {branch,K,V,insert(K0,V0,L),R}; K0 == K -> {branch,K0,V0,L,R}; true

  • >

{branch,K,V,L,insert(K0,V0,R)} end.

slide-24
SLIDE 24

Inferred type

insert(B, C, D) -> A when branchE,F,G,A <= A; branchB,C,G,H <= A; branchE,F,A,H <= A; branchB,C,empty,empty <= A; D <= empty | branchE,F,G,H; G <= empty | branchE,F,G,H; H <= empty | branchE,F,G,H; H <= D; G <= D.

slide-25
SLIDE 25

Simplified type

insert(D, E, F) -> A when empty | branchD,E,A,A <= A; F <= empty | branchD,E,F,F.

slide-26
SLIDE 26

Typed Erlang

  • type lookup(A,tree(A,B)) -> B | error

when B \ error. lookup(K0,empty) -> error; lookup(K0,{branch,K,V,L,R}) -> if K0 < K -> lookup(K0,L); K0 == K -> V; true

  • > lookup(K0,R)

end.

slide-27
SLIDE 27

Inferred type

lookup(B, C) -> A when error <= A C <= empty | branchD,E,F,G; F <= empty | branchD,E,F,G; G <= empty | branchD,E,F,G; E <= A; F <= C; G <= C.

slide-28
SLIDE 28

Simplified type

lookup(1, B) -> error | A when B <= empty | branch1, error | A, B, B; A error.

slide-29
SLIDE 29

Part III

Details

slide-30
SLIDE 30

Syntax

f, g function names c, d constructors X, Y, Z variables E ::= X expression | f(E) | c{E} | case E0 of c1{X1} → E1; · · · ; cn{Xn} → En; X → En+1 prog ::= f1(X1) → E1; · · · ; fn(Xn) → En program

slide-31
SLIDE 31

Types

c, d constructors α, β type variables U, V ::= P | U union type | R P, Q ::= c{U} prime type R ::= αcs remainder | 1cs |

slide-32
SLIDE 32

Typing rules

F; A, X : U; C ⊢ X : U (Var) F; A; C ⊢ E : U C U ⊆ V F; A; C ⊢ E : V (Sub) F; A; C ⊢ E1 : U1 . . . F; A; C ⊢ En : Un F; A; C ⊢ E : U (Multi)

slide-33
SLIDE 33

Typing rules

F, f : ∀α.(U) → V when D; A; C, D[V /α] ⊢ f : ((U) → V )[V /α] (Fun) F; A; C ⊢ f : (U) → V F; A; C ⊢ E : U F; A; C ⊢ f(E) : V (Call) F, f : ((U) → V when C); X : U; C ⊢ E : V FTV((U) → V when C) = α F; ∅; C ⊢ f(X) → E : (∀α.(U) → V when C) (Def)

slide-34
SLIDE 34

Typing rules

F; A; C ⊢ E : U F; A; C ⊢ c{E} : c{U} (Con) F; A; C ⊢ E0 : c1{U1} | . . . | cn{Un} | U F; A, X1 : U1; C ⊢ E1 : V . . . F; A, Xn : Un; C ⊢ En : V F; A, X : U; C ⊢ En+1 : V F; A; C ⊢ (case E0 of c1{X1} → E1; . . . cn{Xn} → En; X → En+1 end) : V (Case)

slide-35
SLIDE 35

Constraint reduction

P | U ⊆ V ⇒ P ⊆ V, U ⊆ V 0 ⊆ U ⇒ none 1cs ⊆ 0 ⇒ fail 1cs ⊆ c{U} | U ⇒ 1 ⊆ U, 1cs ⊆ U if c / ∈ cs 1cs ⊆ U

  • therwise

1cs ⊆ 1ds ⇒ none if ds ⊆ cs fail

  • therwise

1cs ⊆ αds ⇒ 1cs ⊆ αds if ds ⊆ cs fail

  • therwise
slide-36
SLIDE 36

Constraint reduction

c{U} ⊆ 0 ⇒ fail c{U} ⊆ c′{U

′} | U ⇒ U ⊆ U ′

if c = c′ c{U} ⊆ U

  • therwise

c{U} ⊆ 1cs ⇒ none if c / ∈ cs fail

  • therwise

c{U} ⊆ αcs ⇒ c{U} ⊆ αcs if c / ∈ cs fail

  • therwise

U ⊆ αcs, αcs ⊆ V ⇒ U ⊆ V, U ⊆ αcs, αcs ⊆ V

slide-37
SLIDE 37

Part IV

A fly in the ointment

slide-38
SLIDE 38

And

  • datatype bool() = true | false.
  • type and(bool(),bool()) -> bool().

and(true,true) -> true; and(false,X)

  • > false;

and(X,false)

  • > false.
slide-39
SLIDE 39

Uh oh

  • type and(1,false) -> false | true.

and(X,Y) -> let Z = (case Y of false -> false end) in case X of true

  • >

case Y of true -> true; X -> Z end; false -> false; X -> Z end.

slide-40
SLIDE 40

Part V

A simpler approach?

slide-41
SLIDE 41

Typed Erlang, simplified

  • deftype tree(A,B) =

empty | {branch,A,B,T,T}.

  • type new() -> tree(A,B).

new() -> empty.

slide-42
SLIDE 42

Typed Erlang

  • type insert(A,B,tree(A,B)) -> tree(A,B).

insert(K0,V0,empty) -> {branch,K0,V0,empty,empty}; insert(K0,V0,{branch,K,V,L,R}) -> if K0 < K -> {branch,K,V,insert(K0,V0,L),R}; K0 == K -> {branch,K0,V0,L,R}; true

  • >

{branch,K,V,L,insert(K0,V0,R)} end.

slide-43
SLIDE 43

Typed Erlang

  • deftype sum(A,B) =

inl(A) | inr(B).

  • deftype error =

error

  • type lookup(A,tree(A,B)) -> inl(B) | inr(error)

lookup(K0,empty) -> inr(error); lookup(K0,{branch,K,V,L,R}) -> if K0 < K -> lookup(K0,L); K0 == K -> inl(V); true

  • > lookup(K0,R)

end.

slide-44
SLIDE 44

Part VI

A simpler but more powerful approach?

slide-45
SLIDE 45

Types and logic

s ∈ A → B ≡ ∀x. x ∈ A → s x ∈ B

slide-46
SLIDE 46

Retrofitting types

  • type lookup(A,tree(A,B)) -> B | error

when B \ error. lookup(K0,empty) -> error; lookup(K0,{branch,K,V,L,R}) -> if K0 < K -> lookup(K0,L); K0 == K -> V; true

  • > lookup(K0,R)

end.

slide-47
SLIDE 47

Retrofitting types

  • assert

K in A & T in tree(A,B) & V = lookup(K,T) & not (error in B)

  • > V in B \/ V in error.

lookup(K0,empty) -> error; lookup(K0,{branch,K,V,L,R}) -> if K0 < K -> lookup(K0,L); K0 == K -> V; true

  • > lookup(K0,R)

end.

slide-48
SLIDE 48

Part VII

Conclusions

slide-49
SLIDE 49

Conclusions Types are good Erlang is good Typed Erlang could be better

slide-50
SLIDE 50

Conclusions Types are good Erlang is good Typed Erlang could be better

Long live λ calculus!

slide-51
SLIDE 51

Further reading

Simon Marlow and Philip Wadler, A practical subtyping system for Erlang, 2’nd International Conference on Functional Programming, Amsterdam, June 1997. Philip Wadler, New Languages, Old Logic, Dr Dobbs Journal, special supplement on Software in the 21st century, December 2000. (See also, 19th century logic and 21st century computing, on my web page.) Philip Wadler, The Girard-Reynolds isomorphism, Theoretical Aspects of Computer Software Sendai, Japan, October 2001. Journal version to appear in Information and Computation.