Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Similar documents
Lecture 5: The Untyped λ-calculus

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

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages

Introduction to the λ-calculus

CMSC 330: Organization of Programming Languages

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Chapter 5: The Untyped Lambda Calculus

The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages

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

Lambda Calculus. Variables and Functions. cs3723 1

The Untyped Lambda Calculus

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

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

Fundamentals and lambda calculus

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

dynamically typed dynamically scoped

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

Type Systems Winter Semester 2006

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

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

Concepts of programming languages

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

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

CSE 505: Concepts of Programming Languages

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

Formal Systems and their Applications

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

Introduction to the Lambda Calculus

Lambda Calculus. Lecture 4 CS /26/10

The Untyped Lambda Calculus

Harvard School of Engineering and Applied Sciences CS 152: 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...

CIS 500 Software Foundations Fall September 25

Lambda Calculus and Extensions as Foundation of Functional Programming

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

The Untyped Lambda Calculus

Elixir, functional programming and the Lambda calculus.

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

Functional Languages and Higher-Order Functions

Part I. Historical Origins

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CMSC 330: Organization of Programming Languages. Lambda Calculus

CITS3211 FUNCTIONAL PROGRAMMING

Lambda Calculus and Type Inference

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

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

CMSC 330: Organization of Programming Languages

CMSC330. Objects, Functional Programming, and lambda calculus

VU Semantik von Programmiersprachen

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

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Pure Lambda Calculus. Lecture 17

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lambda Calculus. Lambda Calculus

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Functional Programming

Chapter 5: The Untyped Lambda Calculus

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

1 Scope, Bound and Free Occurrences, Closed Terms

Lambda Calculus-2. Church Rosser Property

Programming Languages

Introductory Example

Lambda Calculus as a Programming Language

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

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

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

Recursive Definitions, Fixed Points and the Combinator

Introduction to the Lambda Calculus. Chris Lomont

Fundamental Concepts. Chapter 1

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

(Refer Slide Time: 4:00)

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

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Untyped Lambda Calculus

From the λ-calculus to Functional Programming Drew McDermott Posted

CIS 500 Software Foundations Fall October 2

Lecture Notes on Data Representation

Untyped Lambda Calculus

Lexicografie computationala Feb., 2012

Lambda Calculus as a Programming Language

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

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

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

4.5 Pure Linear Functional Programming

More Lambda Calculus and Intro to Type Systems

301AA - Advanced Programming

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

CSE-321 Programming Languages 2010 Midterm

10.6 Theoretical Foundations

Operational Semantics 1 / 13

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

Transcription:

Formal Semantics Aspects to formalize Why formalize? some language features are tricky, e.g. generalizable type variables, nested functions some features have subtle interactions, e.g. polymorphism and mutable references some aspects often overlooked in informal descriptions, e.g. evaluation order, handling of errors Want a clear and unambiguous specification that can be used by language designers and language implementors (and programmers when necessary) Ideally, would allow rigorous proof of desired language properties, e.g. safety correctness of implementation techniques Syntax: what s a syntactically well-formed program? formalize by a context-free grammar, e.g. in EBNF notation Static semantics: which syntactically well-formed programs are also semantically well-formed? i.e., name resolution, type checking, etc. formalize using typing rules, well-formedness judgments Dynamic semantics: to what does a semantically well-formed program evaluate? i.e., run-time behavior of a type-correct program formalize using operational, denotation, and/or axiomatic semantics rules Metatheory: what are the properties of the formalization itself? e.g., is static semantics sound w.r.t. dynamic semantics? Craig Chambers 164 CSE 505 Craig Chambers 165 CSE 505 Approach Lambda calculus Formalizing & proving properties about a full language is very hard, very tedious many, many cases to consider lots of interacting features The tiniest core of a functional programming language Alonzo Church, 1930s The foundation for all formal study of programming languages Better approach: boil full-sized language down into its essential core, then formalize and study the core cut out much of the complication as possible, without losing the key parts that need formal study hope that insights gained about the core carry over to the full language Can study language features in stages: a very tiny core then extend with an additional feature then extend again (or separately) Outline of study: untyped λ-calculus: syntax, dynamic semantics, properties simply typed λ-calculus: also static semantics, soundness standard extensions to λ-calculus: syntax, dynamic semantics, static semantics polymorphic λ-calculus: syntax, dynamic semantics, static semantics Craig Chambers 166 CSE 505 Craig Chambers 167 CSE 505

