1 / 21

THE WORKER / WRAPPER TRANSFORMATION

THE WORKER / WRAPPER TRANSFORMATION. Graham Hutton and Andy Gill. What Is It?. A technique for changing the type of a program in order to improve its performance :. wrapper. program. worker. This Talk. Technique has been used by compiler writers for many years, e.g. GHC since 1991;

brook
Download Presentation

THE WORKER / WRAPPER TRANSFORMATION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill

  2. What Is It? A technique for changing the type of a program in order to improve its performance: wrapper program worker

  3. This Talk • Technique has been used by compiler writers for many years, e.g. GHC since 1991; • But is little known in the wider community, and has never been described precisely; • We explain, formalise and explore the generality of the worker/wrapper transformation.

  4. Fixed Points The key to formalising the technique is the use of explicit fixed points. For example: ones = 1 : ones can be rewritten as: ones = fix body body xs = 1 : xs fix f = f (fix f)

  5. The Problem Suppose we wish to change the type of a recursive program, defined by prog = fix body. B A Type of the desired worker. Type of the original program.

  6. Assumptions We assume conversion functions unwrap A B wrap such that: A can be faithfully represented by B. wrap . unwrap = idA

  7. Let’s Calculate! prog = fix body = fix (idA . body) Rolling rule. = fix (wrap . unwrap . body) = wrap (fix (unwrap . body . wrap)) = wrap work 6

  8. Summary We have derived the following factorisation: prog wrap work = Recursive program of type A. Wrapper of type B  A. Recursive worker of type B.

  9. The Final Step We simplify work = fix (unwrap . body . wrap) to eliminate the overhead of repeatedly converting between the two types, by fusing together and unwrap wrap

  10. The Worker / Wrapper Recipe • Express the original program using fix; • Choose the new type for the program; • Define appropriate conversion functions; • Apply the worker/wrapper transformation; • Simplify the resulting definitions.

  11. Example - Reverse How can we improve: Quadratic time. rev [] = [] rev (x:xs) = rev xs ++ [x] Step 1 - express the program using fix rev = fix body body f [] = [] body f (x:xs) = f xs ++ [x]

  12. Step 2 - choose a new type for the program rep [a] [a]  [a] Key idea (Hughes): represent the result list as a function. where abs rep xs = (xs ++) abs f = f []

  13. Step 3 – define conversion functions unwrap [a]  [a] [a]  [a]  [a] wrap where Satisfies the worker/wrapper assumption. unwrap f = rep . f wrap g = abs . g

  14. Step 4 – apply the transformation rev = wrap work work = fix (unwrap . body . wrap) Step 5 – simplify the result rev :: [a]  [a] rev xs = work xs [] Expanding out wrap.

  15. Using properties of rep and Worker/wrapper fusion property. unwrap (wrap work) = work we obtain a linear time worker: work :: [a]  [a]  [a] work [] ys = ys work (x:xs) ys = work xs (x:ys)

  16. Notes • Once the decision to use Hughes lists is made, the derivation itself is straightforward; • No induction is required, other than the implicit use to verify that lists form a monoid; • Fast reverse fits naturally into our paradigm, but simpler derivations are of course possible.

  17. Example - Unboxing Int  Int Int♯ Int♯ More efficient worker that uses unboxed integers. Type of a simple factorial function. Note: this is how GHC uses worker/wrapper.

  18. Example - Memoisation Nat  Nat Stream Nat More efficient worker that uses a memo table. Type of a simple Fibonacci function.

  19. Example - Continuations Expr  (Int  Mint)  Mint  Mint Expr  Mint More efficient worker that uses success and failure continuations. Type of a simple evaluation function that may fail, where Mint = Maybe Int.

  20. Summary • General technique for changing the type of a program to improve its performance; • Straightforward to understand/apply, requiring only basic equational reasoning principles; • Captures many seemingly unrelated optimisation methods in a single unified framework.

  21. Further Work • Mechanising the technique; • Specialised patterns of recursion; • Generalisation using category theory; • Programs with effects; • Other application areas.

More Related