The lambda calculus (An introduction)

Similar documents
Type systems (An introduction)

Tree-based abstract syntax

Basics of interpretation with Haskell as the metalanguage

Denotational semantics (An introduction)

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

The Untyped Lambda Calculus

The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

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...

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

Lambda Calculi With Polymorphism

Graph reduction. Simple graph reduction with visualisation

Chapter 5: The Untyped Lambda Calculus

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

Type Systems Winter Semester 2006

Lambda Calculi With Polymorphism

The Untyped Lambda Calculus

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

CMSC 330: Organization of Programming Languages

Compilation a primer

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Term rewriting a primer

CSE-321 Programming Languages 2012 Midterm

Fundamental Concepts. Chapter 1

Lecture 5: The Untyped λ-calculus

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

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

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

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.

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Programming Languages Fall 2014

CS 314 Principles of Programming Languages. Lecture 21

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

CMSC 330: Organization of Programming Languages

Programming Languages

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

Lambda Calculus and Type Inference

Introduction to the Lambda Calculus

Programming Languages

Formal Systems and their Applications

CSE-321 Programming Languages 2010 Final

Lab 6: More Steps in Haskell

Chapter 5: The Untyped Lambda Calculus

The Untyped Lambda Calculus

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Getting Started with Haskell (10 points)

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

CMSC 330: Organization of Programming Languages

Graphical Untyped Lambda Calculus Interactive Interpreter

Type Systems COMP 311 Rice University Houston, Texas

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

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

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

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

Typed Lambda Calculus and Exception Handling

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Simply-Typed Lambda Calculus

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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

Untyped Lambda Calculus

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

LECTURE 16. Functional Programming

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

CIS 500 Software Foundations Fall October 2

Type Checking and Type Inference

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

Homework 5 COSE212, Fall 2018

Mutable References. Chapter 1

Untyped Lambda Calculus

Programming Languages Assignment #7

Costly software bugs that could have been averted with type checking

Programming Languages

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

More Lambda Calculus and Intro to Type Systems

Organization of Programming Languages CS3200/5200N. Lecture 11

Denotational Semantics. Domain Theory

Exercise 1 ( = 24 points)

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

Lambda Calculus. Variables and Functions. cs3723 1

More Lambda Calculus and Intro to Type Systems

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

CMSC330. Objects, Functional Programming, and lambda calculus

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CMSC 330: Organization of Programming Languages. Lambda Calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

dynamically typed dynamically scoped

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

CSE505, Fall 2012, Midterm Examination October 30, 2012

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

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

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Transcription:

The lambda calculus (An introduction) Ralf Lämmel Software Language Engineering Team University of Koblenz-Landau http://www.softlang.org/

Dependencies: Tree-based abstract syntax Operational semantics Type systems https://upload.wikimedia.org/wikipedia/commons/f/f9/stop.png

Illustrative lambda expressions Lambda abstraction, i.e., variables abstract over abstract lambda over lambda expressions // Identity function lx. x // Function composition l fgx. f (gx) Variable reference Function application by Function application juxtaposition N.B.: The λ-calculus is essentially a very simple functional programming language.

What s the λ-calculus? The core of functional programming A Turing-complete notion of computability A great example of a calculus in language design A popular starting point in programming language theory I let id = x æ x I let (.) = fgxæ f (g x) I id 42 42 I (not. not) True True N.B.: Haskell is based on the λ-calculus, quite obviously. The concrete syntax of Haskell is obviously slightly di erent from the pro

Call-by-value small-step operational semantics of λ-calculus Reduction of lambda expressions e 1 # e Õ 1 e 1 e 2 # e Õ 1 e 2 [app1] e 2 # e Õ 2 v 1 e 2 # v 1 e Õ 2 ( x. e) v # [v/x]e Substitution [app2] [beta] x. e œ value Values Extra judgment for defining value form(s) [v lambda]

Substitution as needed for beta-reduction Free variables in a lambda expression FREE(x) = {x} FREE(e 1 e 2 )=FREE(e 1 ) fi FREE(e 2 ) FREE( x. e) =FREE(e) \{x} Substitution of x by e within an expression [e/x]x = e [e/x]y = y,if x = y [e/x]e 1 e 2 =([e/x]e 1 )([e/x]e 2 ) [e/x] x. e Õ = x. e Õ [e/x] y. e Õ = y. [e/x]e Õ,if x = y and y/œ FREE(e)

