VU Semantik von Programmiersprachen

Similar documents
Lambda Calculus. Lecture 4 CS /26/10

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

The Untyped Lambda Calculus

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

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

The Untyped Lambda Calculus

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

3.1 λ-calculus: Syntax

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

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

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

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

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

Pure Lambda Calculus. Lecture 17

Chapter 5: The Untyped Lambda Calculus

Chapter 5: The Untyped Lambda Calculus

Lecture 5: The Untyped λ-calculus

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

Lambda Calculus. Variables and Functions. cs3723 1

Introduction to the λ-calculus

CITS3211 FUNCTIONAL PROGRAMMING

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Activity. CSCI 334: Principles of Programming Languages. Lecture 4: Fundamentals II. What is computable? What is computable?

Introduction to the Lambda Calculus. Chris Lomont

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

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

Fundamentals and lambda calculus

1 Scope, Bound and Free Occurrences, Closed Terms

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

Introduction to the Lambda Calculus

Lambda Calculus. Lambda Calculus

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

Lexicografie computationala Feb., 2012

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

dynamically typed dynamically scoped

Lambda Calculus and Extensions as Foundation of Functional Programming

CMSC 330: Organization of Programming Languages

Functional Languages. Hwansoo Han

Programming Language Pragmatics

Lambda Calculus-2. Church Rosser Property

Formal Systems and their Applications

Recursive Definitions, Fixed Points and the Combinator

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

Chapter 11 :: Functional Languages

Part I. Historical Origins

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

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

The Untyped Lambda Calculus

Programming Languages

Functional Programming

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

CMSC 330: Organization of Programming Languages

Lambda Calculus and Type Inference

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

Concepts of programming languages

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

Theory and Implementation of a Functional Programming Language

CMSC 330: Organization of Programming Languages

Implementing functional languages with abstract machines

CMSC 330: Organization of Programming Languages. 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...

Lambda Calculus.

CS 6110 S14 Lecture 1 Introduction 24 January 2014

From the λ-calculus to Functional Programming Drew McDermott Posted

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.

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

Computer Science Tripos (Part Ib) Foundations of Functional Programming (Lent Term)

CSCI.4430/6969 Programming Languages Lecture Notes

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

The Untyped Lambda Calculus

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

Introductory Example

Untyped Lambda Calculus

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

CS 242. Fundamentals. Reading: See last slide

1.3. Conditional expressions To express case distinctions like

Chapter 2 The Language PCF

Organization of Programming Languages CS3200/5200N. Lecture 11

Type Systems Winter Semester 2006

(Refer Slide Time: 4:00)

Untyped Lambda Calculus

Programming Language Concepts: Lecture 19

CMSC330. Objects, Functional Programming, and lambda calculus

Foundations of Functional Programming

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

An Introduction to the Lambda Calculus

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 1. ÚVOD DO PŘEDMĚTU, LAMBDA CALCULUS

Programming Languages Third Edition

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Interpretations of Classical Logic Using λ-calculus. Freddie Agestam

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

Notes on evaluating λ-calculus terms and abstract machines

Elixir, functional programming and the Lambda calculus.

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

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

Transcription:

VU Semantik von Programmiersprachen Agata Ciabattoni Institute für Computersprachen, Theory and Logic group (agata@logic.at) (A gentle) Introduction to λ calculus p. 1

Why shoud I studyλcalculus? p. 2

Why shoud I studyλcalculus? 1. Historical importance In 1936 Turing invented the Turing machines Church invented the lambda calculus In 1937 Turing proved that the two models for computation are equivalent. This lead to the Church-Turing thesis The functions computed by algorithms coincide with the class of functions computable by Turing machines. p. 2

Why shoud I studyλcalculus? II 1. Historical importance 2. Useful for studying formal semantics of programming languages very simple (3 formation rules, 2 operations!!!!!!!) powerful effective paradigm to study binding mechanisms, parameter passing,... easy to extend with features of interest plays a similar role for PL research as Turing machines do for computability Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus (Landin 66) p. 3

Why shoud I studyλcalculus? III 1. Historical importance 2. Useful for studying formal semantics of programming languages 3. It s at the core of functional programming languages (LISP, ML..) The design of the imperative languages is based on the von Neumann architecture (they consist of a sequence of statements) Efficiency is the primary concern The design of the functional languages is based on mathematical functions Solid theoretical basis but relatively unconcerned with the architecture p. 4

