the great type hope
play

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


  1. The Great Type Hope Philip Wadler, Avaya Labs wadler@avaya.com

  2. Part I A logical coincidence

  3. Coincidences

  4. Coincidences Curry-Howard Hindley-Milner Girard-Reynolds

  5. Simply typed lambda calculus Id x 1 : A 1 , . . . , x n : A n ⊢ x i : A i Γ , x : A ⊢ u : B Γ ⊢ s : A → B Γ ⊢ t : A → -I → -E Γ ⊢ λx A . u : A → B Γ ⊢ s t : B

  6. Simply typed lambda calculus Id x 1 : A 1 , . . . , x n : A n ⊢ x i : A i Γ , x : A ⊢ u : B Γ ⊢ s : A → B Γ ⊢ t : A → -I → -E Γ ⊢ λx A . u : A → B Γ ⊢ s t : B

  7. Polymorphic lambda calculus Id x 1 : A 1 , . . . , x n : A n ⊢ x i : A i Γ , x : A ⊢ u : B Γ ⊢ s : A → B Γ ⊢ t : A → -I → -E Γ ⊢ λx A . u : A → B Γ ⊢ s t : B Γ ⊢ s : ∀ X. B Γ ⊢ u : B ∀ 2 -E ∀ 2 -I ( X not free in Γ) Γ ⊢ s A : B [ X := A ] Γ ⊢ Λ X. u : ∀ X. B

  8. Polymorphic lambda calculus Id x 1 : A 1 , . . . , x n : A n ⊢ x i : A i Γ , x : A ⊢ u : B Γ ⊢ s : A → B Γ ⊢ t : A → -I → -E Γ ⊢ λx A . u : A → B Γ ⊢ s t : B Γ ⊢ s : ∀ X. B Γ ⊢ u : B ∀ 2 -E ∀ 2 -I ( X not free in Γ) Γ ⊢ s A : B [ X := A ] Γ ⊢ Λ X. u : ∀ X. B

  9. The Church numeral one Γ ⊢ s : X → X Γ ⊢ z : X → -E s : X → X, z : X ⊢ s z : X → -I s : X → X ⊢ λz X . s z : X → X → -I ⊢ λs X → X . λz X . s z : ( X → X ) → X → X ∀ 2 -I ⊢ Λ X. λs X → X . λz X . s z : ∀ X. ( X → X ) → X → X Γ ≡ s : X → X, z : X

  10. The Church numeral one Γ ⊢ s : X → X Γ ⊢ z : X → -E s : X → X, z : X ⊢ s z : X → -I s : X → X ⊢ λz X . s z : X → X → -I ⊢ λs X → X . λz X . s z : ( X → X ) → X → X ∀ 2 -I ⊢ Λ X. λs X → X . λz X . s z : ∀ X. ( X → X ) → X → X Γ ≡ s : X → X, z : X

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

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

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

  14. Sums Γ ⊢ t : A Γ ⊢ u : B +-I Γ ⊢ inl t : A + 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

  15. Sums Γ ⊢ t : A Γ ⊢ u : B +-I Γ ⊢ inl t : A + 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

  16. Sums Γ ⊢ t : A Γ ⊢ u : B +-I Γ ⊢ inl t : A + 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 Λ X. λj A → X . λk B → X . j t inl t ≡ Λ X. λj A → X . λk B → X . k u ≡ inr u s C ( λx A . t ) ( λy B . u ) case s of inl x → t ; inr y → u ≡

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

  18. The Curry-Howard homeomorphism LC'90

  19. Part II Typed Erlang

  20. Typed Erlang -deftype tree(A,B) = T when T = empty | {branch,A,B,T,T}. -type new() -> tree(0,0). new() -> empty.

  21. Inferred type new() -> A when empty <= A

  22. Simplified type new() -> empty

  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.

  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.

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

  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.

  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.

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

  29. Part III Details

  30. Syntax f, g function names c, d constructors X, Y, Z variables E ::= X expression | f ( E ) | c { E } | case E 0 of c 1 { X 1 } → E 1 ; · · · ; c n { X n } → E n ; X → E n +1 f 1 ( X 1 ) → E 1 ; · · · ; f n ( X n ) → E n ::= program prog

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

  32. Typing rules ( Var ) F ; A, X : U ; C ⊢ X : U F ; A ; C ⊢ E : U C � U ⊆ V ( Sub ) F ; A ; C ⊢ E : V F ; A ; C ⊢ E 1 : U 1 F ; A ; C ⊢ E n : U n . . . ( Multi ) F ; A ; C ⊢ E : U

  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 ( Call ) F ; A ; C ⊢ f ( E ) : V F, f : (( U ) → V when C ); X : U ; C ⊢ E : V FTV(( U ) → V when C ) = α ( Def ) F ; ∅ ; C ⊢ f ( X ) → E : ( ∀ α. ( U ) → V when C )

  34. Typing rules F ; A ; C ⊢ E : U ( Con ) F ; A ; C ⊢ c { E } : c { U } F ; A ; C ⊢ E 0 : c 1 { U 1 } | . . . | c n { U n } | U F ; A, X 1 : U 1 ; C ⊢ E 1 : V F ; A, X n : U n ; C ⊢ E n : V . . . F ; A, X : U ; C ⊢ E n +1 : V F ; A ; C ⊢ ( case E 0 of c 1 { X 1 } → E 1 ; . . . c n { X n } → E n ; X → E n +1 end ) : V ( Case )

  35. Constraint reduction P | U ⊆ V ⇒ P ⊆ V, U ⊆ V 0 ⊆ U ⇒ none 1 cs ⊆ 0 ⇒ fail 1 cs ⊆ c { U } | U ⇒ 1 ⊆ U, 1 cs ⊆ U if c / ∈ cs 1 cs ⊆ U otherwise 1 cs ⊆ 1 ds ⇒ none if ds ⊆ cs fail otherwise 1 cs ⊆ α ds ⇒ 1 cs ⊆ α ds if ds ⊆ cs fail otherwise

  36. Constraint reduction c { U } ⊆ 0 ⇒ fail ′ } | U ⇒ U ⊆ U ′ c { U } ⊆ c ′ { U if c = c ′ c { U } ⊆ U otherwise c { U } ⊆ 1 cs ⇒ none ∈ cs if c / fail otherwise ⇒ c { U } ⊆ α cs if c / c { U } ⊆ α cs ∈ cs fail otherwise U ⊆ α cs , α cs ⊆ V U ⊆ V, U ⊆ α cs , α cs ⊆ V ⇒

  37. Part IV A fly in the ointment

  38. And -datatype bool() = true | false. -type and(bool(),bool()) -> bool(). and(true,true) -> true; and(false,X) -> false; and(X,false) -> false.

  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.

  40. Part V A simpler approach?

  41. Typed Erlang, simplified -deftype tree(A,B) = empty | {branch,A,B,T,T}. -type new() -> tree(A,B). new() -> empty.

  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.

  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.

  44. Part VI A simpler but more powerful approach?

  45. Types and logic s ∈ A → B ≡ ∀ x. x ∈ A → s x ∈ B

  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.

  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.

  48. Part VII Conclusions

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

  50. Conclusions Types are good Erlang is good Typed Erlang could be better Long live λ calculus!

  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 .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend