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

Similar documents
CIS 500 Software Foundations Fall October 2

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

Type Systems Winter Semester 2006

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

Lambda Calculus. Lecture 4 CS /26/10

Chapter 5: The Untyped Lambda Calculus

CIS 500 Software Foundations Fall September 25

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

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

Concepts of programming languages

Lecture 5: The Untyped λ-calculus

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

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

Costly software bugs that could have been averted with type checking

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

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

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

The Untyped Lambda Calculus

Programming Languages

Lecture 9: Typed Lambda Calculus

Programming Languages Fall 2014

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

CIS 500 Software Foundations Midterm I

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Introduction to the λ-calculus

1 Scope, Bound and Free Occurrences, Closed Terms

Introduction to the Lambda Calculus

Fundamentals and lambda calculus

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

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

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus and Type Inference

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Lambda Calculus. Variables and Functions. cs3723 1

Pure Lambda Calculus. Lecture 17

Untyped Lambda Calculus

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

Untyped Lambda Calculus

An Introduction to the Lambda Calculus

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

1 Introduction. 3 Syntax

Lambda Calculus and Extensions as Foundation of Functional Programming

Lambda Calculi With Polymorphism

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

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

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

3.1 λ-calculus: Syntax

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

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.

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

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

VU Semantik von Programmiersprachen

Lambda Calculus.

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

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

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

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

CMSC330. Objects, Functional Programming, and lambda calculus

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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

Lambda Calculi With Polymorphism

dynamically typed dynamically scoped

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

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

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

Lambda Calculus and Type Inference

CSE-321 Programming Languages 2012 Midterm

Lecture Notes on Data Representation

Variables. Substitution

1.3. Conditional expressions To express case distinctions like

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSE-321 Programming Languages 2010 Midterm

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

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Transcription:

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

PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety = Progress + Preservation 1

OVERVIEW Motivation Lambda Calculus Putting λ to work Simple Types for Simple Calculus 2

Motivation

WHAT IS THE CALCULUS? Toy language Simple, Powerful and Formal Allows to play with advanced features formally Saves headache of dealing with the Real World Usually typable 3

EXISTING CALCULI Record calculus Pi-calculus Lambda-calculus Object calculus µabc calculus... 4

Lambda Calculus

LAMBDA OVERVIEW Everything is a function Very simple evaluation Very simple basic typing Structural types are easy Object-oriented types are harder 5

LAMBDA SYNTAX t ::= x λx.t t t terms: variables abstractions applications Where x Var, a countable set of variables 6

BRACKET AND SHORTHAND EXAMPLES Shorthand notation on the left: λx.x = (λx.x) x y z = ((x y) z) λx.x y = (λx.(x y)) (λx.x) y = ((λx.x) y) outer brackets left associate λ is greedy λ is greedy (λx.x x)λy.y y = ((λx.(x x))(λy.(y y))) 7

REDEX & BETA REDUCTION Redex is an application of an abstraction: (λx.x) y x ((λx.x) y) ((λx.x) y) ((λx.x) y) ((λx.x) y) ((λx.x) y) Redex is evaluated by substitution: (λx.t 1 ) t 2 t 1 [x t 2 ] 8

SUBSTITUTION Search & Replace (aka Tretjakov isomorphism): (λx.x) y = x[x y] = y x ((λx.x) y) = x (x[x y]) = x y (λx.x y) λz.z = (x y)[x λz.z] = (λz.z) y = z[z y] = y (λx.y) λz.z = y[x λz.z] = y 9

THE FEMALE INTUITION Lambda abstraction captures functions (not a valid definition): factorial = λn.if n = 0 then 1 else n factorial(n 1) Some common functions: id = λx.x const = λx.λy.x Further on we will use macros for brevity. 10

TERMINATION What does Ω evaluate to? ω = λx.x x Ω = ω ω 11

TERMINATION What does Ω evaluate to? ω = λx.x x Ω = ω ω = (λx.x x) ω x x[x ω] = ω ω = Ω Ladies and Gentlemen the halting problem 11

EVALUATION ORDER What to do if the term has several redexes? Full beta reduction Reduce any redex Normal order Evaluate functions before values Call-by-name Normal order, where evaluation under λ is forbidden Call-by-value Evaluate values before functions Further on we use call-by-value as in most real languages. 12

LAMBDA SEMANTICS Values (also a syntactical category): v ::= λx.t Evaluation rules: E-APP1 t 1 t 1 t 1 t 2 t 1 t 2 E-APP2 t 2 t 2 v 1 t 2 v 1 t 2 values: abstraction value E-APPABS (λx.t) v [x v]t No evaluation under λ-abstraction Redex reduction only over values Left-to-right order of evaluation 13

FREE VARIABLES Free variables are those not bound by λ-abstraction: FV(x) = x FV(λx.t 1 ) = FV(t 1 ) \ {x} FV(t 1 t 2 ) = FV(t 1 ) FV(t 2 ) Further on we suppose that only free variables matter if there are colliding non-free variables they get magically renamed to avoid collision. We will call terms without free variables closed. 14

