CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Similar documents
Lecture 5: Lazy Evaluation and Infinite Data Structures

Urmas Tamm Tartu 2013

CITS3211 FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Compilers: The goal. Safe. e : ASM

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Lambda Calculus-2. Church Rosser Property

Abstract register machines Lecture 23 Tuesday, April 19, 2016

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Last class. CS Principles of Programming Languages. Introduction. Outline

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Lambda Calculus and Extensions as Foundation of Functional Programming

CITS3211 FUNCTIONAL PROGRAMMING. 13. Interpretation

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Fundamentals and lambda calculus

MPRI course 2-4 Functional programming languages Exercises

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CSE 505: Concepts of Programming Languages

4.5 Pure Linear Functional Programming

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

CS558 Programming Languages

Pure Lambda Calculus. Lecture 17

Lecture 5: The Untyped λ-calculus

JVM ByteCode Interpreter

Functional Programming

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Functional Programming

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Functional Languages and Higher-Order Functions

Concepts of programming languages

To figure this out we need a more precise understanding of how ML works

We defined congruence rules that determine the order of evaluation, using the following evaluation

Optimizing Closures in O(0) time

Chapter 5: The Untyped Lambda Calculus

CSE 3302 Programming Languages Lecture 8: Functional Programming

The Metalanguage λprolog and Its Implementation

What does my program mean?

Graph reduction. Simple graph reduction with visualisation

A Small Interpreted Language

Appendix A A Tutorial on Combinator Graph Reduction

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Lambda Calculus. Lecture 4 CS /26/10

HOT-Compilation: Garbage Collection

Type Systems Winter Semester 2006

CPEG 852 Advanced Topics in Computing Systems The Dataflow Model of Computation

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

MaMa a simple abstract machine for functional languages

From the λ-calculus to Functional Programming Drew McDermott Posted

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

G Programming Languages - Fall 2012

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Push/enter vs eval/apply. Simon Marlow Simon Peyton Jones

CS558 Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

The Untyped Lambda Calculus

Recursive Definitions, Fixed Points and the Combinator

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Principles of Operating Systems

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Run-time Environment

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Structure of Programming Languages Lecture 10

CMSC 330: Organization of Programming Languages

More Lambda Calculus and Intro to Type Systems

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Constraint-based Analysis. Harry Xu CS 253/INF 212 Spring 2013

Functional Programming

Accurate Step Counting

CIS 500 Software Foundations Fall September 25

Fundamental Concepts. Chapter 1

Programming Languages and Techniques (CIS120)

Introduction to Lambda Calculus. Lecture 7 CS /08/09

QIR Specification. Romain Vernoux. January 6, Notations 2

dynamically typed dynamically scoped

INTRODUCTION TO HASKELL

Compiler Construction D7011E

Lambda Calculus. Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Trees, Part 1: Unbalanced Trees

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Functional programming Primer I

Lecture 7: Binding Time and Storage

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CSE-321 Programming Languages 2010 Midterm

Functional Languages. Hwansoo Han

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Introduction to the Lambda Calculus

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Untyped Lambda Calculus

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Transcription:

CITS3211 FUNCTIONAL PROGRAMMING 14. Graph reduction Summary: This lecture discusses graph reduction, which is the basis of the most common compilation technique for lazy functional languages. CITS3211 Functional Programming 1 14. Graph reduction

Graph reduction Graph reduction is the basis of the main implementation technique used in the compilation of lazy functional languages A program is represented as a graph A graph is a tree where multiple pointers can reference the same node multiple pointers to a node result in sharing, which avoids repeated evaluation this is in contrast to the interpreter from earlier, which represented sharing via an environment A graph is cyclic if there is a path from any node back to itself, otherwise it is acyclic cyclic graphs can occur in infinite data structures and with recursive functions Evaluation proceeds by replacing part of the graph that represents a redex (e.g. a function application) with the result of reducing the redex In the simple case, this is more like an interpreter than a compiler but later we will see that it is possible to compile the functions in a program into machine code that directly transforms the graph CITS3211 Functional Programming 2 14. Graph reduction

Graph representation of programs We will have four node types to represent expressions 1. A leaf node represents a constant or variable constants will include the names of user defined functions basically named λ abstractions 2. An @ node (an application node) represents a function application 3. A λ node (an abstraction node) represents a λ abstraction, i.e. a function from the users program 4. A : node (a constructor node) represents constructed data tuples, lists, user defined data, etc. the program will already have been type checked, so we can use the same representation for different types Each node in the graph is represented in memory as a contiguous sequence of words known as a cell a cell contains a tag and one or more fields each field contains either an atomic value (a number) or a pointer (an address) CITS3211 Functional Programming 3 14. Graph reduction

