Exploring Languages
with Interpreters
and Functional Programming

Chapter 8
Evaluation Model

H. Conrad Cunningham

16 September 2018

Copyright (C) 2017, 2018, H. Conrad Cunningham

Acknowledgements: I originally created these slides in Fall 2017 to accompany what is now Chapter 8, Evaluation Model, in the 2018 version of the textbook Exploring Languages with Interpreters and Functional Programming.

Browser Advisory: The HTML version of this document may require use of a browser that supports the display of MathML. A good choice as of September 2018 is a recent version of Firefox from Mozilla.

Evaluation of Functional Programs

Lecture Goals

Referential Transparency

Referential transparency:

A symbol (e.g. variable) always represents the same value within some well-defined context (e.g. function body)

Substitution (Reduction) Model

Redex:

a subexpression that can be reduced

Selecting Redexes (1)

By position in expression

Selecting Redexes (2)

By whether contained within another redex

Haskell uses leftmost outermost redex first

Function Example

    fact1 :: Int -> Int 
    fact1 n = if n == 0 then 
                  1 
              else 
                  n * fact1 (n-1) 

Leftmost Outermost (1)

fact1 2
\Longrightarrow { replace fact1 2 using definition }
if 2 == 0 then 1 else 2 * fact1 (2-1)
\Longrightarrow { evaluate 2 == 0 }
if False then 1 else 2 * fact1 (2-1)
\Longrightarrow { evaluate if }
2 * fact1 (2-1)
\Longrightarrow { replace fact1 (2-1) using definition, add implicit parentheses }
2 * (if (2-1) == 0 then 1 else (2-1) * fact1 ((2-1)-1))
\Longrightarrow { evaluate 2-1 in condition }

Leftmost Outermost (2)

2 * (if 1 == 0 then 1 else (2-1) * fact1 ((2-1)-1))
\Longrightarrow { evaluate 1 == 0 }
2 * (if False then 1 else (2-1) * fact1 ((2-1)-1))
\Longrightarrow { evaluate if }
2 * ((2-1) * fact1 ((2-1)-1))
\Longrightarrow { evaluate leftmost 2-1 }
2 * (1 * fact1 ((2-1)-1))
\Longrightarrow { replace fact1 ((2-1)-1) using definition, add implicit parentheses }
2 * (1 * (if ((2-1)-1) == 0 then 1
else ((2-1)-1) * fact1 ((2-1)-1)-1))
\Longrightarrow { evaluate 2-1 in condition }

Leftmost Outermost (3)

2 * (1 * (if (1-1) == 0 then 1
else ((2-1)-1) * fact1 ((2-1)-1)-1))
\Longrightarrow { evaluate 1-1 in condition }
2 * (1 * (if 0 == 0 then 1
else ((2-1)-1) * fact1 ((2-1)-1)-1))
\Longrightarrow { evaluate 0 == 0 }
2 * (1 * (if True then 1
else ((2-1)-1) * fact1 ((2-1)-1)-1))
\Longrightarrow { evaluate if }
2 * (1 * 1)
\Longrightarrow { evaluate 1 * 1 }

Leftmost Outermost (4)

2 * 1
\Longrightarrow { evaluate 2 * 1 }
2

String Reduction Model

2 * fact1 (2-1)
\Longrightarrow 2 * (if (2-1) == 0 then 1 else (2-1) * fact1 ((2-1)-1))

Graph Reduction Model

2 * fact1 (2-1)
\Longrightarrow 2 * (if (2-1) == 0 then 1 else (2-1) * fact1 ((2-1)-1))
\Longrightarrow 2 * (if 1 == 0 then 1 else 1 * fact1 (1)-1))

Leftmost Outermost Graph (1)

fact1 2
\Longrightarrow { replace fact1 2 using definition }
if 2 == 0 then 1 else 2 * fact1 (2-1)
\Longrightarrow { evaluate 2 == 0 in condition }
if False then 1 else 2 * fact1 (2-1) }
\Longrightarrow { evaluate if }
2 * fact1 (2-1)
\Longrightarrow { replace fact1 (2-1) using definition, add implicit parentheses }
2 * (if (2-1) == 0 then 1 else (2-1) * fact1 ((2-1)-1))
\Longrightarrow { evaluate 2-1 because of condition (3 occurrences in graph) }
2 * (if 1 == 0 then 1 else 1 * fact1 (1-1))
\Longrightarrow { evaluate 1 == 0 }

Leftmost Outermost Graph (2)

2 * (if False then 1 else 1 * fact1 (1-1))
\Longrightarrow { evaluate if }
2 * (1 * fact1 (1-1))
\Longrightarrow { replace fact1 ((1-1) using definition, add implicit parentheses }
2 * (1 * (if (1-1) == 0 then 1 else (1-1) * fact1 ((1-1)-1))
\Longrightarrow { evaluate 1-1 because of condition (3 occurrences in graph) }
2 * (1 * (if 0 == 0 then 1 else 0 * fact1 (0-1))
\Longrightarrow { evaluate 0 == 0 }
2 * (1 * (if True then 1 else 0 * fact1 (0-1))
\Longrightarrow { evaluate if }

Leftmost Outermost Graph (3)

2 * (1 * 1)
\Longrightarrow { evaluate 1 * 1 }
2 * 1
\Longrightarrow { evaluate 2 * 1 }
2

Time Complexity

Time measure:

number of steps in leftmost outermost graph reduction

Space Complexity

Space measure:

maximum graph size needed for leftmost outmost graph reduction

Termination

Key Ideas