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

Slides:



Advertisements
Similar presentations
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
Register Allocation Zach Ma.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
CALCULATING AN EXCEPTIONAL MACHINE Graham Hutton and Joel Wright University of Nottingham.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
CATCH: Case and Termination Checker for Haskell Neil Mitchell (Supervised by Colin Runciman)
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
Introduction to Computability Theory
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
CS 536 Spring Code generation I Lecture 20.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
7/2/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
analysis, plug ‘n’ chug, & induction
Solving a System of Equations using Multiplication
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
June 11, 2002© Howard Huang1 Boolean algebra Last time we talked about Boolean functions, Boolean expressions, and truth tables. Today we’ll learn.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Recursion Higher Order Functions CSCE 314 Spring 2016.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Code Optimization Overview and Examples
Recursion.
Programming Languages and Compilers (CS 421)
PROGRAMMING IN HASKELL
Data Structures Interview / VIVA Questions and Answers
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
Pattern Matching Pattern matching allows us to do things like this:
PROGRAMMING IN HASKELL
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Madhusudan Parthasarathy
Announcements Quiz 5 HW6 due October 23
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

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

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

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

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 :[])>

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

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>

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

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>

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

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)

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

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 )

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: