CS 6110 S14 Lecture 1 Introduction 24 January 2014

Similar documents
CS 6110 S16 Lecture 1 Introduction 27 January 2016

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

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

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

Introduction to the Lambda Calculus

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

Programming Languages Third Edition

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

1 Introduction. 3 Syntax

Lexicografie computationala Feb., 2012

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

Introductory Example

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

CS 242. Fundamentals. Reading: See last slide

CITS3211 FUNCTIONAL PROGRAMMING

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Introduction to the Lambda Calculus. Chris Lomont

Introduction to lambda calculus Part 3

CIS 500 Software Foundations Fall September 25

Handout 10: Imperative programs and the Lambda Calculus

Formal Systems and their Applications

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

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

CMSC 330: Organization of Programming Languages

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

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

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

Type Systems Winter Semester 2006

Fundamentals and lambda calculus

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

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

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

Pure Lambda Calculus. Lecture 17

Concepts of programming languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

1 Scope, Bound and Free Occurrences, Closed Terms

Lambda Calculus. Lecture 4 CS /26/10

Elixir, functional programming and the Lambda calculus.

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

Lecture 9: Typed Lambda Calculus

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

Introduction to the λ-calculus

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

Recursive Definitions, Fixed Points and the Combinator

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

CMSC330. Objects, Functional Programming, and lambda calculus

CSE 505: Concepts of Programming Languages

This book is licensed under a Creative Commons Attribution 3.0 License

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

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

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

Lambda Calculus see notes on Lambda Calculus

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

CMSC 330: Organization of Programming Languages. Lambda Calculus

Denotational semantics

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

Functional Languages. Hwansoo Han

Functional Programming. Big Picture. Design of Programming Languages

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.

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

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

Functional Programming

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

A Small Interpreted Language

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

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

Chapter 11 :: Functional Languages

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

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

Lambda Calculus. Variables and Functions. cs3723 1

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

Organization of Programming Languages CS3200/5200N. Lecture 11

Lecture 2: Big-Step Semantics

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

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

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.

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

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

Programming Language Concepts: Lecture 19

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Lambda Calculus.

2.2 Syntax Definition

CSE 12 Abstract Syntax Trees

Denotational Semantics. Domain Theory

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Functional Programming

Lambda Calculus LC-1

Chapter 5: The Untyped Lambda Calculus

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Types and Static Type Checking (Introducing Micro-Haskell)

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Types and Static Type Checking (Introducing Micro-Haskell)

Semantics of programming languages

Transcription:

CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions in a program must be interpreted somehow, and a program s behavior depends on how they are interpreted. We must have a good understanding of this interpretation, otherwise it would be impossible to write programs that do what is intended. It may seem like a straightforward task to specify what a program is supposed to do when it executes. After all, basic instructions are pretty simple. But in fact this task is often quite subtle and difficult. Programming language features often interact in ways that are unexpected or hard to predict. Ideally it would seem desirable to be able to determine the meaning of a program completely by the program text, but that is not always true, as you well know if you have ever tried to port a C program from one platform to another. Even for languages that are nominally platform-independent, meaning is not necessarily determined by program text. For example, consider the following Java fragment. class A { static int a = B.b + 1; } class B { static int b = A.a + 1; } First of all, is this even legal Java? Yes, although no sane programmer would ever write it. So what happens when the classes are initialized? A reasonable educated guess might be that the program goes into an infinite loop trying to initialize A.a and B.b from each other. But no, the initialization terminates with initial values for A.a and B.b. So what are the initial values? Try it and find out, you may be surprised. Can you explain what is going on? This simple bit of pathology illustrates the difficulties that can arise in describing the meaning of programs. Luckily, these are the exception, not the rule. Programs describe computation, but they are more than just lists of instructions. They are mathematical objects as well. A programming language is a logical formalism, just like first-order logic. Such formalisms typically consist of Syntax, a strict set of rules telling how to distinguish well-formed expressions from arbitrary sequences of symbols; and Semantics, a way of interpreting the well-formed expressions. The word semantics is a synonym for meaning or interpretation. Although ostensibly plural, it customarily takes a singular verb. Semantics may include a notion of deduction or computation, which determines how the system performs work. In this course we will see some of the formal tools that have been developed for describing these notions precisely. The course consists of three major components: Dynamic semantics methods for describing and reasoning about what happens when a program runs. Static semantics methods for reasoning about programs before they run. Such methods include type checking, type inference, and static analysis. We would like to find errors in programs as early as possible. By doing so, we can often detect errors that would otherwise show up only at runtime, perhaps after significant damage has already been done. Language features applying the tools of dynamic and static semantics to study actual language features of interest, including some that you may not have encountered previously. 1

