Download presentation

Presentation is loading. Please wait.

Published byHannah Williams Modified over 3 years ago

1
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07

2
Types and Programming Languages Lecture 7 - Simon Gay2 Lambda Calculus with Types We introduce function types: A B is the type of functions with a parameter of type A and a result of type B. Types are defined by this grammar: T ::= int | bool | T T By convention, associates to the right, so that A B C means A (B C). Example int int int curried function of two arguments (int int) int function which is given a function

3
2006/07Types and Programming Languages Lecture 7 - Simon Gay3 Typing Rules for Functions To make it easier to define the typing rules, we will modify the syntax so that a -abstraction explicitly specifies the type of its parameter. v ::= integer literal | true | false | x:T.e e ::= v | x | e + e | e == e | e & e | if e then e else e | ee T ::= int | bool | T T

4
2006/07Types and Programming Languages Lecture 7 - Simon Gay4 Typing Rules for Functions The rules are similar to those in SFL, but simpler because we have function types. (T-Abs) (T-App) We also need the typing rules for SEL, with environments added. The resulting system is called the simply typed lambda calculus. (A broad term: anything with T-Abs, T-App, and at least one base type or ground type such as int or bool.)

5
2006/07Types and Programming Languages Lecture 7 - Simon Gay5 Simply Typed Lambda Calculus true : bool false : bool v : intif v is an integer literal (T-Plus)(T-And) (T-Eq)(T-If) (T-Var) (T-Abs) (T-App)

6
2006/07Types and Programming Languages Lecture 7 - Simon Gay6 Examples

7
2006/07Types and Programming Languages Lecture 7 - Simon Gay7 Termination If a typable closed term of the simply typed lambda calculus is executed, it is guaranteed to terminate. If e:T then for some value v, e * v. (See Pierce chapter 12 for a proof.) This result is usually called strong normalization and it is true no matter which reduction strategy is used. It might seem surprising, because we know that recursion can be expressed in the lambda calculus (and we have even seen a non-terminating reduction sequence). The answer is that any term containing xx is untypable.

8
2006/07Types and Programming Languages Lecture 7 - Simon Gay8 xx is untypable If xx were typable, with some type T, then there would be a derivation of x:U xx :T for some type U. What would this derivation look like? We can see that U must be of the form U T but this is not possible (unless we have recursive types, which are coming later).

9
2006/07Types and Programming Languages Lecture 7 - Simon Gay9 Recursion in simply typed lambda calculus If we want to use the fixed point operator Y to express recursive function definitions in the simply typed lambda calculus then we must introduce it as a new kind of term with its own reduction and typing rules. In fact we need a collection of Ys, one for each type. To improve readability well call them fix. (T-Fix) (R-Fix) where x is not free in v Exercise: why is R-Fix like this, instead of ? And why are we only interested in fix at function types?

10
2006/07Types and Programming Languages Lecture 7 - Simon Gay10 Recursion in simply typed lambda calculus Example f = x:int.if x==0 then 1 else x*f(x-1) f:int int. x:int.if x==0 then 1 else x*f(x-1) Given a recursive function definition: f(x:int) = if x==0 then 1 else x*f(x-1) we can rewrite it as a recursively-defined -abstraction: then abstract again on the function name: this term has type (int int) (int int) and we use the appropriate fix to get the function:

11
2006/07Types and Programming Languages Lecture 7 - Simon Gay11 Naming The lambda calculus does not seem to give a direct way of expressing programs in the SFL style: a sequence of named function definitions followed by an expression. But we do have a way of associating a term with a name: -abstraction and function application. Given the program f(x:int):int = body-f g(x:int):int = body-g e we can construct -terms F and G corresponding to f and g, then form (( f:int int. g:int int.e)F)G so that f and g can be used within e, and F and G are only written once.

12
2006/07Types and Programming Languages Lecture 7 - Simon Gay12 Naming We prefer to work more directly with a programmer-friendly syntax. The idea is to write let f(x:int):int = body-f g(x:int):int = body-g in e end and we can define a typing rule for let: (T-Let) Semantically we regard let x = e in e as an abbreviation for ( x:T.e)e.

13
2006/07Types and Programming Languages Lecture 7 - Simon Gay13 Let and Multiple Definitions A let with several definitions can be regarded as an abbreviation: let f(x:int):int = body-f g(x:int):int = body-g in e end let f(x:int):int = body-f in let g(x:int):int = body-g in e end

14
2006/07Types and Programming Languages Lecture 7 - Simon Gay14 Let and Recursive Definitions We want to allow let definitions to be recursive, intending that recursive function definitions will be translated into the equivalent of fix expressions. Theres a small issue: let allows us to define values as well as functions, but we dont want to allow recursively-defined values (why not?) A convenient solution is to distinguish between value and function definitions by using the keywords val and fun. let fun f(x:int):int = body-f fun g(x:int):int = body-g val a:int = 1 in e end

15
2006/07Types and Programming Languages Lecture 7 - Simon Gay15 Simply Typed Lambda Calculus is Safe It is straightforward to prove type preservation and type safety theorems for the simply typed lambda calculus, in the same way as for the Simple Functional Language. The same is true when we add recursion (using fix). Exercise: prove type preservation and type safety theorems for the simply typed lambda calculus.

16
2006/07Types and Programming Languages Lecture 7 - Simon Gay16 A Better Functional Language Combining the ideas so far (function types, let for introducing names, fun and val to distinguish between function definitions (can be recursive) and value definitions (cant be recursive)), introducing more convenient syntax for (fn x:T => e instead of x:T.e), and using = instead of is in function definitions, we get a BFL. BFL looks very much like a small subset of Standard ML. Multi-argument functions must be curried. An implementation of a typechecker for BFL can be found on the course web page, together with a commentary.

17
2006/07Types and Programming Languages Lecture 7 - Simon Gay17 Reading Pierce: 9; 12 (more challenging) for the proof of termination. Exercises Pierce: 9.2.1, 9.2.2, 9.2.3, 9.3.9, 9.3.10, 9.4.10

Similar presentations

OK

Fixed Point Illustrations. Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points.

Fixed Point Illustrations. Simple Examples f : int -> int f(n) = 5 n = 5 is a unique fixed point. f(n) = n^2 – 2 n = 2 and n = -1 are both fixed points.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on history of olympics Ppt on south african culture facts Ppt on save energy save environment Ppt on sustainable development in bangladesh Ppt on sea level rise due Ppt on maths for class 3 Training ppt on personality development Ppt on new zealand culture in the 19th Ppt on credit policy and procedure Ppt on you can win if you want