WORK IT WRAP IT FIX IT FOLD IT

  • Slides: 17
Download presentation
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe

WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe

This Talk z Worker/wrapper is a simple but powerful method for optimizing recursive programs;

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

Worker / Wrapper A technique for changing the type of a recursive program to

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

Fixed Points Our original formalisation was based on the use of explicit fixed points.

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 (fix f) f xs = 1 : xs 3

The Problem Suppose we wish to change the type of a recursive program that

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

Assumptions We assume conversion functions rep A B abs such that: abs. rep =

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

Let’s Calculate! fix f = fix (id. A. f) = fix (abs. rep. f)

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

Summary We have derived the following factorisation: fix f Recursive program of type A.

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

Final Step We simplify the worker fix g = fix (rep. f. abs) to

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 rep and abs 8

The Worker / Wrapper Recipe ① Express the original program using fix; ② Choose

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. 9

Generalising The technique also works for weaker assumptions: abs. rep = id. A abs.

Generalising The technique also works for weaker assumptions: abs. rep = id. A abs. rep. f = f From the ‘fix’ paper. fix (abs. rep. f) = fix f 10

… and for other conditions relating f and g: g = rep. f. abs

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

Generalised Recipe z If the worker is already given, we aim to verify that

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

Example last [] =⊥ last [x] =x last (x: xs) = last xs last

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 13

Example fib 0 =0 fib 1 =1 fib (n+2) = fib n + fib

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 14

Summary z Generalised technique for changing the type of a program to improve its

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

Further Work z Other recursion patterns; z Time and space analysis; z Computational effects;

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