type based reasoning about efficiency
play

Type-Based Reasoning about Efficiency D. Seidel J. Voigtl ander - PowerPoint PPT Presentation

Type-Based Reasoning about Efficiency D. Seidel J. Voigtl ander University of Bonn August 3rd, 2011 Supported by the DFG under grant VO 1512/1-1. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a :


  1. Type-Based Reasoning about Efficiency D. Seidel ∗ J. Voigtl¨ ander University of Bonn August 3rd, 2011 ∗ Supported by the DFG under grant VO 1512/1-1.

  2. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) 1

  3. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] 1

  4. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not 1

  5. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not map even [1 , 2 , 3] = [False , True , False] 1

  6. Parametric Polymorphism in Haskell A standard function: map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not map even [1 , 2 , 3] = [False , True , False] map not [1 , 2 , 3] 1

  7. Parametric Polymorphism in Haskell A standard function: map :: ( α → β ) → [ α ] → [ β ] map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not map even [1 , 2 , 3] = [False , True , False] map not [1 , 2 , 3] 1

  8. Parametric Polymorphism in Haskell A standard function: map :: ( α → β ) → [ α ] → [ β ] map g [ ] = [ ] map g ( a : as ) = ( g a ) : ( map g as ) Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not map even [1 , 2 , 3] = [False , True , False] map not [1 , 2 , 3] � rejected at compile-time 1

  9. Parametric Polymorphism in Haskell A standard function: map :: ( α → β ) → [ α ] → [ β ] Some invocations: map succ [1 , 2 , 3] = [2 , 3 , 4] [True , False] = [False , True] map not map even [1 , 2 , 3] = [False , True , False] map not [1 , 2 , 3] � rejected at compile-time 1

  10. Another Example reverse :: [ α ] → [ α ] reverse [ ] = [ ] reverse ( a : as ) = ( reverse as ) + + [ a ] 2

  11. Another Example reverse :: [ α ] → [ α ] reverse [ ] = [ ] reverse ( a : as ) = ( reverse as ) + + [ a ] For every choice of g and l : reverse ( map g l ) = map g ( reverse l ) Provable by induction. 2

  12. Another Example reverse :: [ α ] → [ α ] reverse [ ] = [ ] reverse ( a : as ) = ( reverse as ) + + [ a ] For every choice of g and l : reverse ( map g l ) = map g ( reverse l ) Provable by induction. Or as a “free theorem” [Wadler, FPCA’89]. 2

  13. Another Example reverse :: [ α ] → [ α ] For every choice of g and l : reverse ( map g l ) = map g ( reverse l ) Provable by induction. Or as a “free theorem” [Wadler, FPCA’89]. 2

  14. Another Example reverse :: [ α ] → [ α ] tail :: [ α ] → [ α ] For every choice of g and l : reverse ( map g l ) = map g ( reverse l ) tail ( map g l ) = map g ( tail l ) 2

  15. Another Example reverse :: [ α ] → [ α ] tail :: [ α ] → [ α ] f :: [ α ] → [ α ] For every choice of g and l : reverse ( map g l ) = map g ( reverse l ) tail ( map g l ) = map g ( tail l ) f ( map g l ) = map g ( f l ) 2

  16. Automatic Generation of Free Theorems At http://www-ps.iai.uni-bonn.de/ft : 3

  17. Automatic Generation of Free Theorems 3

  18. Applications ◮ Short Cut Fusion [Gill et al., FPCA’93] 4

  19. Applications ◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion [Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02] ◮ . . . 4

  20. Applications ◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion [Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02] ◮ . . . ◮ Knuth’s 0-1-principle and the like [Day et al., Haskell’99], [V., POPL’08] 4

  21. Applications ◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion [Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02] ◮ . . . ◮ Knuth’s 0-1-principle and the like [Day et al., Haskell’99], [V., POPL’08] ◮ Bidirectionalization [V., POPL’09] 4

  22. Applications ◮ Short Cut Fusion [Gill et al., FPCA’93] ◮ The Dual of Short Cut Fusion [Takano & Meijer, FPCA’95], [Svenningsson, ICFP’02] ◮ . . . ◮ Knuth’s 0-1-principle and the like [Day et al., Haskell’99], [V., POPL’08] ◮ Bidirectionalization [V., POPL’09] ◮ . . . ◮ Testing polymorphic properties [Bernardy et al., ESOP’10] 4

  23. What About Efficiency? f :: α → Nat f :: α → α → α f :: α → ( α, α ) f ( g x ) f ( g x ) f ( g x ) ( g y ) = = = let y = f x f x g ( f x y ) in ( g ( fst y ) , g ( snd y )) 5

  24. What About Efficiency? f :: α → Nat f :: α → α → α f :: α → ( α, α ) f ( g x ) f ( g x ) f ( g x ) ( g y ) = = = let y = f x f x g ( f x y ) in ( g ( fst y ) , g ( snd y )) call-by- > > < value 5

  25. What About Efficiency? f :: α → Nat f :: α → α → α f :: α → ( α, α ) f ( g x ) f ( g x ) f ( g x ) ( g y ) = = = let y = f x f x g ( f x y ) in ( g ( fst y ) , g ( snd y )) call-by- > > < value call-by- < (?) = = name 5

  26. What About Efficiency? f :: α → Nat f :: α → α → α f :: α → ( α, α ) f ( g x ) f ( g x ) f ( g x ) ( g y ) = = = let y = f x f x g ( f x y ) in ( g ( fst y ) , g ( snd y )) call-by- > > < value call-by- < (?) = = name call-by- < = = need 5

  27. What About Efficiency? f :: α → Nat f :: α → α → α f :: α → ( α, α ) f ( g x ) f ( g x ) f ( g x ) ( g y ) = = = let y = f x f x g ( f x y ) in ( g ( fst y ) , g ( snd y )) call-by- > > < value call-by- < (?) = = name call-by- < = = need 5

  28. What About Efficiency? Now, how about, say f :: α → α → ( α, α ) ? 6

  29. What About Efficiency? Now, how about, say f :: α → α → ( α, α ) ? Or f :: [ α ] → [ α ] ? 6

  30. What About Efficiency? Now, how about, say f :: α → α → ( α, α ) ? Or f :: [ α ] → [ α ] ? And how to actually establish such statements? 6

  31. But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f ( g x ) = f x for all choices of types τ 1 , τ 2 , function g :: τ 1 → τ 2 , and x :: τ 1 . 7

  32. But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f ( g x ) = f x for all choices of types τ 1 , τ 2 , function g :: τ 1 → τ 2 , and x :: τ 1 . Clearly, this means that f is a constant function, i.e., for all x and y : f y = f x 7

  33. But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f ( g x ) = f x for all choices of types τ 1 , τ 2 , function g :: τ 1 → τ 2 , and x :: τ 1 . Clearly, this means that f is a constant function, i.e., for all x and y : f y = f x So, “obviously”, f ( g x ) ⊒ f x where ⊒ means “the same result, and equally fast or slower”. 7

  34. But isn’t it Somehow Trivial? Recall f :: α → Nat, with standard free theorem: f ( g x ) = f x for all choices of types τ 1 , τ 2 , function g :: τ 1 → τ 2 , and x :: τ 1 . Clearly, this means that f is a constant function, i.e., for all x and y : f y = f x So, “obviously”, f ( g x ) ⊒ f x where ⊒ means “the same result, and equally fast or slower”. What’s wrong with this reasoning? 7

  35. But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f ( x − 1) 8

  36. But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f ( x − 1) Certainly, for all x and y : f y = f x 8

  37. But isn’t it Somehow Trivial? Consider: f x = if x == 0 g x = 0 then 0 else f ( x − 1) Certainly, for all x and y : f y = f x But certainly not, for x > 0: f ( g x ) ⊒ f x 8

  38. But isn’t it Somehow Trivial? Consider: f :: Nat → Nat g :: α → Nat f x = if x == 0 g x = 0 then 0 else f ( x − 1) Certainly, for all x and y : f y = f x But certainly not, for x > 0: f ( g x ) ⊒ f x 8

  39. But isn’t it Somehow Trivial? Consider: f :: Nat → Nat g :: α → Nat f x = if x == 0 g x = 0 then 0 else f ( x − 1) Certainly, for all x and y : f y = f x But certainly not, for x > 0: f ( g x ) ⊒ f x Exploiting polymorphism is really essential, and not just for the extensional statements! 8

  40. Free Theorems, Formally — In a Nutshell Syntax for a typed λ -calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λ x :: τ. t | t t | . . . 9

  41. Free Theorems, Formally — In a Nutshell Syntax for a typed λ -calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λ x :: τ. t | t t | . . . Semantics: � α � θ = θ ( α ) � Nat � θ = N � τ 1 � θ � τ 1 → τ 2 � θ = � τ 2 � θ ∈ Set TVar θ 9

  42. Free Theorems, Formally — In a Nutshell Syntax for a typed λ -calculus: τ ::= α | Nat | τ → τ | . . . t ::= x | n | t + t | λ x :: τ. t | t t | . . . Semantics: � α � θ = θ ( α ) � x � σ = σ ( x ) � Nat � θ = N � n � σ = n � τ 1 � θ � τ 1 → τ 2 � θ = � τ 2 � θ � λ x :: τ. t � σ = λ v . � t � σ [ x �→ v ] ∈ Set TVar θ � t 1 t 2 � σ = � t 1 � σ � t 2 � σ 9

Recommend


More recommend