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

Similar documents
Pure Lambda Calculus. Lecture 17

CITS3211 FUNCTIONAL PROGRAMMING

Lambda Calculus and Extensions as Foundation of Functional Programming

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

Introduction to the Lambda Calculus

1 Scope, Bound and Free Occurrences, Closed Terms

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages

Lambda Calculus. Lecture 4 CS /26/10

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lecture 5: The Untyped λ-calculus

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

Fundamentals and lambda calculus

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

A Quick Overview. CAS 701 Class Presentation 18 November Department of Computing & Software McMaster University. Church s Lambda Calculus

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Urmas Tamm Tartu 2013

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

Formal Systems and their Applications

Untyped Lambda Calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Concepts of programming languages

Recursive Definitions, Fixed Points and the Combinator

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

Type Systems Winter Semester 2006

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

Functional Programming

The Untyped Lambda Calculus

From the λ-calculus to Functional Programming Drew McDermott Posted

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

The Untyped Lambda Calculus

Untyped Lambda Calculus

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

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

> module Lambda where > import Data.List -- I need some standard list functions

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Part I. Historical Origins

Lambda Calculus. Lambda Calculus

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

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

lambda calculus History 11/28/13 Jianguo Lu A formal system designed to inves:gate func:on defini:on, func:on applica:on, and 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

Variables. Substitution

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Lambda Calculus and Type Inference

Introduction to the λ-calculus

Lambda Calculus.

Programming Languages Third Edition

CMSC330. Objects, Functional Programming, and lambda calculus

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

Functional Programming

dynamically typed dynamically scoped

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

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

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

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

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

CSE 505: Concepts of Programming Languages

VU Semantik von Programmiersprachen

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Chapter 5: The Untyped Lambda Calculus

Lambda Calculus and Type Inference

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Functional Languages and Higher-Order Functions

Shell CSCE 314 TAMU. Haskell Functions

Chapter 5: The Untyped Lambda Calculus

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

3.1 λ-calculus: Syntax

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

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

10.6 Theoretical Foundations

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Introduction to the Lambda Calculus. Chris Lomont

(Refer Slide Time: 4:00)

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

Elixir, functional programming and the Lambda calculus.

Functional Programming Languages (FPL)

The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

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

The Untyped Lambda Calculus

Functional Languages. Hwansoo Han

Operational Semantics 1 / 13

CS 242. Fundamentals. Reading: See last slide

Programming Language Concepts: Lecture 19

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Lambda Calculus. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Software Paradigms (Lesson 4) Functional Programming Paradigm

CMSC 330: Organization of Programming Languages. Operational Semantics

Costly software bugs that could have been averted with type checking

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

INF 212 ANALYSIS OF PROG. LANGS LAMBDA CALCULUS. Instructors: Crista Lopes Copyright Instructors.

Transcription:

Last class CS6848 - Principles of Programming Languages Principles of Programming Languages V. Krishna Nandivada IIT Madras Interpreters A Environment B Cells C Closures D Recursive environments E Interpreting OO (MicroJava) programs. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 1 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 2 / 1 Outline Introduction An interpreter executes a program as per the semantics. An interpreter can be viewed as an executable description of the semantics of a programming language. Program semantics is the field concerned with the rigorous mathematical study of the meaning of programming languages and models of computation. Formal ways of describing the programming semantics. Operational semantics - execution of programs in the language is described directly (in the context of an abstract machine). Big-step semantics (with environments) -is close in spirit to the interpreters we have seen earlier. Small-step semantics (with syntactic substitution) - formalizes the inlining of a procedure call as an approach to computation. Denotational Semantics - each phrase in the language is translatedto a denotation - a phrase in some other language. Axiomatic semantics - gives meaning to phrases by describing the logical axioms that apply to them. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 3 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 4 / 1

Lambda Calculus Extension of the Lambda-calculus The traditional syntax for procedures in the lambda-calculus uses the Greek letter λ (lambda), and the grammar for the lambda-calculus can be written as: e ::= x λx.e e 1 e 2 x Identifier (infinite set of variables) Brackets are only used for grouping of expressions. Convention for saving brackets: that the body of a λ-abstraction extends as far as possible. For example, λx.xy is short for λx.(xy) and not (λx.x)y. Moreover, e 1 e 2 e 3 is short for (e 1 e 2 )e 3 and not e 1 (e 2 e 3 ). We will give the semantics for the following extension of the lambda-calculus: e ::= x λx.e e 1 e 2 c succ e x Identifier (infinite set of variables) c Integer V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 5 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 6 / 1 Outline Big step semantics Here is a big-step semantics with environments for the lambda-calculus. w, v Value v ::= c (λ x.e, ρ) ρ Environment ρ ::= x 1 v 1, x n v n The semantics is given by the following five rules: V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 7 / 1 (1) (2) (3) (4) (5) ρ x v (ρ(x) = v) ρ λx.e (λx.e,ρ) ρ e 1 (λx.e,ρ ) ρ e 2 v ρ,x v e w ρ e 1 e 2 w ρ c c ρ e c 1 ρ succ e c 2 c 2 = c 1 + 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 8 / 1

