1 Announcements Exam 2 graded Rainbow grades: HW1-5, Exam 1-2, Quiz 1-6 Part 1: Problem 1, Problem 2, will go over 2 Part 2: Problem 1, Problem 2 Part.

Slides:



Advertisements
Similar presentations
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type checking © Marcelo d’Amorim 2010.
Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Type Checking.
Compiler Construction
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis Semantic Analysis v Lexically and syntactically correct programs may still contain other errors v Lexical and syntax analyses.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
CPS120: Introduction to Computer Science Variables and Constants.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Explicit Polymorphism.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Type Inference – Chapter.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Principles of programming languages 12: Functional programming
Lambda Calculus CSE 340 – Principles of Programming Languages
Types CSCE 314 Spring 2016.
CSE 3302 Programming Languages
Principles of programming languages 8: Types
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Data Types.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Java Reasoning About Code
Announcements Quiz 7 HW 9 is due on Friday Rainbow grades
Announcements Quiz 6 HW7 due Tuesday, October 30
Semantic Analysis Chapter 6.
Compiler Construction
CSCE 314: Programming Languages Dr. Dylan Shell
CSE S. Tanimoto Lambda Calculus
Type Systems Terms to learn about types: Related concepts: Type
Compiler Construction
CSE 3302 Programming Languages
Presentation transcript:

1 Announcements Exam 2 graded Rainbow grades: HW1-5, Exam 1-2, Quiz 1-6 Part 1: Problem 1, Problem 2, will go over 2 Part 2: Problem 1, Problem 2 Part 3: Problem 1, Problem 2, Problem 3, will go over 2 and 3 Part 4: Problem 1, Problem 2, will go over 1 later in lecture Part 5 Spring 16 CSCI 4430, A Milanova