Recall: Von Neuman architecture One shared memory for instructions (program) and data One data bus and one address bus between processor and memory Instructions and data have to be fetched in sequential order (known as Bottleneck), limiting the operation bandwidth. p. 5

On functional programming languages Functions are first-class objects. They can be passed as arguments to other functions and they can also be returned as results. higher-order way to program (functions can manipulate other functions) every expression denotes a single value the value cannot be changed by evaluating an expression or by sharing it between different parts of the program no references to global data: there is no global data no side effects (some impure functional programming languages do have them, e.g. Lisp, Scheme, ML) Referential transparency: all expressions can be replaced with their value without changing the behavior of a program p. 6

The inventor of λ calculus: Alonso Church (1903-1995) Had a few successful graduate students including Stephen Kleene (Regular expressions) Michael O. Rabin (Nondeterministic automata) Dana Scott (Formal programming language semantics) Alan Turing (Turing Machines) Turing award winners p. 7

λ calculus mathematical formalism can be seen as a core (programming) language, easy and small but still Turing complete p. 8

λ notation...blackboard explanations... p. 9

Multi-argument functions Ex: adding two numbers 3 + 5, in λ notation (λx.λy.x + y)3 5 we partially evaluate λx, resulting in a new function (λy.3+y) this is equivalent to having a λ-binding with multiple arguments this is known as Currying p. 10

Syntax λ-terms are built from a given, countable collection of variables x,y,z... using two operations: λ abstraction: (λx.m) (where x is a variable and M a λ-term) application: (MM ) (where M and M are λ-terms) p. 11

Variables The variable in a term can be computed using the following algorithm varsof x = {x} varsof (M N) = varsof M varsof N varsof (λx.m) = {x} varsof M Note the form of this algorithm: a structural induction. p. 12

