Download presentation
Presentation is loading. Please wait.
Published byCora Small Modified over 8 years ago
1
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
2
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
3 Announcements HW 7 will be out tonight, will be due April 21 Lambda calculus and types Spring 16 CSCI 4430, A Milanova
4
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
5
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
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
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
8
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
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
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 =
11
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))
12
Spring 16 CSCI 4430, A Milanova 12 Define times = ? Exercise: define factorial = ? The Fixed-Point Combinator
13
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
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.
15
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
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
17
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.
18
Spring 16 CSCI 4430, A Milanova 18 Example Deduce the type for x: int. y: bool. xin the nil environment
19
19 Read: Scott, Chapter 7.1 – 7.4 Types
20
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
21
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)
22
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
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
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
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
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
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
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
29
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
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
31
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…
32
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
33
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
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
35 Lecture Outline Types Type systems Type checking Type safety Type equivalence
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.