Exam 2 (define A (lambda () (let* ((x 0) (C (lambda (P) (let ((x 1)) (P)))) (D (lambda () x)) (B (lambda () (let ((x 2)) (C D))))) (B) ) Spring 16 CSCI 4430, A Milanova 2

3 Announcements HW 7 will be out tonight, will be due April 21 Lambda calculus and types Spring 16 CSCI 4430, A Milanova

4 Today’s Lecture Outline From pure lambda calculus to a functional programming language Applied lambda calculus Typed lambda calculus Types Type systems Type checking Type safety Type equivalence

From Pure Lambda Calculus to a Programming Language 1 Applied Lambda Calculus Typed Lambda Calculus 1 This material will only appear on extra credit HWs 5

6 Pure Lambda Calculus M  x | ( x. M 1 ) | ( M 1 M 2 ) Amazingly, we can define boolean logic (true and false constants, and, or and not functions) Church booleans Amazingly, we can define arithmetic (numerals, predecessor, successor, plus, times functions, etc.) just in terms of the pure lambda calculus! Church numerals We can define control flow (if-then-else and recursion) in terms of the pure lambda calculus! Spring 16 CSCI 4430, A Milanova

7 Applied Lambda Calculus (from Sethi) M  c | x | ( x.M 1 ) | ( M 1 M 2 ) Augments the pure lambda calculus with constants. Each applied lambda calculus defines its set of constants and reduction rules. For example: Constants:Rules: if, true, false 0, iszero, pred, succ if true M N => δ M if false M N => δ N iszero 0 => δ true iszero (succ k 0) => δ false, k>0 iszero (pred k 0) => δ false, k>0 succ (pred M) => δ M pred (succ M) => δ M

Spring 16 CSCI 4430, A Milanova 8 A Functional Language Construct Applied -CalculusOur Language (ML) Variablexx Constantcc ApplicationM NM N Abstraction x.Mfun x => M Integersucc k 0, k>0k pred k 0, k>0-k Conditionalif P M Nif P then M else N Let( x.M) Nlet val x = N in M end

9 Example Applied lambda calculus expression ( x. if x true y) false In our language (ML), it becomes: let val x = false in if x then true else y end In Scheme, it becomes: (let ((x #f)) (if x #t y)) or ((lambda (x) (if x #t y)) #f) Spring 16 CSCI 4430, A Milanova

10 The Fixed-Point Combinator One more constant, and one more rule: fixfix M => δ M (fix M) Needed to define recursive functions: Therefore, we must have: plus = xy. if (iszero x) y (plus (pred x) (succ y)) y if x = 0 plus (pred x) (succ y) otherwise plus x y =

Spring 16 CSCI 4430, A Milanova 11 The Fixed-Point Combinator But how do we define plus? Define plus = fix M, where M = f. xy. if (iszero x) y (f (pred x) (succ y)) We must show that fix M = δβ xy. if (iszero x) y ((fix M) (pred x) (succ y))

Spring 16 CSCI 4430, A Milanova 12 Define times = ? Exercise: define factorial = ? The Fixed-Point Combinator

The Y Combinator fix is a pure lambda expression! Y = f. ( x. f (x x)) ( x. f (x x)) This is the famous Y-combinator Show that Y M beta-reduces to M (Y M) Think of fix (e.g., Y) as the function that takes a function M and returns M(M(M(…))) 13 Spring 16 CSCI 4430, A Milanova

14 Typed Lambda Calculus Constants add power But they raise problems because they permit erroneous expressions such as if ( x.x) y z function values not permitted as predicate, only true/false (0 x) 0 is not a function succ true undefined in our language plus true ( x.x x) etc.

Spring 16 CSCI 4430, A Milanova 15 Type Expressions Introducing type expressions τ ::= b | τ  τ A type is a basic type (bool, int) or a function type Examples int bool int  (int  int) Syntax of typed lambda calculus: M  x | ( x : τ. M 1 ) | ( M 1 M 2 )

16 Type Judgments and Environment An expression in the typed lambda calculus is Type correct, or Type incorrect The rules of type correctness are given in the form of type judgments in an environment Environment env |- M : τ Read: environment env entails that M has type τ Type judgment env |- M 1 : τ 1  τ 2 env |- M 2 :τ 1 env |- (M 1 M 2 ) : τ 2 Premises Conclusion

Spring 16 CSCI 4430, A Milanova 17 Rules for Typed Lambda Calculus env |- M 1 : τ 1  τ 2 env |- M 2 : τ 1 env |- (M 1 M 2 ) : τ 2 env |- x : lookup(env, x) cons( x: τ 1, env) |- M 1 : τ 2 env |- ( x: τ 1. M 1 ) : τ 1  τ 2 (variable) (application) (abstraction) cons: Augment env with binding x to type τ 1. lookup: looks the type of x in environment env.

Spring 16 CSCI 4430, A Milanova 18 Example Deduce the type for x: int. y: bool. xin the nil environment

19 Read: Scott, Chapter 7.1 – 7.4 Types

Spring 16 CSCI 4430, A Milanova/BG Ryder 20 What is a type? A set of values and the valid operations on those values Integers: + - * / = >... Arrays: lookUp(, ) assign(,, ) initialize( ), setBounds( ) User-defined types: Java interfaces

Spring 16 CSCI 4430, A Milanova/BG Ryder 21 What is the role of types? What is the role of types in programming languages? Semantic correctness Data abstraction ADTs Documentation (static types only)

Spring 16 CSCI 4430, A Milanova/BG Ryder 22 3 Views of Types Denotational (or set) point of view: A type is simply a set of values. A value has a given type if it belongs to the set. E.g. int = { 1,2,... } char = { ‘a’,’b’,... } bool = { true, false } Abstraction-based point of view: A type is an interface consisting of a set of operations with well-defined meaning

23 3 Views of Types Constructive point of view: Primitive/simple/built-in types: e.g., int, char, bool Composite/constructed types: Constructed by applying type constructors pointer e.g., pointerTo(int) array e.g., arrayOf(char) or arrayOf(char,20) or... record/struct e.g., record(age:int, name:string) union e.g. union(int, pointerTo(char)) liste.g., list(...) functione.g., float  int CAN BE NESTED! pointerTo(arrayOf(pointerTo(char))) For most of us, types are a mixture of these 3 views Spring 16 CSCI 4430, A Milanova/BG Ryder

24 What is a Type System? A mechanism to define types and associate them with programming language constructs Additional rules for type equivalence, type compatibility Important from pragmatic point of view

25 What is Type Checking? The process of ensuring that the program obeys the type rules of the language Type checking can be done statically At compile-time, i.e., before execution Statically typed (or statically checked) language Type checking can be done dynamically At runtime, i.e., during execution Dynamically typed (or dynamically checked) language Spring 16 CSCI 4430, A Milanova/BG Ryder

26 What is Type Checking? Statically typed (better term: statically checked) languages Typically require type annotations (e.g., A a, List list) Typically have a complex type system, and most of type checking is performed statically (at compile-time) Ada, Pascal, Java, C++ A form of early binding Dynamically typed (better term: dynamically checked) languages. Also known as Duck typed… Typically require no type annotations! All type checking is performed dynamically (at runtime) Smalltalk, Lisp and Scheme, Python, JavaScript Spring 16 CSCI 4430, A Milanova/BG Ryder

27 What is Type Checking? The process of ensuring that the program obeys the type rules of the language Type safety Textbook defines term prohibited application (also known as forbidden error): intuitively, a prohibited application is an application of an operation on values of the wrong type Type safety is the property that no operation ever applies to values of the wrong type at runtime. I.e., no prohibited application (forbidden error) ever occurs Spring 16 CSCI 4430, A Milanova/BG Ryder

28 Language Design Choices Design choice: what is the set of forbidden errors? Obviously, we cannot forbid all possible semantic errors… Define a set of forbidden errors Design choice: Once we’ve chosen the set of forbidden errors, how does the type system prevent them? Static checks only? Dynamic checks only? A combination of both? Furthermore, are we going to absolutely disallow forbidden errors (be type safe), or are we going to allow for programs to circumvent the system and exhibit forbidden errors (i.e., be type unsafe)? Spring 16 CSCI 4430, A Milanova

Spring 16 CSCI 4430, A Milanova/BG Ryder 29 Forbidden Errors Example: indexing an array out of bounds a[i], a is of size Bound, i<0 or Bound≤i In C, C++, this is not a forbidden error 0≤i and i<Bound is not checked (bounds are not part of type) What are the tradeoffs here? In Pascal, this is a forbidden error. Prevented with static checks 0≤i and i<Bound must be checked at compile time What are the tradeoffs here? In Java, this is a forbidden error. It is prevented with dynamic checks 0≤i and i<Bound must be checked at runtime What are the tradeoffs here?

30 Type Safety Java vs. C++: Java: Duck q; …; q.quack() class Duck has quack C++: Duck *q; …; q->quack() class Duck has quack Can we write code that calls quack() on an object that isn’t a Duck ? In Java? In C++? Java is said to be type safe while C++ is said to be type unsafe

Spring 16 CSCI 4430, A Milanova/BG Ryder 31 C++ is type unsafe //#1 void* x = (void *) new A; B* q = (B*) x; //a safe downcast? int case1 = q->foo()//what happens? //#2 void* x = (void *) new A; B* q = (B *) x; //a safe downcast? int case2 = q->foo(66); //what happens? A B virtual foo() virtual foo() vritual foo(int) q->foo(66) is a prohibited application (i.e., application of an operation on a value of the wrong type, i.e., forbidden error). Static type B* q “promises” the programmer that q will point to a B object. However, language does not “honor” this promise…

What is Type Checking statically not statically typed typed (i.e., dynamically typed) type safe ML, Java Scheme type unsafe C, C++ Assembly Spring 16 CSCI 4430, A Milanova/BG Ryder 32

Spring 16 CSCI 4430, A Milanova/BG Ryder 33 What is Type Checking? Static typing vs. dynamic typing What are the advantages of static typing? What are the advantages of dynamic typing?

34 One more thing… What is strong typing? One often hears “Java is strongly typed” while “C++ is weakly typed”… The term is often used in nonsensical way “The language has a type checker” “The language is sound” To most it seems to mean: “A language like C or Java related in a way I can’t make quite precise” Correct (I think): the language has some element of static typing and is type safe Spring 16 CSCI 4430, A Milanova/BG Ryder

35 Lecture Outline Types Type systems Type checking Type safety Type equivalence