Untyped λ-calculus: syntax Free and bound variables Syntax: E ::= λi. E function / abstraction E E call / application I variable [That s it!] Application binds tighter than. Can freely parenthesize as needed Example (with minimum parens): (λx. λy. x y) λz.z λi.e binds I in E An occurrence of a variable I is free in an expression E if it s not bound by some enclosing lambda in E FV(E): set of free variables in E FV(I) = {I} FV(λI.E) = FV(E) - {I} FV(E 1 E 2 ) = FV(E 1 ) FV(E 2 ) FV(E) = E is closed ML analogue (if ignore types): (fn x => (fn y => x y)) (fn z => z) Trees described by this grammar are called term trees Craig Chambers 168 CSE 505 Craig Chambers 169 CSE 505 α-renaming Evaluation, β-reduction First semantic property of λ-calculus: a bound variable in a term tree (and all its references) can be renamed without affecting the semantics of the term tree cannot rename free variables Precise definition: α-equivalence: λi 1.E λi 2.[I 2 /I 1 ]E (if I 2 FV(E)) [E 2 /I]E 1 : substitute all free occurrences of I in E 1 with E 2 (formalized soon) Since names of bound variables don t matter, it s convenient to treat all α-equivalent term trees as a single term define all later semantics for terms can assume that all bound variables are distinct for any particular term tree, do α-renaming to make this so Define how a λ-calculus program runs via a set of rewrite rules, a.k.a. reductions E 1 E 2 means E 1 reduces to E 2 in one step One rule: (λi.e 1 )E 2 [E 2 /I]E 1 applying a function to an argument expression reduces to the function s body after substituting the argument expression for the function s formal this rule is called the β-reduction rule Other rules state that the β-reduction rule can be applied to nested subexpressions, too (formalized later) Define how a λ-calculus program runs to compute a final result as the reflexive, transitive closure of one-step reduction E V means E reduces to result value V (formalized later) That s it! Craig Chambers 170 CSE 505 Craig Chambers 171 CSE 505

Examples Substitution Substitution is suprisingly tricky must avoid changing the meaning of any variable reference, in either substitutee or substituted expressions capture-avoiding substitution Define formally by cases, over the syntax of the substitutee: identifiers: [E 2 /I]I = E 2 [E 2 /I]J = J (if J I) applications: [E 2 /I](E 1 E 3 ) = ([E 2 /I]E 1 ) ([E 2 /I]E 3 ) abstractions: [E 2 /I](λI.E) = λi.e [E 2 /I](λJ.E) = λj.[e 2 /I]E (if J I and J FV(E 2 )) use α-renaming on (λj.e) to ensure J FV(E 2 ) Defines the scoping rules of the λ-calculus Craig Chambers 172 CSE 505 Craig Chambers 173 CSE 505 Normal forms Reduction order E V: E evaluates fully to a value V defined as the reflexive, transitive closure of What is V? an expression with no opportunities for β-reduction such expressions are called normal forms Can define formally: V ::= λi.v I V I (I.e., any E except one containing (λi.e 1 )E 2 somewhere) Q: does every λ-calculus term have a normal form? Q: is a term s normal form unique? Can have several places in an expression where a lambda is applied to an argument each is called a redex (λx.(λy.x) x) ((λz.z) (λw.(λv.v) w)) Therefore, have a choice in what reduction to make next Which one is the right one to choose to reduce next? Does it matter? to the final result? to how long it takes to compute it? to whether the result is computed at all? Craig Chambers 174 CSE 505 Craig Chambers 175 CSE 505