We want to be as precise as possible about these notions. Ideally, the more precisely we can describe the semantics of a programming language, the better equipped we will be to understand its power and limitations and to predict its behavior. Such understanding is essential not only for writing correct programs, but also for building tools like compilers, optimizers, and interpreters. Understanding the meaning of programs allows us to ascertain whether these tools are implemented correctly. But the very notion of correctness is subject to semantic interpretation. It should be clear that the task before us is inherently mathematical. Initially, we will characterize the semantics of a program as a function that produces an output value based on some input value. Thus, we will start by presenting some mathematical tools for constructing and reasoning about functions. 1.1 Binary Relations and Functions Denote by A B the set of all ordered pairs (a, b) with a A and b B. A binary relation on A B is just a subset R A B. The sets A and B can be the same, but they do not have to be. The set A is called the domain and B the codomain (or range) of R. The smallest binary relation on A B is the null relation consisting of no pairs, and the largest binary relation on A B is A B itself. The identity relation on A is {(a, a) a A} A A. An important operation on binary relations is relational composition R ; S = {(a, c) b (a, b) R (b, c) S}, where the codomain of R is the same as the domain of S. A (total) function (or map) is a binary relation f A B in which each element of A is associated with exactly one element of B. If f is such a function, we write: f : A B In other words, a function f : A B is a binary relation f A B such that for each element a A, there is exactly one pair (a, b) f with first component a. There can be more than one element of A associated with the same element of B, and not all elements of B need be associated with an element of A. The set A is the domain and B is the codomain or range of f. The image of f is the set of elements in B that come from at least one element in A under f: f(a) = {x B x = f(a) for some a A} = {f(a) a A}. The notation f(a) is standard, albeit somewhat of an abuse. The operation of functional composition is: if f : A B and g : B C, then g f : A C is the function (g f)(x) = g(f(x)). Viewing functions as a special case of binary relations, functional composition is the same as relational composition, but the order is reversed in the notation: g f = f ; g. A partial function f : A B (note the shape of the arrow) is a function f : A B defined on some subset A A. The notation dom f refers to A, the domain of f. If f : A B is total, then dom f = A. A function f : A B is said to be one-to-one (or injective) if a b implies f(a) f(b) and onto (or surjective) if every b B is f(a) for some a A. 2

1.2 Representation of Functions Mathematically, a function is equal to its extension, which is the set of all its (input, output) pairs. One way to describe a function is to describe its extension directly, usually by specifying some mathematical relationship between the inputs and outputs. This is called an extensional representation. Another way is to give an intensional 1 representation, which is essentially a program or evaluation procedure to compute the output corresponding to a given input. The main differences are there can be more than one intensional representation of the same function, but there is only one extension; intensional representations typically give a method for computing the output from a given input, whereas extensional representations need not concern themselves with computation (and seldom do). A central issue in semantics and a good part of this course is concerned with how to go from an intensional representation to a corresponding extensional representation. 2 The λ-calculus The λ-calculus (λ = lambda, the Greek letter l) 2 was introduced by Alonzo Church (1903 1995) and Stephen Cole Kleene (1909 1994) in the 1930s to study the interaction of functional abstraction and functional application. The λ-calculus provides a succinct and unambiguous notation for the intensional representation of functions, as well as a general mechanism based on substitution for evaluating them. The λ-calculus forms the theoretical foundation of all modern functional programming languages, including Lisp, Scheme, Haskell, OCaml, and Standard ML. One cannot understand the semantics of these languages without a thorough understanding of the λ-calculus. It is common to use λ-notation in conjunction with other operators and values in some domain (e.g. λx. x+2), but the pure λ-calculus has only λ-terms and only the operators of functional abstraction and functional application, nothing else. In the pure λ-calculus, λ-terms act as functions that take other λ-terms as input and produce λ-terms as output. Nevertheless, it is possible to code common data structures such as Booleans, integers, lists, and trees as λ-terms. The λ-calculus is computationally powerful enough to represent and compute any computable function over these data structures. It is thus equivalent to Turing machines in computational power. 2.1 Syntax The following is the syntax of the pure untyped λ-calculus. Here pure means there are no constructs other than λ-terms, and untyped means that there are no restrictions on how λ-terms can be combined to form other λ-terms; every well-formed λ-term is considered meaningful. A λ-term is defined inductively as follows. Let Var be a countably infinite set of variables x, y,.... Any variable x Var is a λ-term. 1 Note the spelling: intensional and intentional are not the same! 2 Why λ? To distinguish the bound variables from the unbound (free) variables, Church placed a caret on top of the bound variables, thus λx. x + yx 2 was represented as x. x + yx 2. Apparently, the printers could not handle the caret, so it moved to the front and became a λ. 3

