21
THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill

THE WORKER / WRAPPER TRANSFORMATION

  • Upload
    brook

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

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; - PowerPoint PPT Presentation

Citation preview

Page 1: THE WORKER / WRAPPER TRANSFORMATION

THE WORKER / WRAPPER TRANSFORMATION

Graham Hutton and Andy Gill

Page 2: THE WORKER / WRAPPER TRANSFORMATION

2

What Is It?

program

wrapper

worker

A technique for changing the type of a program inorder to improve its performance:

Page 3: THE WORKER / WRAPPER TRANSFORMATION

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.

Page 4: THE WORKER / WRAPPER TRANSFORMATION

4

Fixed Points

ones = 1 : ones

ones = fix body

body xs = 1 : xs

can be rewritten as:

fix f = f (fix f)

The key to formalising the technique is the use ofexplicit fixed points. For example:

Page 5: THE WORKER / WRAPPER TRANSFORMATION

5

The Problem

A

Type of the desired worker.

Type of the original

program.

B

Suppose we wish to change the type of a recursiveprogram, defined by prog = fix body.

Page 6: THE WORKER / WRAPPER TRANSFORMATION

6

Assumptions

We assume conversion functions

A can be faithfully

represented by B.

such that:

wrap . unwrap = idA

A B

wrap

unwrap

Page 7: THE WORKER / WRAPPER TRANSFORMATION

7

Let’s Calculate!

7

prog

fix body

=

fix (wrap . unwrap . body)

=fix (idA . body)

=

wrap work=

wrap (fix (unwrap . body . wrap))=

Rolling rule.

Page 8: THE WORKER / WRAPPER TRANSFORMATION

8

Summary

prog

We have derived the following factorisation:

Wrapper of type B

A.

Recursive program of type A.

wrap=

Recursive worker of type

B.

work

Page 9: THE WORKER / WRAPPER TRANSFORMATION

9

The Final Step

We simplify

work = fix (unwrap . body . wrap)

unwrap wrapand

to eliminate the overhead of repeatedly convertingbetween the two types, by fusing together

Page 10: THE WORKER / WRAPPER TRANSFORMATION

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.

Page 11: THE WORKER / WRAPPER TRANSFORMATION

11

Example - Reverse

How can we improve:

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]

Quadratic time.

Page 12: THE WORKER / WRAPPER TRANSFORMATION

12

Step 2 - choose a new type for the program

[a] [a] [a]

abs

rep

where

rep xs = (xs ++)

abs f = f []

Key idea (Hughes):

represent the result list as a

function.

Page 13: THE WORKER / WRAPPER TRANSFORMATION

13

Step 3 – define conversion functions

[a] [a]

wrap

unwrap

where

unwrap f = rep . f

wrap g = abs . g

Satisfies the worker/wrapp

er assumption.

[a] [a] [a]

Page 14: THE WORKER / WRAPPER TRANSFORMATION

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.

Page 15: THE WORKER / WRAPPER TRANSFORMATION

15

Using properties of rep and

Worker/wrapper fusion

property.

we obtain a linear time worker:

work :: [a] [a] [a]work [] ys = yswork (x:xs) ys = work xs (x:ys)

unwrap (wrap work)

work

=

Page 16: THE WORKER / WRAPPER TRANSFORMATION

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.

Page 17: THE WORKER / WRAPPER TRANSFORMATION

17

Example - Unboxing

Int Int

More efficient worker that uses

unboxed integers.

Type of a simple factorial

function.

Int♯ Int♯

Note: this is how GHC uses worker/wrapper.

Page 18: THE WORKER / WRAPPER TRANSFORMATION

18

Example - Memoisation

More efficient worker that

uses a memo table.

Type of a simple Fibonacci function.

Nat Nat Stream Nat

Page 19: THE WORKER / WRAPPER TRANSFORMATION

19

Example - Continuations

Expr Mint

More efficient worker that uses

success and failure

continuations.

Type of a simple evaluation

function that may fail, where Mint =

Maybe Int.

Expr

(Int Mint)

Mint Mint

Page 20: THE WORKER / WRAPPER TRANSFORMATION

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.

Page 21: THE WORKER / WRAPPER TRANSFORMATION

21

Further Work

Mechanising the technique;

Specialised patterns of recursion;

Generalisation using category theory;

Programs with effects;

Other application areas.