Download presentation
Presentation is loading. Please wait.
Published byLuke Goodman Modified over 9 years ago
1
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe
2
1 This Talk zWorker/wrapper is a simple but powerful method for optimizing recursive programs; zPreviously, we developed theories for fix and fold, but with different correctness conditions; zWe combine and extend the two approaches to give a generalised worker/wrapper theory.
3
2 Worker / Wrapper program wrapper worker A technique for changing the type of a recursive program to improve its performance:
4
3 Fixed Points ones = 1 : ones ones = fix f f xs = 1 : xs can be rewritten as: fix f = f (fix f) Our original formalisation was based on the use of explicit fixed points. For example:
5
4 The Problem A Type of the desired worker, fix g. Type of the original program, fix f. B Suppose we wish to change the type of a recursive program that is defined using fix.
6
5 Assumptions We assume conversion functions A can be faithfully represented by B. such that: abs. rep = id A AB abs rep
7
6 Let’s Calculate! 6 fix f fix (abs. rep. f) = fix (id A. f) = abs (fix g) = abs (fix (rep. f. abs)) = Rolling rule.
8
7 Summary fix f We have derived the following factorisation: Wrapper of type B A. Recursive program of type A. abs = Recursive worker of type B. fix g
9
8 Final Step We simplify the worker fix (rep. f. abs) rep abs and to eliminate the overhead of repeatedly converting between the two types, by fusing together fix g =
10
9 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
10 Generalising The technique also works for weaker assumptions: From the ‘fix’ paper. abs. rep = id A abs. rep. f = f fix (abs. rep. f) = fix f
12
11 … and for other conditions relating f and g: g = rep. f. abs rep. f = g. rep abs. g = f. abs From the ‘fold’ paper. fix g = fix (rep. f. abs) fix g = rep (fix f) Necessary and sufficient.
13
12 Generalised Recipe zIf the worker is already given, we aim to verify that one of the conditions is satisfied; zOtherwise, our aim is to construct the worker, using one of the conditions as a specification; zSimilar assumptions and conditions also give a generalised worker/wrapper theory for fold.
14
13 Example last [] = ⊥ last [x] = x last (x:xs) = last xs last [] = ⊥ last (x:xs) = work x xs work x [] = x work x (y:ys) = work y ys
15
14 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
15 Summary zGeneralised technique for changing the type of a program to improve its performance; zCaptures a wide variety of program optimization techniques in a single unified framework; zThe paper also presents a range of new results concerning strictness side conditions.
17
16 Further Work zOther recursion patterns; zTime and space analysis; zComputational effects; zImplementing the technique.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.