Adding predefined values and operations Abstract syntax Concrete syntax var(x) x lambda(x, e) x. e apply(e 1,e 2 ) e 1 e 2 true true false false zero zero succ(e) succ e pred(e) pred e iszero(e) iszero e if(e 1,e 2,e 3 ) if e 1 then e 2 else e 3 operational semantics of the resulting app N.B.: Thus, BTL Basic TAPL Language (referring again to Pierce s textbook Types and programming languages ) is added to the λ-calculus.

If we only had recursive functions add = n. m. if iszero n then m else succ (add (pred n) m) mul = n. m. if iszero n then zero else add m (mul (pred n) m) factorial = n. if iszero n then succ zero else mul n (factorial (pred n)) However, recursive definitions like this are not expressible in the lambd N.B.: Lambda-abstractions allow us to abstract over λ-expressions (i.e., functions), but there is no form of self-reference.

Recursion by means of a fixed-point combinator add = fix ( f. n. m. if iszero n then m else succ (f (pred n) m)) mul = fix ( f. n. m. if iszero n then zero else add m (f (pred n) m)) factorial = fix ( f. n. if iszero n then succ zero else mul n (f (pred n))) Thus, we take a fixed point of lambda expressions with an extra argument e # e Õ [fix1] fix e # fix e Õ fix x. e # [fix x. e/x]e [fix2] N.B.: We parametrize in the recursive reference (i.e., f above) and we tie the recursive knot with the fix-construct.

Turing completeness Value/Operation Encoding true t. f. t false t. f. f if b. v. w. b v w 0 s. z. z 1 s. z. s z 2 s. z. s (s z) 3 s. z. s (s (s z))...... succ n. s. z. s (n sz) fix = f.( x.f ( v.((xx)v))) ( x.f ( v.((xx)v))) nding this combinator properly or deriving it ration N.B.: None of the extensions discussed thus far are essential.

A realization of the λ-calculus: ULL Untyped Lambda Language add = Fix (Lambda "f" (Lambda "n" (Lambda "m" (If (IsZero (Var "n")) (Var "m") (Succ (Apply (Apply (Var "f") (Pred(Var"n"))) (Var "m")))))))... Convert Haskell Int to expression such that isnat is True fromint :: Int æ Expr fromint i i >= 0 = if i==0then Zero else Succ (fromint (i 1)) Convert expression such that isnat is True to Haskell Int toint :: Expr æ Int toint Zero = 0 toint (Succ e) = toint e + 1 I let (Just r) = steps (Apply (Apply add (fromint 20)) (fromint 22)) I toint r 42 N.B.: For convenience s sake regarding the use of natural numbers That is, we apply the recursive function add to 20 and 22 (after converting n

Type system of the simply typed λ-calculus x : T 2 G G ` x : T [t var] G ` e 1 : T 1! T 2 G ` e 2 : T 1 G ` e 1 e 2 : T 2 [t apply] G,x : T 1 ` e : T 2 [t abstr] G ` lx : T 1. e : T 1! T 2 Type annotation N.B.: An extra type context maintains the types of variables as declared by the annotations of the λ-abstractions.

A realization of the simply typed λ-calculus: TLL Typed Lambda Language add = Fix (Lambda "f" (FunType NatType (FunType NatType NatType)) (Lambda "n" NatType (Lambda "m" NatType (If (IsZero (Var "n")) (Var "m") (Succ (Apply (Apply (Var "f") (Pred(Var"n"))) (Var "m"))))))) I typeof empty add Just (FunType NatType (FunType NatType NatType)) That is, add is a function that takes two arguments of the NatType and

A trivial form of type erasure x x [e var] e 1 e Õ 1 e 2 e Õ 2 e 1 e 2 e Õ 1 eõ 2 [e apply] e e Õ x : T. e x. e Õ [e abstr] N.B.: As a consequence, we can leverage directly the semantics of the untyped calculus. We could also aim at inferring the types of the variables instead of relying on type annotations.

Online resources YAS GitHub repository contains all code. YAS (Yet Another SLR (Software Language Repository)) http://www.softlang.org/yas See Haskell-based implementations of languages ULL and TLL. The Software Languages Book http://www.softlang.org/book The book discusses also polymorphism.