Urmas Tamm Tartu 2013

Similar documents
CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

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

Functional Languages and Higher-Order Functions

CITS3211 FUNCTIONAL PROGRAMMING

Functional Languages. Hwansoo Han

Programming Language Pragmatics

ii Copyright cfl1999 Andrew Cheadle and Imperial College. All rights reserved. This document is free and is distributed in the hope that it will be us

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

Chapter 11 :: Functional Languages

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

Type Systems Winter Semester 2006

From the λ-calculus to Functional Programming Drew McDermott Posted

Graph reduction. Simple graph reduction with visualisation

Untyped Lambda Calculus

Semantics of Lazy Evaluation using the Two-Level Grammar

Untyped Lambda Calculus

CSE 3302 Programming Languages Lecture 8: Functional Programming

Lecture 5: The Untyped λ-calculus

Lambda Calculus-2. Church Rosser Property

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

CIS 500 Software Foundations Fall September 25

Chapter 5: The Untyped Lambda Calculus

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Recursive Definitions, Fixed Points and the Combinator

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Graphical Untyped Lambda Calculus Interactive Interpreter

Functional Programming

Lambda Calculus. Concepts in Programming Languages Recitation 6:

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

Fundamentals and lambda calculus

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

Demonstrating Lambda Calculus Reduction

Introduction to the Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Once Upon a Polymorphic Type

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

Appendix A A Tutorial on Combinator Graph Reduction

Lambda Calculus. Lambda Calculus

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

Qualifying Exam in Programming Languages and Compilers

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

CS 242. Fundamentals. Reading: See last slide

What does my program mean?

Lambda Calculus.

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

Dynamic analysis in the Reduceron. Matthew Naylor and Colin Runciman University of York

Efficient Interpretation by Transforming Data Types and Patterns to Functions

Lexicografie computationala Feb., 2012

Formal Systems and their Applications

CSE 505: Concepts of Programming Languages

CS 415 Midterm Exam Spring 2002

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Chapter 5: The Untyped Lambda Calculus

Lambda Calculus and Type Inference

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

University of London. For The Following Qualification:-

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus and Extensions as Foundation of Functional Programming

The Untyped Lambda Calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Data Types and Pattern Matching by Function Application

Meta-programming with Names and Necessity p.1

Introduction to the λ-calculus

Functional Programming Language Haskell

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Lambda Calculus. Lecture 4 CS /26/10

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

4/19/2018. Chapter 11 :: Functional Languages

Programming Languages

Lambda Calculus and Type Inference

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Programming in Haskell

3. Functional Programming. Oscar Nierstrasz

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

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

Software Paradigms (Lesson 4) Functional Programming Paradigm

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

LECTURE 16. Functional Programming

λ calculus is inconsistent

VU Semantik von Programmiersprachen

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

Part III. Chapter 15: Subtyping

Conservative Concurrency in Haskell

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Programming Languages

Denotational Semantics. Domain Theory

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

The Very Lazy ń-calculus and the STEC Machine

Introduction. chapter Functions

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.

Scope, Functions, and Storage Management

Plan. Syntax Substitution Operational Semantics ( with contexts!) Evaluation strategies Equality

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Programming Language Concepts: Lecture 19

Transcription:

The implementation and optimization of functional languages Urmas Tamm Tartu 2013

Intro The implementation of functional programming languages, Simon L. Peyton Jones, 1987 Miranda fully lazy strict a predecessor of Haskell

Lambda Calculus Theoretical framework behind functional programming Turing complete Simple intermediate language: only a few syntactic constructs no data-types no recursion graph reduction

Evaluation Selecting a reducible expression or redex and reducing it Multiple reduction orders: Normal order Applicative order All reduction orders always lead to the same normal form (Church-Rosser theorem) Corollary: if lambda-term has a normal form, the normal form is unique.

Translation into Lambda Calculus Two ways: Simplify the program until the final step only includes the change of syntax Use a version of enriched lambda calculus and simplify it into ordinary lambda calculus

Enriched Lambda Calculus Ordinary Lambda Calculus with extra constructs: let and letrec expressions pattern matching lambda expressions the infix operator case-expressions Translation schemas: TE returns corresponding lambda expression TD produces a letrec expression

Graph reduction Expression is held in the form of its syntax tree constant values built-in functions variable names During reduction tree becomes a graph Nodes represent small store areas cells: tag type of the cell fields, pointers Fixed-size cells vs variable-sized cells

Graph reduction

An example syntax tree Graph reduction

Selecting the next redex Evaluator reduces the graph to the normal form Call by need vs call by value Ingredients of lazy evaluation Call by need Arguments evaluated once or not at all Execution speed Weak Head Normal Form (WHNF)

Selecting the next redex Printing out an infinite list as it is generated:

Finding the next top level redex F may be a: data object built-in function a lambda abstraction

The Spine Stack

Graph reduction of lambda expressions Sharing substituting pointers instead of copying values

Graph reduction of lambda expressions Constructing a new instance of the lambda body create a new copy of the body make substitutions within the new instance

Graph reduction of lambda expressions Lazy graph implementation strategy:

Supercombinators and lambda-lifting Instantiating the body of a lambda abstraction: case analysis on the tag of each node test if the node is the formal parameter new instances of sub-expressions containing no free occurrences of the formal parameter Compilation a more efficient alternative a fixed sequence of instructions constructed in advance by a compiler Free variables in lambda bodies

Supercombinators and lambda-lifting Algorithm for generating supercombinators: Choose any lambda abstraction which has no inner lambda abstractions in its body. Take out all its free variables as extra parameters. Give an arbitrary name to the lambda abstraction Replace the occurrence of the lambda abstraction by the name applied to the free variables. Compile the lambda abstraction and associate the name with the compiled code.

Fully-lazy lambda-lifting Template-instantiation procedure risks constructing multiple instances of the same expression Leave out sub-expressions which contain no free occurrences of the formal parameter Maximal free expressions Using pointers and sharing results

SK Combinators Graph reduction technique based on a fixed set of combinators An extremely simple reduction machine

Garbage Collector Allocation of new cells during graph reduction Heap an unordered collection of cells