GC 163011 Functional Programming Lecture 17 Graph Reduction

















- Slides: 17
GC 16/3011 Functional Programming Lecture 17 Graph Reduction 2/22/2021 1
Contents § Representation of lambda expressions: § Abstract § Physical § § Performing a beta reduction Reduction orders Lazy and strict evaluation Parallel evaluation 2/22/2021 2
Abstract representation (1) fx=x+3 main = f 5 (l x. (x + 3)) 5 Syntax tree: “root” node @ lx 5 @ @ + 3 (binary application nodes) x 2/22/2021 3
Abstract representation (2) z=3 fxy=x+y main = f z z @ @ (l z. ((l x. (ly. (x + y))) z z )) 3 lx Syntax graph after first beta reduction: ly 3 (sharing) @ @ + 2/22/2021 y x 4
Abstract representation (3) f x = 3, if (x = 0) = 1 + f (x-1), otherwise main = f 5 (Y (l f. (l x. (if (x=0) 3 (1+(f (x-1)))))) ) 5 2/22/2021 5
(Y (l f. (l x. (if (x=0) 3 (1+(f (x-1)))))) ) 5 @ @ 5 lf Y lx @ @ @ if 3 @ 0 @ = @ @ + @ 1 @ f @ x 2/22/2021 1 x 6
Alternative abstract representation @ 5 lx @ @ @ if 3 @ 0 @ = @ @ + @ @ 1 @ x 2/22/2021 1 x 7
Physical representation § Each node (& leaf) of the graph is held in memory § Position in memory is irrelevant § Just because two nodes occupy adjacent memory locations, that doesn’t mean they are related § Related nodes are connected using pointers (addresses) § Set aside a chunk of memory just to hold these nodes § The “HEAP” 2/22/2021 8
Physical representation (2) § Each node requires sufficient memory to store: § A tag (@, l, “op”, IND, …) § A pointer to a left subtree (alternatively, a number representing a built-in operator or a variable) § A pointer to a right subtree (alternatively, a variable or a constant such as a number) § We call each such small chunk of memory a “cell” § The heap therefore consists of many cells 2/22/2021 9
Physical representation (3) § The heap: @ 5 l x @ @ 3 x op + 2/22/2021 10
Performing a beta reduction § Make a COPY of the function body § Substitute the actual parameters for the formal parameters § OVERWRITE the root node of the expression with an indirection to the root node of the copy (preserves sharing) 2/22/2021 11
Beta reduction (2) @ l @ @ op 5 l x 3 x + @ @ @ op 5 l x 3 x + 3 5 + 2/22/2021 IND @ @ op x 3 x + 3 5 + 12
Graph reduction § § § A “redex” is a reducible expression, i. e. an expression that can be reduced by a, b, h or drule reduction A program may contain many redexes Evaluation of a program is a process of successive transformations (reductions) of the graph until the final value is found: § § § Decide which redex to reduce next Reduce it Loop to 1. 2/22/2021 13
Redexes @ @ @ if 3 @ 0 @ A redex = @ @ + @ 1 Another @ f @ 5 - 2/22/2021 redex 1 5 14
Reduction orders § There will often be many redexes that could be evaluated next § The evaluator must choose an EVALUATION ORDER: this may have a great effect on the performance of the system (but not on its (partial) correctness) 2/22/2021 15
Reduction orders (2) § NORMAL ORDER EVALUATION § starts at the top node and follows the left pointers until the first redex is found § leftmost outermost § lazy evaluation. § APPLICATIVE ORDER EVALUATION § leftmost innermost redex § strict evaluation § PARALLEL EVALUATION § § § referential transparency redexes can be reduced in any order may be reduced concurrently! 2/22/2021 16
Summary § Physical representation of lambda expressions § Performing a beta reduction § Reduction orders § Lazy and strict evaluation § Parallel evaluation 2/22/2021 17

















