Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space.

Similar presentations


Presentation on theme: "Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space."— Presentation transcript:

1 Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space

2 Hylomorphisms Common programming pattern Intermediate data structure, generated and consumed These two stages could be fused together to eliminate this data structure This talk: look at space behaviour of hylomorphisms where the intermediate data structure is a list unfoldfold

3 Unfold unfold :: (a  Bool)  (a  b)  (a  a)  a  [b] unfold p hd tl = f where f x = if p x then [] else hd x : f (tl x) Example downfrom = unfold (≡ 0) id pred downfrom 3 3 : f 2 3 : (2 : f 1) 3 : (2 : (1 : f 0)) 3 : (2 : (1 : [])) f 3 > > > > > >

4 Fold-right foldr :: (b  c  c)  c  [b]  c foldr ⊕ v = g where g [] = v g (x:xs) = x ⊕ g xs Example product = foldr (*) 1 g (3 : 2 : 1 : []) 3 * g (2 : 1 : []) 3 * (2 * g (1 : [])) 3 * (2 * (1 * g []))) 3 * (2 * (1 * 1)) 3 * (2 * 1) 3 * 2 6 > > > > > > > > product (3 : 2 : 1 :[])>

5 Fusing foldrunfold hylo hylo p hd tl ⊕ v = foldr ⊕ v ◦ unfold p hd tl Eliminate intermediate list

6 Factorial factorial = product ◦ downfrom factorial = h where h x = if (x ≡ 0) then 1 else x * h (x-1) List structure still exists Not constant space “Impedance mismatch” 3 * (2 * (1 * 1))> 3 * (2 * 1)> 3 * 2> 6> 3 * (2 * (1 * h 0))> Cannot multiply until completely unfolded h 3> 3 * h 2> 3 * (2 * h 1)> factorial 3>

7 Fold-left foldl :: (c  b  c)  c  [b]  c foldl ⊕ v = g v where g a [] = a g a (x:xs) = g (a ⊕ x) xs Duality law if ⊕ associative with unit v then, foldr ⊕ v = foldl ⊕ v Example productl = foldl (*) 1 g 3 (2 : 1 : []) g (3 * 2) (1 : []) g 6 (1 : []) g (6 * 1) [] g 6 [] 6 > > > > > > g 1 3 g (1 * 3) (2 : 1 : []) > > productl (3 : 2 : 1 :[])> Strictness operator ensures accumulator expression evaluated first (g $! (a ⊕ x)) xs hylol p hd tl ⊕ v = foldl ⊕ v ◦ unfold p hd tl

8 Factorial again factorialL = productl ◦ downfrom factorialL = h 1 where h a x = if (x ≡ 0) then a else h (a * x) (x-1) h 3 2> h (3 * 2) 1> h 6 1> h (6 * 1) 0> h 6 0> 6> Constant space No intermediate structure generated Multiply can be applied as soon as elements produced h 1 3> h (1 * 3) 2> factorialL 3>

9 Formalising space results View hylo as an evaluator and derive abstract machine Make evaluation order explicit and reveal underlying data structures Instrument with space information by comparing sizes of structures on the left and right side of transitions Space is allocated if the structure on the right is bigger, and freed if it is smaller A space counting hylo function can then be backwards derived from the machine

10 hylo machine data Stack b = TOP | PUSH b (Stack b) hyloMach p hd tl ⊕ v x = f x TOP where f x c = if p x then exec c v else f (tl x) (PUSH (hd x) c) exec TOP v’ = v’ exec (PUSH y c) z = exec c (y ⊕ z)

11 factorialSpace x = free 1 ◦ f x ◦ alloc 1 where f x = if (x ≡ 0) then free 2 else free 2 ◦ f (x –1) ◦ alloc 2 Space requirements are linear, since the allocated space doesn’t change after initially freeing 2 x units: allocated ◦ factorialSpace x ◦ free 2x ≡ allocated (proof by induction over x ) Analysing factorial

12 Analysing factorialL hylol function is already an abstract machine (tail- recursive and first-order) factorialLSpace x = f x where f x = if (x ≡ 0) then free 1 else f (x –1) ◦ free 1 ◦ alloc 1 factorialSpaceL executes in constant space: allocated ◦ factorialLSpace x ◦ free 1 = allocated (proof by induction on x )

13 Summary Space is only saved through fusion if elements can be consumed as they're produced The space requirements of a function can be revealed by looking at the underlying abstract machine Generalising to other structures –foldl doesn’t generalise, but can get similar results using crush: crush = fold ◦ flatten For more details: www.cs.nott.ac.uk/~cvh


Download ppt "Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space."

Similar presentations


Ads by Google