SUBSTITUTION REVISITED The formal definition of substitution on terms: [x s]x = s [x s]y = y, if x y [x s](t 1 t 2 ) = [x s]t 1 [x s]t 2 [x s](λy.t 1 ) = λy.[x s]t 1 if x y, where y / FV(s) The last rule implies that magic renaming (called alpha-conversion) takes place. 15

Putting λ to work

HIGHER ORDER FUNCTIONS Functions with multiple arguments: add = λn.λm.n + m Higher order functions Simulate multiple arguments Allow partial application (currying) Allow passing functions to functions 16

BOOLEANS Can we incorporate Booleans into λ-calculus? true = λt.λf.t false = λt.λf.f ifthenelse = λb.λl.λr.b l r Yes, we can: ifthenelse true x y = (λt.λf.t) x y = x ifthenelse false x y = (λt.λf.f) x y = y 17

NUMERALS What about arithmetical expressions? c 0 = λs.λz.z c n = λs.λz.s n (z) succ = λn.λs.λz.s (n s z) iszero = λm.m(λx.false)true c n is interpreted as number n, it is also possible to define addition, multiplication,... 18

RECURSION To define recursion we only have to define recursion (still not valid): factorial = λn.if n = 0 then 1 else n factorial(n 1) Fixpoints to the rescue: factstep = λf.λn.if n = 0 then 1 else n f(n 1) factorial = fix factstep 19

FIXPOINTS, MATHEMATICALLY A fixpoint (also fixed point) for function f is such x that f(x) = x In our case factstep(factorial) = factorial Often a fixpoint can be found using some operator fix and in such a case is denoted x = fix f 20

LAMBDA FIXPOINTS In call-by-value λ-calculus we can define the fix operator as fix = λf.(λx.f(λy.x x y)) (λx.f(λy.x x y)) fix f f (fix f) factorial = fix factstep factstep(fix factstep) factstep(factstep( (0) )) The latter shows that factorial definition indeed makes sense. 21

Simple Types for Simple Calculus

FUNCTION TYPES Following our interpretation of λ-abstractions as higher order functions we first introduce function types: T ::= T n T T types: type variables types of functions A lambda term then would have type according to the number of λ-abstractions, e.g. T 1 (T 2 T 3 ). 22

EXPLICIT VERSUS IMPLICIT TYPING There are two main ways of typing λ-calculus: Annotate the λ-abstraction arguments with their types. Deduce the λ-abstraction arguments types from the λ-abstraction body (type inference). Further we use the first solution: λx:t 1.t 23

TYPING CONTEXT How do we type λx:t 1.x y We can choose to type only closed terms, but such a case can occur as a subterm: λy :T 2.(λx:T 1.x y) We won t our type relation to be compositional, therefore we introduce the notion of a typing context. 24

TYPING CONTEXT Γ ::= contexts: empty context Γ, x:t term variable binding Typing context is a set of variable and type pairs that connect the free variables with their types. Lambda typing relation involves contexts, terms and types: Γ t : T 25

LAMBDA SIMPLE TYPING Finally we can present the Lambda calculus simple typing: T-VAR x:t Γ Γ x : T T-ABS Γ, x:t 1 t 2 : T 2 Γ λx:t 1.t 2 : T 1 T 2 T-APP Γ t 1 : T 1 T 2 Γ t 2 : T 1 Γ t 1 t 2 : T 2 Further on we refer to typed Lambda calculus as λ. 26

LAMBDA SIMPLE TYPING EXAMPLE Lets derive the type for the constant function (λx.λy.x): T-ABS T-ABS T-VAR therefore x:t 1 {x:t 1, y :T 2 } x:t 1, y :T 2 x : Φ 2 = T 1 x:t 1 λy :T 2.x : Φ 1 = T 2 Φ 2 λx:t 1.λy :T 2.x : T 1 Φ 1, λx:t 1.λy :T 2.x : T 1 (T 2 T 1 ) 27

LAMBDA SAFETY Progress proof outline: Straightforward induction on typing derivation Preservation proof outline: Prove that substitution preserves types Induction on typing derivation Both proofs are relatively simple. 28

LAMBDA SAFETY, ADVANCED Uniqueness of typing Every lambda term has one unique type. Normalization Every lambda term evaluation halts after a finite number of reductions. The first property is trivial to prove. The second property proof uses the property that with every reduction either the maximal depth of redexes decreases or the number of redexes with maximal depth decreases. 29

NORMALIZATION AND TERMINATION Why is normalization possible? 30

NORMALIZATION AND TERMINATION Why is normalization possible? ω = λx.x x Ω = ω ω What s ω type? T-APP T-ABS T-VAR x:φ 1 x : Φ 1 Φ 2 x:φ 1 x : Φ 1 x:φ 1 x x : Φ 2 ω = λx:φ 1.x x : Φ 2 30

NORMALIZATION AND TERMINATION Fixpoint operator is not typable in λ. Nor is any other kind of recursion. Some type systems are reducible to λ. Most of the languages we examine lack normalization. Some type systems have normalization as their goal. 31