Outline Small step semantics In small step semantics, one step of computation = either one primitive operation, or inline one procedure call. We can do steps of computation in different orders: > (define foo (lambda (x y) (+ ( x 3) y))) > (foo (+ 4 1) 7) 22 Let us calculate: (foo (+ 4 1) 7) => ((lambda (x y) (+ ( x 3) y)) (+ 4 1) 7) => (+ ( (+ 4 1) 3) 7) => 22 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 9 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 10 / 1 Small step semantics (contd.) Free variables We can also calculate like this: (foo (+ 4 1) 7) => (foo 5 7) => ((lambda (x y) (+ ( x 3) y)) 5 7) => (+ ( 5 3) 7) A variable x occurs free in an expression E iff x is not bound in E.Examples: no variables occur free in the expression (lambda (y) ((lambda (x) x) y)) the variable y occurs free in the expression ((lambda (x) x) y) An expression is closed if it does not contain free variables. A program is a closed expression. => 22 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 11 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 12 / 1

Methods of procedure application Methods of procedure application Call by value ((lambda (x) x) ((lambda (y) (+ y 9)) 5)) => ((lambda (x) x) (+ 5 9)) => ((lambda (x) x) 14) => 14 Always evaluate the arguments first Example: Scheme, ML, C, C++, Java Call by name (or lazy-evaluation) ((lambda (x) x) ((lambda (y) (+ y 9) 5)) => ((lambda (y) (+ y 9)) 5) => (+ 5 9) => 14 Avoid the work if you can Example: Miranda and Haskell Lazy or eager: Is one more efficient? Are both the same? V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 13 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 14 / 1 Difference Q: If we run the same program using these two semantics, can we get different results? A: If the run with call-by-value reduction terminates, then the run with call- by-name reduction terminates. (But the converse is in general false). If both runs terminate, then they give the same result. Call by value - too eager? Sometimes call-by-value reduction fails to terminate, even though call-by- name reduction terminates. (define delta (lambda (x) (x x))) (delta delta) => (delta delta) => (delta delta) =>... Church Rosser theorem E E1 V E2 Consider the program: (define const (lambda (y) 7)) (const (delta delta)) call by value reduction fails to terminate; cannot finish evaluating the operand. call by name reduction terminates. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 15 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 16 / 1

Summary - calling convention Beta reduction call by value is more efficient but may not terminate call by name may evaluate the same expression multiple times. Lazy languages uses - call-by-need. Languages like Scala allow both call by value and name! A procedure call which is ready to be inlined is called a beta-redex. Example ( (lambda (var) body ) rand ) In lambda-calculus call-by-value and call-by-name reduction allow the choosing of arbitrary beta-redex. The process of inlining a beta-redex for some reducible expression is called beta-reduction. ( (lambda (var) body ) rand ) body[var:=rand] η conversion: A simple optimization: (λ x (E x)) = E A conversion when applied in the left-to-right direction is called a reduction. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 17 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 18 / 1 Notes on reduction Notes on reduction Applicative order reduction - A β reduction can be applied only if both the operator and the operand are already values. Else? Applicative order reduction (call by value), example: Scheme, C, Java. Is there a reduction strategy which is guaranteed to find the answer if it exists? leftmost reduction (lazy evaluation). leftmost-reduction reduce the β-redex whose left parenthesis comes first A lambda expression is in normal form if it contains no β-redexes. An expression in normal form cannot be further reduced. e.g. constant or (lambda (x) x) Church-Rosser theorem expression can have at most one normal form. leftmost reduction will find the normal form of an expression if one exists. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 19 / 1 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 20 / 1

Name clashes Substitution Care must be taken to avoid name clashes. Example: ((lambda (x) (lambda (y) (y x))) (y 5)) should not be transformed into (lambda (y) (y (y 5))) The reference to y in (y 5) should remain free! The solution is to change the name of the inner variable name y to some name, say z, that does not occur free in the argument y 5. ((lambda (x) (lambda (z) (z x))) (y 5)) => (lambda (z) (z (y x))) ;; the y present. V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 21 / 1 The notation e[x := M] denotes e with M substituted for every free occurrence of x in such that a way that name clashes are avoided. We will define e[x := M] inductively on e. x[x := M] M y[x := M] y (x y) (λx.e 1 )[x := M] (λx.e 1 ) (λy.e 1 )[x := M] λz.((e 1 [y := z])[x := M]) (where x y and z does not occur free in e 1 or M). (e 1 e 2 )[x := M] (e 1 [x := M])(e 2 [x := M]) c[x := M] c (succ e 1 )[x := M] succ (e 1 [x := M]) The renaming of a bound variable by a fresh variable is called alpha-conversion. Q: Can we avoid creating a new variable in application? V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 22 / 1 Small step semantics Here is a small-step semantics with syntactic substitution for the lambda-calculus. v Value v ::= c λx.e The semantics is given by the reflexive, transitive closure of the relation V (6) (7) (8) (9) (10) V Expression Expression (λx.e)v V e[x := v] e 1 V e 1 e 1 e 2 V e 1 e 2 e 2 V e 2 ve 2 V ve 2 succc 1 V c 2 ( c 2 = c 1 + 1) e 1 V e 2 succ e 1 V succe 2 V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 23 / 1