Fixed vs. variable sized cells An implementation may support fixed sized or variable sized cells fixed sized: all cells contain exactly two fields corresponds to a binary graph variable sized: cells can have any number of fields corresponds to an n ary graph Variable sized cells are clearly more flexible and give a more compact representation e.g. for tuples, curried functions, arbitrary precision numbers, user defined data Variable sized cells are more efficient but are more complicated to implement in particular, storage management is more complicated CITS3211 Functional Programming 4 14. Graph reduction

Boxed vs. unboxed representations An implementation may support unboxed values or only boxed values In a boxed implementation, every value has its own cell including atomic values like numbers In an unboxed implementation, atomic values can occur directly in the fields of other cells Unboxed implementations are more flexible and clearly give a more compact representation Unboxed implementations are faster but incur some implementation complexity storage management is more complicated fields must contain tags (in addition to the tags on cells) to distinguish between numbers and pointers CITS3211 Functional Programming 5 14. Graph reduction

Evaluation An expression is reduced to weak head normal form by repeated reduction of the outermost redex normal order reduction At each stage, we have to identify the next top level redex A redex is either a built in function applied to (at least) the right number of arguments an abstraction applied to one or more arguments We have to identify the leftmost lowermost node in the program graph to identify the redex type We unwind the spine of the graph until we reach the leftmost node the spine then holds the argument(s) of the redex If a function is strict in an argument, the argument must be evaluated before the function is applied CITS3211 Functional Programming 6 14. Graph reduction

β reduction β reduction is one of the main steps in the evaluation of a functional language program (λx.e) E β E[E /x] β reduction in the graph reduction model has three essential parts 1. Copy the body E of the abstraction 2. Substitute a pointer to the argument E for every instance of the bound variable x 3. Overwrite the redex with the root node of the copy note that these parts are not necessarily implemented separately Consider the evaluation of (λf. λx. f (f x) ) (λy.+ y 1) (+ 2 3) CITS3211 Functional Programming 7 14. Graph reduction

Copying and garbage Copying the body of an abstraction involves allocating space for each node in the copy this preserves referential transparency clearly it is important that storage allocation is fast Only the root node is overwritten always with an expression which is equivalent to the original Sometimes copying a graph doesn t change it i.e. if the graph doesn t contain any instances of the variable being substituted Avoiding unnecessary copying saves space and time if two graphs are the same, only one copy is needed we can avoid the copying time this also increases sharing if the graph contains any redexes an implementation that maximises sharing in this way is called fully lazy When a reduction is performed, some nodes may become detached from the graph if these nodes are no longer accessible, the space they occupy can be collected and recycled these nodes are known as garbage nodes the process of recycling garbage nodes is known as garbage collection CITS3211 Functional Programming 8 14. Graph reduction

The reduction algorithm repeat unwind the spine of the graph to the first non @ node args = stack depth case node type of leaf node: if it is a λ name then get the definition else if it is a primitive and args arity then reduce any strict arguments apply the appropriate rule overwrite the root of the redex λ node: if args > 0 then copy the body substitute the argument overwrite the root of the redex : node: (don't do anything) end until expression in WHNF CITS3211 Functional Programming 9 14. Graph reduction

Projector functions A projector function is a function whose body is a single variable e.g. id, const, head, tail, fst, snd Projector functions can cause a loss of sharing consider the application head [f E] naive overwriting causes the application f E to be duplicated The problem can be solved by introducing a new node type, the indirection node an indirection node is a pointer to another node it represents the same value as the node to which it points However indirections are a potential source of inefficiency because they can occur anywhere every time we perform any operation, we have to test for indirections chains of indirections can form CITS3211 Functional Programming 10 14. Graph reduction

Indirections continued These problems can be avoided with a simple observation the result of a projector function will be the next outermost redex the (relevant portion of the) argument can be evaluated before applying the projector function this is an example of how we can vary the reduction order for efficiency reasons without compromising the semantics of the language This helps both schemes (with and without indirections) it saves the loss of sharing through overwriting the node that we copy won t be a redex it prevents chains of indirections from forming It is unclear whether copying or using indirections is better overall indirections use less space (the space they use can be recovered quickly) but we must still test for indirections at every operation CITS3211 Functional Programming 11 14. Graph reduction