If e is a λ-term, then so is λx. e (functional abstraction). If e 1 and e 2 are λ-terms, then so is e 1 e 2 (functional application). We usually omit the application operator, writing e 1 (e 2 ), (e 1 e 2 ), or even e 1 e 2 for e 1 e 2. Intuitively, this term represents the result of applying of e 1 as a function to e 2 as its input. The term λx. e represents a function with input parameter x and body e. 2.2 Examples A term representing the identity function is id = λx. x. The term λx. λa. a represents a function that ignores its argument and return the identity function. This is the same as λx. id. The term λf. fa represents a function that takes another function f as an argument and applies it to a. Thus we can define functions that can take other functions as arguments and return functions as results; that is, functions are first-class values. The term λv. λf. fv represents a function that takes an argument v and returns a function λf. fv that calls its argument some function f on v. A function that takes a pair of functions f and g and returns their composition g f is represented by λf. λg. λx. g(fx). We could define the composition operator this way. In the pure λ-calculus, every λ-term represents a function, since any λ-term can appear on the left-hand side of an application operator. 2.3 BNF Notation Backus Naur form (BNF) is a kind of grammar used to specify the syntax of programming languages. It is named for John Backus (1924 2007), the inventor of Fortran, and Peter Naur (1928 ), the inventor of Algol 60. We can express the syntax of the pure untyped λ-calculus very concisely using BNF notation: e ::= x e 1 e 2 λx. e Here the e is a metavariable representing a syntactic class (in this case λ-terms) in the language. It is not a variable at the level of the programming language. We use subscripts to differentiate metavariables of the same syntactic class. In this definition, e 0, e 1 and e all represent λ-terms. The pure untyped λ-calculus has only two syntactic classes, variables and λ-terms, but we shall soon see other more complicated BNF definitions. 2.4 Other Domains The λ-calculus can be used in conjunction with other domains of primitive values and operations on them. Some popular choices are the natural numbers N = {0, 1, 2,...} and integers Z = {..., 2, 1, 0, 1, 2,...} along with the basic arithmetic operations +, and tests =,, <; and the two-element Boolean algebra 2 = {false, true} along with the basic Boolean operations (and), (or), (not), and (implication). 3 3 The German mathematician Leopold Kronecker (1823 1891) was fond of saying, God created the natural numbers; all else is the work of Man. Actually, there is not much evidence that God created N. But for 2, there is no question: And the earth was without form, and void... And God said, Let there be light... And God divided the light from the darkness... Genesis 1 : 2 4 4

The λ-calculus gives a convenient notation for describing functions built from these objects. We can incorporate them in the language by assuming there is a constant for each primitive value and each distinguished operation, and extending the definition of term accordingly. This allows us to write expressions like λx. x 2 for the squaring function on integers. In mathematics, it is common to define a function f by describing its value f(x) on a typical input x. For example, one might specify the squaring function on integers by writing f(x) = x 2, or anonymously by x x 2. Using λ-notation, we would write f = λx. x 2 or λx. x 2, respectively. 2.5 Abstract Syntax and Parsing Conventions The BNF definition above actually defines the abstract syntax of the λ-calculus; that is, we consider a term to be already parsed into its abstract syntax tree. In the text, however, we are constrained to use sequences of symbols to represent terms, so we need some conventions to be sure that they are read unambiguously. We use parentheses to show explicitly how to parse expressions, but we also assign a precedence to the operators in order to save parentheses. Conventionally, function application is higher precedence (binds tighter) than λ-abstraction; thus λx. x λy. y should be read as λx. (x λy. y), not (λx. x) (λy. y). If you want the latter, you must use explicit parentheses. Another way to view this is that the body of a λ-abstraction λx.... extends as far to the right as it can it is greedy. Thus the body is delimited on the right only by a right parenthesis whose matching left parenthesis is to the left of the λx, or by the end of the entire expression. Another convention is that application is left-associative, which means that e 1 e 2 e 3 (e 1 e 2 ) e 3. If you want e 1 (e 2 e 3 ), you must use parentheses. should be read as It never hurts to include parentheses if you are not sure. 2.6 Terms and Types Typically, programming languages have two different kinds of expressions: terms and types. We have not talked about types yet, but we will soon. A term is an expression representing a value; a type is an expression representing a class of similar values. The value represented by a term is determined at runtime by evaluating the term; its value at compile time may not be known. Indeed, it may not even have a value if the evaluation does not terminate. On the other hand, types can be determined at compile time and are used by the compiler to rule out ill-formed terms. When we say a given term has a given type (for example, λx.x 2 has type Z Z), we are saying that the value of the term after evaluation at runtime, if it exists, will be a member of the class of similar values represented by the type. In the pure untyped λ-calculus, there are no types, and all terms are meaningful. 2.7 Multi-Argument Functions and Currying We would like to allow multiple arguments to a function, as for example in (λ(x, y). x + y) (5, 2). However, we do not need to introduce a primitive pairing operator to do this. Instead, we can write (λx. λy. x + y) 5 2. That is, instead of the function taking two arguments and adding them, the function takes only the first argument and returns a function that takes the second argument and then adds the two arguments. The 5

notation λx 1... x n. e is considered an abbreviation for λx 1. λx 2. λx 3.... λx n. e. Thus we consider the multiargument version of the λ-calculus as just syntactic sugar. The desugaring transformation λx 1... x n. e λx 1. λx 2. λx n. e e 0 (e 1,..., e n ) e 0 e 1 e 2 e n for this particular form of sugar is called currying after Haskell B. Curry (1900 1982). 3 Preview Next time we will discuss capture-avoiding (safe) substitution and the computational rules of the λ-calculus, namely α-, β-, and η-reduction. This is the calculus part of the λ-calculus. References [1] H. P. Barendregt. The Lambda Calculus, Its Syntax and Semantics. North-Holland, 2nd edition, 1984. 6