Some possible reduction strategies Amazing fact #1: Church-Rosser Thm., Part 1 (λx.(λy.x) x) ((λz.z) (λw.(λv.v) w)) normal-order reduction: always choose leftmost, outermost redex call-by-name, lazy evaluation: same, and ignore redexes underneath λ applicative-order reduction: always choose leftmost, outermost redex whose argument is in normal form call-by-value, eager evaluation: same, and ignore redexes underneath λ Thm (Confluence). If e 1 e 2 and e 1 e 3, then e 4 s.t. e 2 e 4 and e 3 e 4. e 1 e 2 e 3 e 4 Corollary (Normalization). Every term has a unique normal form, if it exists No matter what reduction order is used! Again, does it matter? to the final result? to how long it takes to compute it? to whether the result is computed at all? Proof? [e.g. by contradiction] Craig Chambers 176 CSE 505 Craig Chambers 177 CSE 505 Existence of normal form? Amazing fact #2: Church-Rosser Thm., Part 2 Does every term have a normal form? (If it does, we already know it s unique) Consider: (λx.x x) (λx.x x) Thm. If a term has a normal form, then normal-order reduction will find it! applicative-order reduction might not! (λx.(λy.y)) ((λz.z z) (λz.z z)) Same example, but using abbreviations: id (λy.y) loop ((λz.z z) (λz.z z)) (λx.id) loop (Abbreviations are not really in the λ-calculus; expand away textually before evaluating) Q: How can I tell whether a term has a normal form? Craig Chambers 178 CSE 505 Craig Chambers 179 CSE 505

Amazing fact #3: λ-calculus is Turing-complete! Multiple arguments, via currying Can translate any Turing machine program into an equivalent λ-calculus program, and vice versa But how? Encode multiple arguments by currying λ(x,y).e λx.(λy.e) E(E 1,E 2 ) (E E 1 ) E 2 λ-calculus lacks: functions with multiple arguments numbers and arithmetic booleans and conditional branches data structures local variables recursive definitions and loops Multiple arguments can be had via a syntactic sugar, so they re not essential, and they can be dropped from the core language All it s got are one-argument, non-recursive functions... Craig Chambers 180 CSE 505 Craig Chambers 181 CSE 505 Church numerals Arithmetic on Church numerals Encode natural numbers using stylized λ terms zero (λs.λz.z) (λs.λz.s 0 z) one (λs.λz.s z) (λs.λz.s 1 z) two (λs.λz.s (s z)) (λs.λz.s 2 z)... N (λs.λz.s N z) A basic arithmetic function: succ succ N N+1 Definition: succ (λn. λs.λz.s (n s z)) (N is the λ-calculus encoding of the mathematical number N) A unary representation of numbers, but one that can be used to do computation a number N is a function that applies a successor function (s) N times to a zero value (z) Examples: succ zero = (λn.λs.λz.s (n s z)) (λs.λz.z ) (λs.λz.s ((λs.λz.z ) s z)) (λs.λz.s ((λz.z ) z)) (λs.λz.s z) = one succ two = (λn.λs.λz.s (n s z)) (λs.λz.s (s z )) (λs.λz.s ((λs.λz.s (s z )) s z)) (λs.λz.s ((λz.s (s z )) z)) (λs.λz.s (s (s z))) = three Craig Chambers 182 CSE 505 Craig Chambers 183 CSE 505

Addition Multiplication Another basic arithmetic function: add add X Y X+Y Another basic arithmetic function: mul mul X Y X*Y Algorithm: to add X to Y, apply succ to Y X times Key trick: X is a function that applies its first argument to its second argument X times a number is as a number does Definition: add (λx.λy.x succ y) add two three = (λx.λy.x succ y) two three two succ three = (λs.λz.s (s z)) succ three succ (succ three) five (pred is tricky, but doable; sub then is similar to add) Craig Chambers 184 CSE 505 Craig Chambers 185 CSE 505 Booleans and conditionals Testing numbers How to make choices? We only have functions... Key idea: true and false are encoded as functions that work differently call the boolean value to control evaluation To complete Peano arithmetic, need an iszero predicate iszero N N=0 Idea: implement by calling the number on a successor function that always returns false and a zero value that is true true (λt.λe.t) false (λt.λe.e) Definition: iszero (λn.n (λx.false) true) if (λb.λt.λe.b t e) if false loop three = (λb.λt.λe.b t e) false loop three false loop three = (λt.λe.e) loop three three Examples: iszero zero = (λn.n (λx.false) true) zero (λs.λz.z ) (λx.false) true true iszero two = (λn.n (λx.false) true) two (λs.λz.s (s z )) (λx.false) true (λx.false) ((λx.false) true) false Craig Chambers 186 CSE 505 Craig Chambers 187 CSE 505

