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

Similar documents
CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

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

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.

CMSC 330: Organization of Programming Languages

CIS 500 Software Foundations Midterm I

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

CMSC 330: Organization of Programming Languages

Chapter 5: The Untyped Lambda Calculus

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

Formal Systems and their Applications

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

Graphical Untyped Lambda Calculus Interactive Interpreter

CSE-321 Programming Languages 2010 Midterm

Chapter 5: The Untyped Lambda Calculus

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

CSE 505: Concepts of Programming Languages

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Concepts of programming languages

CMSC 330: Organization of Programming Languages

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

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

Untyped Lambda Calculus

Introduction to the Lambda Calculus

Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330: Organization of Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

dynamically typed dynamically scoped

Lambda Calculus.

More Lambda Calculus and Intro to Type Systems

CMSC 330: Organization of Programming Languages

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

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Introduction to the λ-calculus

More Lambda Calculus and Intro to Type Systems

CMSC 330: Organization of Programming Languages. Lambda Calculus

CS 6110 S14 Lecture 1 Introduction 24 January 2014

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

Denotational Semantics. Domain Theory

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

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

CSE-321 Programming Languages 2012 Midterm

λ calculus is inconsistent

Lecture 5: The Untyped λ-calculus

Lambda Calculus. Lecture 4 CS /26/10

The lambda calculus (An introduction)

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

CSE-321 Programming Languages 2010 Final

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Elixir, functional programming and the Lambda calculus.

Costly software bugs that could have been averted with type checking

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

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

The Untyped Lambda Calculus

Programming Languages Fall 2014

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

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

The Substitution Model

CMSC330. Objects, Functional Programming, and lambda calculus

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

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

CS 11 Haskell track: lecture 1

Denotational semantics

Lambda Calculus and Extensions as Foundation of Functional Programming

Functional Programming

Functional Languages. Hwansoo Han

Programming Languages

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

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

1 Introduction. 3 Syntax

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

CIS 500 Software Foundations Fall October 2

Lecture Notes on Data Representation

From the λ-calculus to Functional Programming Drew McDermott Posted

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

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

The Untyped Lambda Calculus

Functional Programming

Exercise 1 (2+2+2 points)

The Substitution Model. Nate Foster Spring 2018

The Untyped Lambda Calculus

Lecture 9: Typed Lambda Calculus

Lambda Calculus LC-1

Lambda Calculus. Lambda Calculus

Fundamental Concepts. Chapter 1

1 Scope, Bound and Free Occurrences, Closed Terms

Transcription:

1 LAMBDA CALCULUS (untyped)

2 The Untyped Lambda Calculus (λ) Designed by Alonzo Church (1930s) Turing Complete (Turing was his doctoral student!) Models functions, always as 1-input Definition: terms, values, and evaluation t ::= x λ x. t (t t) v ::= λ x. t Notes terms t are variables, lambdas, or applications only lambdas are values. this language is untyped!

3 λ : Evaluation Semantics Evaluation: applying these rules to simplify your term until you have a value (no more evaluation possible). E-App1 t 1 t 1 ' (t 1 t 2 ) (t 1 ' t 2 ) E-App2 t t' (v t) (v t') E-App-Abs ((λ x. t) v) t[x v] E-App1: "If I know that a term t 1 can be evaluated to t 1 ', then when I've got an application (t 1 t 2 ), I can evaluate just t 1 to t 1 ', and then I've got (t 1 ' t 2 ) left over." -------------------------------------------------- E-App2: "When an application has a value first and a term second, and that term can evaluate further, we are allowed to evaluate that second term." -------------------------------------------------- E-App-Abs: "When a lambda term is applied to a value v, it can simplify down to the body of the lambda, t, with all occurrences of the lambda parameter x being replaced with the argument v. We don't need any simplifications available (nothing above the line)."

4 Sample Expressions Consider each expression. Are they already values? If not, show each reduction, and name the rule used. ((λ x. x + 1) 3) (λ e. e + 1) ((λ z. z * z) 5) (((λa. (λb. a)) 10) 20) ( (λa. a*a) ((λx. x + 1) 6) ) ( ( (λx. (λy. x y + 1)) 10) 6) ( (λx. (λy. x y + 1)) 10)

5 Think in Trees Drawing out an abstract syntax tree for terms can help understand them: (((λa. (λb. a)) 10) 20) @ @ 20 λa 10 λb a

6 λ : Evaluation Rules as Trees E-App1 t 1 t 1 ' @ @ t 1 t 2 t 1 ' t 2 E-App2 t 2 t 2 ' @ @ v t 2 v t 2 '

7 λ : Evaluation Rules as Trees E-App-Abs @ λx v t[x v] t

8 Reading Expressions the body of a lambda expression grabs as much as it can (it reads through until it hits a close-parenthesis it didn't open, or the end of the expression). We can remove some parentheses for slightly easier reading. These are equivalent: (λx. (λy. x+y)) λx. λy. x+y These are not: ((λx. x+1) 3) (λx. x+1 3)

9 Extending λ we will add more terms and values, to have more primitives in our language. The core lambda calculus is actually pretty painful/nearly useless on its own (we already sneaked numbers/ops in!) Adding Booleans t ::= true false if t t t v ::= true false

10 Extending evaluation: Booleans Add these rules: E-if t 1 t 1 ' (if t 1 t 2 t 3 ) (if t 1 ' t 2 t 3 ) E-if-true (if true t 2 t 3 ) t 2 E-if-false (if false t 2 t 3 ) t 3

