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

Similar documents
Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

1 Introduction. 3 Syntax

CSE 505: Concepts of Programming Languages

Pure Lambda Calculus. Lecture 17

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Recursive Definitions, Fixed Points and the Combinator

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

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

CSE-321 Programming Languages 2011 Final

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

CSE-321 Programming Languages 2012 Midterm

Graphical Untyped Lambda Calculus Interactive Interpreter

Lambda Calculus and Extensions as Foundation of Functional Programming

Chapter 5: The Untyped Lambda Calculus

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

Fundamentals and lambda calculus

CIS 500 Software Foundations Midterm I

Concepts of programming languages

Functional Languages and Higher-Order Functions

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lecture 9: Typed Lambda Calculus

Exercise 1 ( = 24 points)

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

CIS 500 Software Foundations Fall September 25

Lambda Calculus. Lecture 4 CS /26/10

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CITS3211 FUNCTIONAL PROGRAMMING

Introduction to the Lambda Calculus

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

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

Mutable References. Chapter 1

Lecture 5: Lazy Evaluation and Infinite Data Structures

Errata for Theories of Programming Languages

Untyped Lambda Calculus

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

1998 Midterm and Solutions

Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

Elixir, functional programming and the Lambda calculus.

Programming Languages Lecture 15: Recursive Types & Subtyping

Lecture Notes on Data Representation

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

CMSC 330: Organization of Programming Languages

Types and Programming Languages. Lecture 5. Extensions of simple types

Type Systems Winter Semester 2006

Lecture 14: Recursive Types

Exercise 1 ( = 18 points)

Untyped Lambda Calculus

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

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

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Functional Programming

Abstract register machines Lecture 23 Tuesday, April 19, 2016

Organization of Programming Languages CS3200/5200N. Lecture 11

CS 209 Functional Programming

If a program is well-typed, then a type can be inferred. For example, consider the program

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.

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

λ calculus is inconsistent

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

CMSC 330, Fall 2018 Midterm 2

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

CSE 3302 Programming Languages Lecture 8: Functional Programming

301AA - Advanced Programming

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

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

Theorem Proving Principles, Techniques, Applications Recursion

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

Lambda Calculus. Variables and Functions. cs3723 1

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

CSE-321 Programming Languages 2010 Midterm

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

Functional programming languages

Functional Programming

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

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

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

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

CMSC330. Objects, Functional Programming, and lambda calculus

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Types and Programming Languages. Lecture 8. Recursive type

Accurate Step Counting

CSE-321 Programming Languages 2014 Final

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

Programming Languages

Transcription:

Overview A normal-order language Strictness Recursion Infinite data structures Direct denotational semantics Transition semantics Lazy (call-by-need) evaluation and its semantics

A Normal-Order Language Recall that normal-order evaluation guarantees finding a canonical form if one exists; under eager evaluation more terms diverge. So it is useful to consider a language based on normal-order evaluation. Syntactically the language is similar to the eager functional language, but semantically all tuples and alternatives are canonical forms: cfm ::= intcfm boolcfm funcfm tupcfm altcfm tupcfm ::= exp,... exp eager: cfm,... cfm altcfm ::= @ tag exp eager: @ tag cfm tuple and alternative constructors are lazy, evaluation is forced by deconstruction: e e 0,... e n 1 e.k z e k z e @ k e e k e z sumcase e of (e 0,... e n 1 ) z (k < n) application is reduced in normal order: e λv. ê (ê/v e z) e e z

Strictness of Constructs A construct is strict in one of its subterm if its evaluation always requires the evaluation of that subterm since the meaning of a term created by this construct can be expressed as a strict function applied to the meaning of the subterm (recall a strict function between domains maps to ). E.g. application is strict in the subterm in function position: e λv. ê (ê/v e z) e e z

Semantics of Arithmetic and Boolean Operations Transition semantics and strictness of arithmetic/boolean constructs: e n -e n e n e n e+e n + n e true e z if e then e else e z e false e z if e then e else e z

Additional syntax: with evaluation rule Recursion in the Normal-Order Language exp ::=... rec exp e (rec e) z rec e z Thus rec e is syntactic sugar for Y e in untyped languages or those with recursive types, but must be primitive in a simply typed language. More syntactic sugar: letrec p 1 e 1,... p n e n in e def = let p 1,... p n rec (λ p 1,... p n. e 1,... e n ) in e Note: fixed-points are not necessarily functions since all tuples and alternatives are canonical forms, so e.g. the following does not diverge: letrec ones 1::ones in ones

Using Normal-Order Evaluation One can define short-circuit Boolean operators as syntactic sugar: e def = if e then false else true e e def = if e then e else false Since only the subterms whose value is necessary are evaluated, we can use the standard foldl (reduce) to define short-circuit operations on lists: let foldl λf. λz. rec (λg. λl. listcase l of (z, λx. λxs. f x (g z xs))), prod foldl (λx. λy. if x = 0 then 0 else x*y) 1 in...

Programming with Infinite Data Structures Since fixed points can be of any type, one can define letrec nats 0 :: map (λx. x+1) nats, sumlists λxs. λys. listcase xs of (ys, λx. λxs. listcase ys of (nil, λy. λys. (x+y):: sumlist xs ys )), fib 0 :: fib1, fib1 1 :: sumlists fib fib1 in... Any finite part of these data structures will be computed in finite time as the canonical forms of its subterms become necessary.