λ-terms Notational conventions: (λx 1 x 2...x n.m) means (λx 1.(λx 2...(λx n.m)...) (M 1 M 2...M n ) means (...(M 1 M 2 )...M n ), i.e. application is left-associative drop outermost parentheses and those enclosing the body of a λ-abstraction p. 13

Scope of variables As in all languages with variables it is important to discuss the notion of scope Recall: the scope of a variable (= identifier) is the portion of a program where the identifier is accessible An abstraction λx.m binds the variable x in M. We say that: M is the scope of x x is bound in λx.m just like formal function arguments are bound in the function body p. 14

Free and Bound Variables In λx.m we call x the bound variable and M the body of the λ abstraction. p. 15

Free and Bound Variables In λx.m we call x the bound variable and M the body of the λ abstraction. An occurrence of x in a λ-term M is called binding, if in between λ and. bound if in the body of a binding occurrence of x free if neither binding not bound p. 15

Free and Bound Variables formal definition Sets of free and bound variables FV(x) = {x} FV(λx.M) = FV(M)\{x} FV(MN) = FV(M) FV(N) BV(x) = BV(λx.M) = BV(M) {x} BV(MN) = BV(M) BV(N) If FV(M) =, M is called a closed term, or combinator p. 16

Free and Bound Variables II Static nested scope: the scope of an identifier is fixed at compile-time to be the smallest block (begin/end, function, or procedure body) containing the identifier s declaration. p. 17

Free and Bound Variables II Static nested scope: the scope of an identifier is fixed at compile-time to be the smallest block (begin/end, function, or procedure body) containing the identifier s declaration. Just like in any language with statically nested scoping we have to worry about variable shadowing (= a variable declared within a certain scope has the same name as a variable declared in an outer scope) E.g. An occurrence of a variable might refer to different things in different contexts λx.(xλx.x) p. 17

Renaming bound variables λ-terms that can be obtained from one another by renaming the bound variables are considered identical. This is called α-equivalence. E.g. λx.x is equivalent to λy.y Intuition: by changing the name of a formal argument and of all its occurrences in the function body, the behaviour of the function does not change. Try to rename bound variables so that they are unique: this makes it easy to see the scope of bindings and also prevent confusion! p. 18

α-equivalencem = α M λx.m is intended to represent the function f such that f(x) = M, for all x. p. 19

α-equivalencem = α M λx.m is intended to represent the function f such that f(x) = M, for all x. Hence if M = M[x /x] is the result of taking M and changing all occurrences of x to some variable x that does not appear free in M, then λx.m and λx.m both represent the same function. λx.m α λx.m[x /x] p. 19

λ calculus Operational Semantics p. 20

β-reduction The process of evaluating lambda terms by "plugging arguments into functions". λx.m can be seen as a function on λ-terms via substitution: map each N to M[N/x]. p. 21

β-reduction The process of evaluating lambda terms by "plugging arguments into functions". λx.m can be seen as a function on λ-terms via substitution: map each N to M[N/x]. So the natural notion of computation for λ-terms is given by stepping from a β-redex (λx.m)n to the corresponding β-reduct M[N/x] p. 21

SubstitutionM[N/x] Watch out! We must be careful if the term we are substituting into has a λ inside. x[n/x] = N y[n/x] = y (if x y) (M 1 M 2 )[N/x] = M 1 [N/x]M 2 [N/x] λy.m[n/x] = λy.(m[n/x]) if x y and y FV(N) The side condition makes substitution "capture avoiding". λy.m[n/x] = λy.(m{ y / y }[N/x]) y fresh p. 22

Evaluation and the static scope the definition of substitution (β-reduction) guarantees that evaluation respects stating scoping: (y remains free) (λx.(λy.yx))(y(λx.x)) λz.z(y(λv.v)) if we forget to rename the bound y (λx.(λy.yx))(y(λx.x)) λy.y(y(λv.v)) (y was free before but it is "captured" i.e. it is bound now) p. 23

Structural Operational Semantics Single-step β-reduction: Is the smallest relation β on terms satisfying M β M MN β M N (cong 1 ) (non deterministic semantics) (λx.m)n β M[N/x] (β) M β N λx.m β λx.n (η) N β N MN β M N (cong 2 ) p. 24

On reduction order Languages defined by non-deterministic sets of rules are common: Logic programming languages Expert systems Constraint satisfaction systems... It is useful to know whether such system have the diamond property p. 25

β-reduction andβ-equivalence What it does mean for two λ terms to be computationally equivalent? is the reflexive and transitive closure of β. M M if M reduces to M in zero or more steps. p. 26

β-reduction andβ-equivalence What it does mean for two λ terms to be computationally equivalent? is the reflexive and transitive closure of β. M M if M reduces to M in zero or more steps. M = β N holds if N can be obtained from M performing zero or more steps of α(+η)-reductions, β-reductions and/or inverse reduction steps (i.e. making β symmetric). Def.: = β is the reflexive, symmetric and transitive closure of β, i.e. the smallest equivalence relation containing β. p. 26

Reduction Order The order in which you reduce things can matter. (λx.λy.y)((λz.z)(λz.z)) Two things can be reduced: (λz.z)(λz.z) (λx.λy.y)(...)... with different outcomes! p. 27

Normal Form A lambda term that cannot be β-reduced is in normal form. p. 28

Normal Form A lambda term that cannot be β-reduced is in normal form. 1. Can every λ term be reduced to a normal form? 2. If there is more than one reduction strategy, does each one lead to the same normal form? 3. Is there a reduction strategy that will guarantee that a normal form will be produced (if any)? p. 28

Non-termination (question 1) Some λ terms have no β-nf Ex. Ω := (λx.xx)(λx.xx) p. 29

Diamond Property Church-Rosser Theorem is confluent, that is, if then there exists M such that M 1 M M 2 M 1 M M 2 p. 30

Normal Form (question 2) Def. A λ-term N is in β-normal form (β nf) if it contains no β-redex (i.e. no sub-terms of the form (λx.m)m ). M has β-nf N if M = β N with N a β-nf. p. 31

Normal Form (question 2) Def. A λ-term N is in β-normal form (β nf) if it contains no β-redex (i.e. no sub-terms of the form (λx.m)m ). M has β-nf N if M = β N with N a β-nf. Note that if N is in β-nf and N N, then N = α N. p. 31

Normal Form (question 2) Def. A λ-term N is in β-normal form (β nf) if it contains no β-redex (i.e. no sub-terms of the form (λx.m)m ). M has β-nf N if M = β N with N a β-nf. Note that if N is in β-nf and N N, then N = α N. Corollary: The β-nf of M is unique up to α-equivalence, if it exists....proof on the blackboard p. 31

Evaluation Strategies set of deterministic rules for evaluating expressions in a programming language (for functions) define when and in what order the arguments of a function are evaluated, when they are substituted into the function, and what form that substitution takes. λ calculus very often used to model evaluation strategies (reduction strategies). two basic groups: strict and non-strict, based on how function arguments are handled. p. 32

Evaluation Strategies λcalculus Church Rosser Theorem says that independent of the reduction strategy we will not find more than one normal form But some reduction strategies might fail to find a normal form (question 3) We will consider the strategies: normal order applicative order Def. A β-redex is outermost if there is no β-redex outside it and it is innermost if there is no β-redex inside it. p. 33

Applicative order reduce the leftmost innermost redex first always fully evaluate the arguments of a λ term before evaluating the term itself Call-by-value: functions are only called on values (= variable or lambda abstraction) p. 34

Normal-order reduce the leftmost outermost redex. Call-by-name: the arguments to a function are not evaluated before the function is called and rather, they are substituted directly into the function body and then left to be evaluated whenever they appear in the function. p. 35

Normal-order reduce the leftmost outermost redex. Call-by-name: the arguments to a function are not evaluated before the function is called and rather, they are substituted directly into the function body and then left to be evaluated whenever they appear in the function. Not all λ terms have normal forms, but this a reliable way to find the normal form, if it exists. Church Rosser Theorem II: If M N and N is in normal form then there exists a normal order reduction sequence from M to N. p. 35

Evaluation strategies considerations call-by-name more difficult to implement (must pass unvaluated expressions) the order of evaluation is harder to predict terminates more often than the call-by-value call-by-value easy to implement well-behaved (predictable) w.r.t. side effects Which is better? p. 36

λ calculus Denotational Semantics: give meaning to a language by interpreting its terms as mathematical objects. function (interpretation) from terms to semantic objects p. 37

λ calculus Denotational Semantics: give meaning to a language by interpreting its terms as mathematical objects. function (interpretation) from terms to semantic objects We need a space D such that D is isomorphic to D D (?) D. Scott ( 69) solved this problem by restricting D D to the continous functions on D having a certain topology. p. 37

Computing withλcalculus λ calculus can be seen as a core (programming) language, easy and small but still Turing complete How can we possibly compute with the λ calculus when we have no data to manipulate? 1. no numbers 2. no data-structures 3. no control structures (if-then-else, loops) Answer: Use what we have to build these from scratch! p. 38

Encoding data inλcalculus Computation in λ calculus is given by β-reduction. To relate this to computation in a programming language, or computation in other models we have to see how to encode numbers, pairs, lists... as λ-terms. True and False T := λxy.x F := λxy.y Corollary T F (Church-Rosser theorem!) p. 39

Encoding data inλcalculus Computation in λ calculus is given by β-reduction. To relate this to computation in a programming language, or computation in other models we have to see how to encode numbers, pairs, lists... as λ-terms. True and False T := λxy.x F := λxy.y Corollary T F (Church-Rosser theorem!) Examples of boolean functions and:= λab.abf if-then-else:= λa.a p. 39

λ-definable functions f : N n N is λ-definable if there is a closed λ-term F that represents it: for all (x 1,...x n ) N n and y N if f(x 1,...x n ) = y then Fx 1...x n = β y if f(x 1,...x n ), then Fx 1...x n has no (head) normal form (notation: p stands for the encoding of the number p in λ calculus) Computable = λ-definable Turing machines λ-calculus Recursive functions While language... p. 40

Church s numerals n := λxy.x(x(...(xy)...) }{{} n times Example 0 = λxy.y 1 = λxy.xy 2 = λxy.x(xy) (nm)n = β M(M(...(M N)...) }{{} n times One example: Test 0 := λnpq.n(tq)p p. 41

Bibliography Simple introduction http: //krchowdhary.com/me-tfl/lambda-calculus-chapter5.pdf Selinger, Lecture Notes on the Lambda Calculus http://arxiv.org/pdf/0804.3434v1.pdf (Ch. 1, 2, 3.1, 3.2, 4.1, 4.2) https: //files.nyu.edu/cb125/public/lambda/barendregt.94.pdf Coding in λ calculus, http: //www.utdallas.edu/~gupta/courses/apl/lambda.pdf Barendregt. Introduction to Lambda Calculus. p. 42