Data structures Local variables E.g., pairs Encode let using functions Idea: a pair is a function that remembers its two parts (via lexical scoping & closures) pair function takes a selector function that s passed both parts and then chooses one pair (λf.λs.λb.b f s) fst (λp.p (λf.λs.f)) snd (λp.p (λf.λs.s)) let I = E 1 in E 2 (λi.e 2 ) E 1 let x = one in let y = two in add x y (λx.(λy.add x y) two) one Examples: pair true four = (λf.λs.λb.b f s) true four (λb.b true four) snd (pair true four) = (λp.p (λf.λs.s)) (p t f) (pair true four) (λf.λs.s) (λb.b true four) (λf.λs.s) (λf.λs.s) true four four Doesn t handle recursive declarations, though: let fact =... fact... in fact two (λfact.fact two) (... fact...) Craig Chambers 188 CSE 505 Craig Chambers 189 CSE 505 Loops and recursion We ve seen that we can write infinite loops in the λ-calculus loop ((λz.z z) (λz.z z)) Can we write useful loops? I.e., can we write recursive functions? Amazing fact #4: Can define recursive functions non-recursively! Step 1: replace the bogus recursive reference with an explicit argument factg (λfact.λn. if (iszero n) one (mul n (fact (pred n)))) The let encoding won t work, as we saw Step 2: use the paradoxical Y combinator to pass factg to itself in a funky way to yield plain fact fact (Y factg) How about this? fact (λn. if (iszero n) one (mul n (fact (pred n)))) Now all we have to do is write Y in the raw λ-calculus Craig Chambers 190 CSE 505 Craig Chambers 191 CSE 505

The Y combinator Example A definition of Y: Y (λf.(λx.f (x x)) (λx.f (x x))) Y fg = (λf.(λx.f (x x)) (λx.f (x x ))) fg (λx.fg (x x)) (λx.fg (x x )) fg ((λx.fg (x x )) (λx.fg (x x )))) = fg (Y fg) So: (Y fg) reduces to a call to fg, whose argument is an expression that, if evaluated inside fg, will reinvoke fg again with the same argument normal-order evaluation will only reduce recursive argument (Y fg) on demand, as needed A concrete example: factg (λfact.λn. if (iszero n) one (mul n (fact (pred n)))) fact (Y factg) (* Y fg fg (Y fg) *) fact two = Y factg two factg (Y factg) two if (iszero two) one (mul two ((Y factg) (pred two))) mul two ((Y factg) (pred two)) [doing some applicative-order reduction, for simplicity] mul two (factg (Y factg) one) mul two (if (iszero one) one (mul one ((Y factg) (pred one)))) mul two (mul one ((Y factg) (pred one))) mul two (mul one (if (iszero zero) one (mul zero...))) mul two (mul one one) two Craig Chambers 192 CSE 505 Craig Chambers 193 CSE 505 Letrec Summary, so far Can now define a recursive version of let: letrec I = E 1 in E 2 let I = Y (λi.e 1 ) in E 2 can now reference I recursively inside E 1 letrec in fact = (λn. if (iszero n) one (mul n (fact (pred n))))... fact... Saw untyped λ-calculus Saw α-renaming, β-reduction rules both relied on capture-avoiding substitution α-renaming defined families of equivalent term trees name choice of formals doesn t matter to semantics β-reduction defined evaluation of a λ-calculus program normal forms: no more β-reduction possible the results of a program reduction strategies such as normal-order & applicative-order had different termination properties, but not different results Church-Rosser: key confluence & normalization thms. Turing-completeness of untyped λ-calculus suggested by successfully encoding many standard PL features Craig Chambers 194 CSE 505 Craig Chambers 195 CSE 505