1 / 17

WORK IT, WRAP IT, FIX IT, FOLD IT

WORK IT, WRAP IT, FIX IT, FOLD IT. Graham Hutton and Neil Sculthorpe. This Talk. Worker/wrapper is a simple but powerful method for optimizing recursive programs; Previously, we developed theories for fix and fold , but with different correctness conditions;

lucita
Download Presentation

WORK IT, WRAP IT, FIX IT, FOLD IT

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. WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe

  2. This Talk • Worker/wrapper is a simple but powerful method for optimizing recursive programs; • Previously, we developed theories for fix and fold, but with different correctness conditions; • We combine and extend the two approaches to give a generalised worker/wrapper theory.

  3. Worker / Wrapper A technique for changing the type of a recursive program to improve its performance: wrapper program worker

  4. Fixed Points Our original formalisation was based on the use of explicit fixed points. For example: ones = 1 : ones can be rewritten as: ones = fix f f xs = 1 : xs fix f = f (fix f)

  5. The Problem Suppose we wish to change the type of a recursive program that is defined using fix. B A Type of the desired worker, fix g. Type of the original program, fix f.

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

  7. Let’s Calculate! fix f = fix (idA . f) = Rolling rule. fix (abs . rep . f) = abs (fix (rep . f . abs)) = abs (fix g) 6

  8. Summary We have derived the following factorisation: fix f abs fix g = Wrapper of type B  A. Recursive program of type A. Recursive worker of type B.

  9. Final Step We simplify the worker fix g = fix (rep . f. abs) to eliminate the overhead of repeatedly converting between the two types, by fusing together and rep abs

  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. Generalising The technique also works for weakerassumptions: abs . rep = idA From the ‘fix’ paper. abs . rep . f = f fix (abs . rep . f) = fix f

  12. … and for otherconditions relating f and g: g = rep . f . abs From the ‘fold’ paper. rep . f = g . rep abs . g = f . abs Necessary and sufficient. fix g = rep (fix f) fix g = fix (rep . f . abs)

  13. Generalised Recipe • If the worker is already given, we aim to verify that one of the conditions is satisfied; • Otherwise, our aim is to construct the worker, using one of the conditions as a specification; • Similar assumptions and conditions also give a generalised worker/wrapper theory for fold.

  14. Example last [] = ⊥ last [x] = x last (x:xs) = last xs last [] = ⊥ last (x:xs) = work xxs work x [] = x work x (y:ys) = work yys

  15. Example fib 0 = 0 fib 1 = 1 fib (n+2) = fib n + fib (n+1) fib n = fst (work n) work 0 = (0,1) work (n+1) = (y,x+y) where (x,y) = work n

  16. Summary • Generalised technique for changing the type of a program to improve its performance; • Captures a wide variety of program optimization techniques in a single unified framework; • The paper also presents a range of new results concerning strictness side conditions.

  17. Further Work • Other recursion patterns; • Time and space analysis; • Computational effects; • Implementing the technique.

More Related