Direct Denotational Semantics The major change from the semantics of an eager language is that the environments bind variables to computations in V, not values in V : E def = var V Similarly the (pre)domains of functions, tuples, and alternatives are V fun = [V V ] since variables are bound to computations V tup = (V ) since tuples... V alt = N V... since tuples and alternatives are non-strict Semantic equations for abstraction and application: [[λv. e]]η = ι norm (ι fun (λa V. [[e]][η v : a])) [[e e ]]η = (λf V fun. f ([[e ]]η)) fun ([[e]]η)

Semantics of Tuples and Alternatives Unlike the eager language, tuples and alternatives are non-strict, hence divergence in subterms of tuples and alternatives is not propagated to the meaning of the entire term: [[ e 0,... e n 1 ]]η = ι norm (ι tup [[e 0 ]]η,... [[e n 1 ]]η ) [[@ k e]]η = ι norm (ι alt k, [[e]]η ) The meaning of selecting a component of a tuple is simply the meaning of the component, so no extra care is needed to propagate its divergence: [[e.k]]η = (λt V tup. if k dom t then t k else tyerr) tuple ([[e]]η) The sumcase construct is strict in the discriminant, hence the use of lifting: [[sumcase e of (e 0,... e n 1 )]]η = (λ k, a V alt. if k < n then (λf V fun. f a) fun ([[e k ]]η) else tyerr) alt ([[e]]η)

Transition Semantics The normal-order evaluation of a term can be described by a transition relation : (λv. e) e (e/v e ) if true then e else e e if false then e else e e e 0,... e n 1.k e k, if k < n sumcase @ k e of (e 0,... e n 1 ) e k e, if k < n rec e e (rec e) plus contextual closure according to the strictness of constructs, e.g. e e 1 e e e 1 e e e 1 if e then e else e if e 1 then e else e

Lazy (Call-By-Need) Evaluation A naïve substitution-based implementation of normal-order evaluation would be very inefficient because it would duplicate much of the work: e n (λx. x+x) e n + n will evaluate e to n twice; in general the extra amount of work is not bounded by any elementary function. Call-by-need evaluation uses sharing to reduce each term at most once: λx app + + + e n + n + x x n x e n Hence we need to extend the language so it can express sharing.

The Call-By-Need Calculus For simplicity the syntax of application and alternatives is restricted to exp ::=... exp var @ tag var but letrec is promoted to a basic construct, so we can define the general forms of application and alternatives as sugar: where v / F V (e). e e @ k e rec e def = letrec v e in e v def = letrec v e in @ k v def = letrec v e v in v We also rename as necessary so that no bindings in a term bind the same variables.

Semantics of the Call-By-Need Calculus Sharing is expressed by referring to variables, which are mapped to terms by heaps: σ var (exp {busy}) A heap σ is closed if F V (σ v) dom σ for each v dom σ. A term e and a heap σ are compatible if σ is closed and F V (e) dom σ. Evaluation of a variable v re-maps it to the value of the term that v is bound to: [σ v : busy], e z, σ [σ v : e], v z, [σ v : z renamed ] Implementations usually avoid reevaluating values by marking them as such. The token busy is used to prevent attempts to compute the value of a variable whose value is currently being computed (busy is not really necessary since we don t allow infinite derivations anyway). Evaluation of letrec moves the bindings to the heap: [σ v 0 : e 0...], e z, σ σ, letrec v 0 e 0,... in e z, σ

A Call-By-Need Example (λf. f 4+f 2) ((λx. λy. (x+2)*y) 5) expands as letrec g (letrec z 5 in (λx. λy. (x+2)*y) z) in (λf. f 4+f 2) g [], letrec g (letrec z 5 in (λx. λy. (x+2)*y) z) in (λf. f 4+f 2) g [g : letrec z 5 in (λx. λy. (x+2)*y) z], (λf. f 4+f 2) g [g : letrec z 5 in (λx. λy. (x+2)*y) z], λf. f 4+f 2 λf. f 4+f 2, [g : letrec z 5 in (λx. λy. (x+2)*y) z] [g : letrec z 5 in (λx. λy. (x+2)*y) z], g 4+g 2 [g : letrec z 5 in (λx. λy. (x+2)*y) z], g 4 [g : letrec z 5 in (λx. λy. (x+2)*y) z], g [g : busy], letrec z 5 in (λx. λy. (x+2)*y) z [g : busy z : 5], (λx. λy. (x+2)*y) z [g : busy z : 5], λy. (z+2)*y λy. (z+2)*y, [g : λy. (z+2)*y z : 5] [g : λy. (z+2)*y z : 5], (λy. (z+2)*y) 4 [g : λy. (z+2)*y z : 5], (z+2)*4... 28, [g : λy. (z+2)*y z : 5] [g : λy. (z+2)*y z : 5], g 2 [g : λy. (z+2)*y z : 5], g λy. (z+2)*y, [g : λy. (z+2)*y z : 5]... 14, [g : λy. (z+2)*y z : 5] 42, [g : λy. (z+2)*y z : 5] evaluation of g begins evaluation of g ends g is already evaluated

A Remark on Lazy Evaluation Note that in the example the term (z+2) is evaluated twice, although the heap binding of z to 5 is unchanged, so the work for evaluating (z+2) is duplicated. Call-by-need cannot completely eliminate this kind of duplication of work. This can be achieved using optimal evaluation [Lévy 78, Lamping 90, Gonthier/Abadi/Lévy 92].