CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
A Type System for Well-Founded Recursion Derek Dreyer Carnegie Mellon University POPL 2004 Venice, Italy.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Prof. Hilfinger CS164 Lecture 191 Type Checking Lecture 19 (from notes by G. Necula)
Foundations of Programming Languages: Introduction to Lambda Calculus
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
ISBN Chapter 7 Expressions and Assignment Statements.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
ISBN Lecture 07 Expressions and Assignment Statements.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Prof. Fateman CS 164 Lecture 23
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Exponents An exponent is the number of times the base is multiplied by itself. Example 27 can also be written as 3 This means 3 X 3 X 3.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator.
Arithmetic Expressions
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Types in programming languages1 What are types, and why do we need them?
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
ALGEBRA READINESS Chapter 5 Section 6.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
CMSC 330: Organization of Programming Languages Operational Semantics.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
7.2 Arithmetic Expressions
Programming Languages and Compilers (CS 421)
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Functions.
Lecture 6: Lambda Calculus
Chapter 7: Expressions and Assignment Statements
CSE-321 Programming Languages Simply Typed -Calculus
Introduction to Computer Science / Procedural – 67130
Chapter 7: Expressions and Assignment Statements
Lambda Calculus Revisited
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Type Systems Terms to learn about types: Related concepts: Type
Programming Languages and Compilers (CS 421)
Exponents An exponent is the number of times the base is multiplied by itself. Example 27 can also be written as 3 This means 3 X 3 X 3 3.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
College of Computer Science and Engineering
Programming Languages and Compilers (CS 421)
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad)L13-1-Lambda-Opt2 Lecture Outline Lambda Calculus –Review and examples Simple types Type checking Comparison with OO type systems

CS7120 (Prasad)L13-1-Lambda-Opt3 Lambda Calculus Review Grammar: E ::= x variables | E 1 E 2 function application | x. E function creation Evaluation: ( x. e) e’  [e’/x]e

CS7120 (Prasad)L13-1-Lambda-Opt4 Example 1 ( x.x) y.y  y.y ( x.x) y.y  [ y.y/x] x = y.y

CS7120 (Prasad)L13-1-Lambda-Opt5 Example 2 ( x. y.x) ( z.z) e  * z.z (( x. y.x) ( z.z)) e  ([ z.z/x] y.x) e  ( y. z.z) e  [e/y] z.z = z.z

CS7120 (Prasad)L13-1-Lambda-Opt6 Example 3 ( x. x x) ( x. x x)  ( x. x x) ( x. x x) ... ( x. x x) ( x. x x)  [( x. x x)/x](x x) = ( x. x x)

CS7120 (Prasad)L13-1-Lambda-Opt7 Question What programming errors can occur in lambda calculus?

CS7120 (Prasad)L13-1-Lambda-Opt8 Notes We can encode anything we like in lambda calculus –Booleans, integers, objects, if-then-else, recursion,... But don’t forget that these are encodings –Akin to programming directly with 0’s and 1’s

CS7120 (Prasad)L13-1-Lambda-Opt9 Extension Grammar: E ::= x variables | E 1 E 2 function application | x. E function creation | 0,1,2,… integers | + addition Evaluation: ( x. e) e’  [e’/x]e + i j  k where k = i + j

CS7120 (Prasad)L13-1-Lambda-Opt10 Digression There is nothing magic about adding integers and + as constants to the lambda calculus We could add any data types and operations we like They can be encoded directly in lambda calculus anyway

CS7120 (Prasad)L13-1-Lambda-Opt11 Examples ( x. + x 1) ( x. + x x) ( x. + x 1) 3  4 ( x. + x 1) ( y.y)  ?

CS7120 (Prasad)L13-1-Lambda-Opt12 What Happens? ( x. + x 1) ( y.y)  + ( y.y) 1  ? Answer: It depends. A runtime error; we can’t add to a function Or no error: If + and 1 are encoded as lambda terms, computation will continue!

CS7120 (Prasad)L13-1-Lambda-Opt13 Notes Assume 1, + are encoded as lambda terms Nothing guarantees the encoding of 1 is used as an integer –E.g., (1 x.x) –Evaluation doesn’t know what the encodings are supposed to represent

CS7120 (Prasad)L13-1-Lambda-Opt14 Back to the Future We need to be able to restrict uses of values to appropriate operations We need a type system! Note that we’d like a type system whether or not + ( y.y) 1 causes a runtime error –Catching these errors before program execution is better

CS7120 (Prasad)L13-1-Lambda-Opt15 Typed Lambda Calculus Grammar: E ::= x variables | E 1 E 2 function application | x: . E function creation | 0,1,2,… integers | + addition Evaluation: ( x: . e) e’  [e’/x]e + i j  k where k = i + j

CS7120 (Prasad)L13-1-Lambda-Opt16 The Types We have only two kinds of values –Integers –Functions Type grammar:  := int |   

CS7120 (Prasad)L13-1-Lambda-Opt17 Examples of Types int int  int (int  int)  int int  (int  int)

CS7120 (Prasad)L13-1-Lambda-Opt18 Type Judgments Type environment Type Expression “it is provable that” “has type”

CS7120 (Prasad)L13-1-Lambda-Opt19 Examples

CS7120 (Prasad)L13-1-Lambda-Opt20 More Examples

CS7120 (Prasad)L13-1-Lambda-Opt21 Type Rule: Variables A variable has the type assumed in the type environment. [Var]

CS7120 (Prasad)L13-1-Lambda-Opt22 Abstraction A function has type  1   2 if the function body has type  2 when we assume the argument has type  1. [Abs]

CS7120 (Prasad)L13-1-Lambda-Opt23 Application Applying a function of type  1   2 to an argument of type  1 gives a result of type  2 [App]

CS7120 (Prasad)L13-1-Lambda-Opt24 Integers An integer has type int [Int]

CS7120 (Prasad)L13-1-Lambda-Opt25 Addition Adding two int’s produces an int [Add]

CS7120 (Prasad)L13-1-Lambda-Opt26 Example 1

CS7120 (Prasad)L13-1-Lambda-Opt27 Example 2

CS7120 (Prasad)L13-1-Lambda-Opt28 Type Checking One recursive descent traversal. Top-down: Add type declarations of formal parameters to environments [Abs]

CS7120 (Prasad)L13-1-Lambda-Opt29 Type Checking (Cont.) Bottom-up: Check that types match in applications [App]

CS7120 (Prasad)L13-1-Lambda-Opt30 Structural Equality “Types match” means “types are equal” Equality is recursively defined int = int a  b = c  d  a = c  b = d

CS7120 (Prasad)L13-1-Lambda-Opt31 Notes In typed lambda calculus: –Types of all variables are declared –Types are structured (e.g., int  int) –Types are checked for equality Many typed languages in this class –Nearly all typed languages before 1980 –FORTRAN, ALGOL, Pascal, C –Captures C typing except for casts & coercions

CS7120 (Prasad)L13-1-Lambda-Opt32 Typed OO Languages In many typed object-oriented languages –Types of all variables are declared –Types are non-structural (just names) Declare all types and type relationships –Types are checked for subtyping relationships What if type declarations are omitted? –A language with type inference

CS7120 (Prasad)L13-1-Lambda-Opt33 Discussion What about structural types + subtyping? –Area of current research –Currently no consensus on the right way to combine C-like type systems with typical OO-like type systems