kinds are calling conventions
play

Kinds Are Calling Conventions P a ul Downen, Zen a M. Ariol a , Simon - PowerPoint PPT Presentation

Kinds Are Calling Conventions P a ul Downen, Zen a M. Ariol a , Simon Peyton Jones, Rich a rd A. Eisenberg ICFP 2020, August 23 29 Ef f icient Function Calls P a r a meter P a ssing Techniques Ef f icient Function Calls P a r a meter P a ssing


  1. Kinds Are Calling Conventions P a ul Downen, Zen a M. Ariol a , Simon Peyton Jones, Rich a rd A. Eisenberg ICFP 2020, August 23 — 29

  2. Ef f icient Function Calls P a r a meter P a ssing Techniques

  3. Ef f icient Function Calls P a r a meter P a ssing Techniques • Representation — What & Where?

  4. Ef f icient Function Calls P a r a meter P a ssing Techniques • Representation — What & Where? • Arity — How many?

  5. Ef f icient Function Calls P a r a meter P a ssing Techniques • Representation — What & Where? • Arity — How many? • Levity (aka Evaluation Strategy) — When to compute?

  6. Ef f icient Function Calls P a r a meter P a ssing Techniques • Representation — What & Where? • Arity — How many? Arity • Levity (aka Evaluation Strategy) — When to compute?

  7. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2

  8. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x in y + z

  9. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x Arity 2 in y + z

  10. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 in y + z

  11. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 = \x -> \y -> f1 x y in y + z

  12. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z

  13. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x in \y -> y + z

  14. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x in \y -> y + z Hint: ‘ expensive x ’ may be costly, or even cause side e ff ects

  15. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x Arity 1 in \y -> y + z Hint: ‘ expensive x ’ may be costly, or even cause side e ff ects

  16. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x f4 = \x -> f3 x Arity 1 in \y -> y + z Hint: ‘ expensive x ’ may be costly, or even cause side e ff ects

  17. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x f4 = \x -> f3 x Arity 1 ≠ \x -> \y -> f3 x y in \y -> y + z Hint: ‘ expensive x ’ may be costly, or even cause side e ff ects

  18. Determining Function Arity f1, f2, f3, f4 :: Int -> Int -> Int Type suggests arity 2 f1 = \x -> \y -> let z = expensive x f2 = \x -> f1 x Arity 2 Arity 2 = \x -> \y -> f1 x y in y + z f3 = \x -> let z = expensive x f4 = \x -> f3 x Arity 1 Arity 1 ≠ \x -> \y -> f3 x y in \y -> y + z Hint: ‘ expensive x ’ may be costly, or even cause side e ff ects

  19. What Is Arity? For Curried Functions

  20. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.”

  21. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3

  22. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3 De fi nition 2 . The number of times a function may be soundly η -expanded.

  23. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3 De fi nition 2 . The number of times a function may be soundly η -expanded. • If ‘ f ’ is equivalent to ‘ \x y z -> f x y z ’, then ‘ f ’ has arity 3

  24. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3 De fi nition 2 . The number of times a function may be soundly η -expanded. • If ‘ f ’ is equivalent to ‘ \x y z -> f x y z ’, then ‘ f ’ has arity 3 De fi nition 3 . The number of arguments passed simultaneously to a function during one call.

  25. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3 De fi nition 2 . The number of times a function may be soundly η -expanded. • If ‘ f ’ is equivalent to ‘ \x y z -> f x y z ’, then ‘ f ’ has arity 3 De fi nition 3 . The number of arguments passed simultaneously to a function during one call. • If ‘ f ’ has arity 3, then ‘ f 1 2 3 ’ can be implemented as a single call

  26. What Is Arity? For Curried Functions De fi nition 1. The number of arguments a function needs before doing “serious work.” • If ‘ f 1 2 3 ’ does work, but ‘ f 1 2 ’ does not, then ‘ f ’ has arity 3 De fi nition 2 . The number of times a function may be soundly η -expanded. • If ‘ f ’ is equivalent to ‘ \x y z -> f x y z ’, then ‘ f ’ has arity 3 De fi nition 3 . The number of arguments passed simultaneously to a function during one call. • If ‘ f ’ has arity 3, then ‘ f 1 2 3 ’ can be implemented as a single call

  27. Goal : An IL with un restricted η for functions, along with restricted β for other types

  28. Static Arity In a n Intermedi a te L a ngu a ge

  29. Static Arity In a n Intermedi a te L a ngu a ge • New type of primitive functions (ASCII ‘ a ~> b ’) a ⇝ b • To distinguish from the source-level a → b with di ff erent semantics

  30. Static Arity In a n Intermedi a te L a ngu a ge • New type of primitive functions (ASCII ‘ a ~> b ’) a ⇝ b • To distinguish from the source-level a → b with di ff erent semantics • Primitive functions are fully extensional , un like source functions • λ x . f x = η f : a ⇝ b unconditionally

  31. Static Arity In a n Intermedi a te L a ngu a ge • New type of primitive functions (ASCII ‘ a ~> b ’) a ⇝ b • To distinguish from the source-level a → b with di ff erent semantics • Primitive functions are fully extensional , un like source functions • λ x . f x = η f : a ⇝ b unconditionally • Application may still be restricted for e ffi ciency, like source functions • ( λ x . x + x ) ( fact 10 6 ) fact 10 6 does not recompute

  32. Static Arity In a n Intermedi a te L a ngu a ge • New type of primitive functions (ASCII ‘ a ~> b ’) a ⇝ b • To distinguish from the source-level a → b with di ff erent semantics • Primitive functions are fully extensional , un like source functions • λ x . f x = η f : a ⇝ b unconditionally • Application may still be restricted for e ffi ciency, like source functions • ( λ x . x + x ) ( fact 10 6 ) fact 10 6 does not recompute • With full η , types express arity — just count the arrows • f : Int ⇝ Bool ⇝ String has arity 2, no matter ’s de fi nition f

  33. Currying When P a rti a l Applic a tion M a tters

  34. Currying When P a rti a l Applic a tion M a tters f3 :: Int ~> Int ~> Int 
 f3 = \x -> let z = expensive x in \y -> y + z

  35. Currying When P a rti a l Applic a tion M a tters f3 :: Int ~> Int ~> Int 
 f3 = \x -> let z = expensive x in \y -> y + z • Because of η , f3 now has arity 2, not 1!

  36. Currying When P a rti a l Applic a tion M a tters f3 :: Int ~> Int ~> Int 
 f3 = \x -> let z = expensive x in \y -> y + z • Because of η , f3 now has arity 2, not 1! • map (f3 100) [1..10^6] recomputes ‘expensive 100’ a million times ☹

  37. Currying When P a rti a l Applic a tion M a tters f3 :: Int ~> Int ~> Int 
 f3 = \x -> let z = expensive x in \y -> y + z • Because of η , f3 now has arity 2, not 1! • map (f3 100) [1..10^6] recomputes ‘expensive 100’ a million times ☹ f3’ :: Int ~> { Int ~> Int } 
 f3’ = \x -> let z = expensive x in Clos (\y -> y + z) Clos :: (Int ~> Int) ~> {Int ~> Int}

Recommend


More recommend