11 Sample Expressions - Booleans Consider each expression. Are they already values? If not, show each reduction, and name the rule used. Show what happens in each step. (if true 5 10) (if false true 20) (if true 5 (3/0)) (λ x. if x 3 6) ((λ x. if x 4 7) false) ((λ z. if true z 9) 5) (((λ a. (λ b. if b 100 200)) (2+3)) (if (a>4) true false))

12 Using Definitions We can name expressions and use them. Using these definitions: definition: definition: not = (λ x. if x false true) and = (λ a. (λ b. if a b false)) Simplify these expressions. (not true) ((λ x. x and true) true) ((λ x. x and true) false) Define these: or xor

13 Extension: Natural Numbers We create our own numbers as zero, successor of a number, and predecessor of a number. Adding Naturals t ::= zero succ t pred t v ::= zero succ v

14 Extending evaluation: Naturals E-succ t 1 t 1 ' succ t 1 succ t 1 ' E-pred t 1 t 1 ' pred t 1 pred t 1 ' E-pred-succ pred(succ t) t

15 Extension: Pairs We add primitive support for paired values. Adding Pairs t ::= pair t t fst t snd t v ::= pair v v E-pair1 t 1 t 1 ' pair t 1 t 2 pair t 1 ' t 2 E-pair2 t 2 t 2 ' pair v t 2 pair v t 2 '

16 Extending evaluation: Pairs E-fst t t' fst t fst t' E-snd t t' snd t snd t' E-pair-fst fst (pair t 1 t 2 ) t 1 E-pair-snd snd (pair t 1 t 2 ) t 2

17 Encoding Values Without any extensions, we can still represent some simple values. we used numbers and mathematical operators on the previous slide, but those aren't actually in our core language! Each value shall be represented as a higher-order function Note, we will much prefer extending the core language!

18 Encoding Boolean values/operations Start again with only the core untyped lambda calculus no true/false values, no if-term, no E-Bool rules. Encoding 'true' and 'false' as functions: true = λ x. λ y. x false = λ x. λ y. y Operator encodings not = λ a. (a false) true and = λ a. (λ b. ((a b) a)) or = λ a. (λ b. ((a a) b)) if = λ b. (λ t. (λ e. ((b t) e))) note: all ()'s are optional on this slide

19 Implementing the untyped lambda calculus Investigate implementing the untyped lambda calculus in Haskell: data for our terms is_value function to check if a term is a value eval function to perform evaluation needs substitution capability (subst function) Until we extend our language, it'll seem ungainly the only values we have are functions! no booleans, numbers, nothing. see ULC_bools_nums.hs, which includes booleans and numbers.

20 Formal Language extension recipe Create more terms t::= both constructors and observers add some new terms (if any) to be values v::= add more evaluation rules enough that all "proper" terms can become values observers will inspect/consume constructors

21 Coded Language extension recipe (implementing in Haskell) add to datatype Tm (extend terms) add cases to is_val (extend the values) add cases to eval, subst, etc. language is extended! What other features can we add?

22 Aside: Evaluation Strategy As written, our evaluation rules require that functions' arguments are evaluated first: E-App-Abs ((λ x. t) v) t[x v] We could have implemented lazy evaluation (and removed E-App2): E-App-Abs-lazy ((λ x. t 1 ) t 2 ) t 1 [x t 2 ]

23 Evaluation Strategy How/where does the chosen evaluation strategy affect: your implementation? Your language usage?

24 Building an Interpreter Start with simple core features define terms, values, evaluation expand with more features Implementation choice: Domain Specific Language (DSL) a language designed/dedicated to one task or domain of knowledge write all tools, e.g. parser/compiler Embedded DSL (EDSL) DSL that is implemented as a library directly in some other language. All of the host's features are directly available: we're actually writing code in the host language that heavily uses the library definitions we're exploring an EDSL.

25 Choosing a value space We can choose any type that's already available in the host language, like Int. every single expression must result in a value of this type! see ExprLang1.hs We can make our own data type for the value space see ExprLang2.hs

26 Choosing evaluation semantics Once we include some notion of functions in our code, we can then choose calling conventions. how can we introduce functions? where do declarations go? what kinds of declarations are allowed? (recursive?) we can implement any evaluation strategy, such as pass-by-value, pass-by-name, simply by changing our eval definition.

27 Fun diversion The ω-combinator always diverges. ω = (λ x. x x) (λ x. x x) Try performing the application. What do you get?

28 Representing Recursion In the untyped lambda calculus, we can represent recursion directly or with a language extension. (see ycomb.txt) The y combinator ycomb = \f. ( (\x. (f (\y. x x y))) (\x. (f (\y. x x y))) ) evenf = Lam "self" $ Lam "n" $ If (Equal vn (Num 0)) Tru $ If (Equal vn (Num 1)) Fls $ App (Var "self") (Sub vn (Num 2)) personally, I prefer extending the language. The y-combinator is a real headache to watch in action! iseven = App ycomb evenf

29 Providing primitive recursion We can provide primitive definitions for recursion. Adding Fix t ::= fix t E-fix fix (λ x. t) t [x (fix (λ x. t))]

30 Other features How might we introduce each of the following? case statements let expressions records abstract data types variable assignments classes and objects types type inference What else would you want to add to your language?

31 Valuable resource To get a much more thorough treatment of writing interpreters for more advanced language features, look for this book: Types and Programming Languages, by Benjamin Pierce. you can view it electronically through our library's website for free!