types and programming languages lecture 4 simon gay department of computing science university of...

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

Upload: mia-doyle

Post on 28-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Types and Programming Languages

Lecture 4

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

Page 2: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 2

Variables

We can easily extend the language of expressions to includevariables.

v ::= integer literal | true | falsex ::= identifiere ::= v | x | e + e | e == e | e & e | if e then e else e

We can form expressions such as x+1 or if x then y else 2but what do they mean?

If values (or perhaps expressions) are substituted for thevariables then the resulting expression can be reduced.This sounds like function application…

Page 3: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 3

Typing expressions with variables

We want to typecheck expressions like x+1 before substitutingvalues for variables. We can say:

if x:int then x+1:int

and we write this as:

intxintx :1:

In general: Te :

where nn TxTx :,,: 11 is the environment.

is a typing judgement

Page 4: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 4

Typing expressions with variables

The typing rules are as before, with the environment added:

true : bool false : bool v : int if v is an integer literal

intfeintfinte

:::

boolfeboolfboole

:&::

(T-Plus) (T-And)

boolfeintfinte

:::

(T-Eq)Tfelseethencif

TfTeboolc::::

(T-If)

and a rule for variables: , x:T x:T orTx

Tx:

:

(T-Var)

Page 5: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 5

Example: typing expressions with variables

If-Tintelseythenxif

intVarT

intyinty

Var-Tboolx

boolx

:2:2:

::

:

If =x:bool, y:int then we have the following derivation:

Page 6: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 6

Functions

Define a syntax for function definitions:

def ::= f(x:T,…,x:T):T is ef ::= identifierT ::= int | bool

and extend the syntax of expressions to include function calls:

e ::= v | x | e + e | e == e | e & e | if e then e else e | f(e,…,e)

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

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 7

Functions

To typecheck a function we need to know the types of itsarguments and results, so we’ll say that an environment contains typed variables x:T and function definitions

eisTTxTxf nn :):,,:( 11

Now we can define a typing rule for function application:

TeefTeTeeisTTxTxf

n

nnnn:),(

:::):,:(

1

1111

(T-App)

Page 8: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 8

Functions

We also need to typecheck a function definition, to make surethat the function body has the declared result type and usesthe arguments according to their declared types.

We introduce a new kind of judgement:

okeisTTxTxf nn :):,,:( 11

and define a rule which specifies when a function definition is ok:

okeisTTxTxfTeTxTx

nn

nn:):,:(

::,:,

11

11

(T-Def)

This allows the function body e to refer to functions (including f )defined in . So mutually recursive functions are allowed.

Page 9: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 9

Programs with functions

Define a program to be a sequence of function definitionsfollowed by a typed expression e:T. Let be the environmentconsisting of all the function definitions (and no variables).

To typecheck a program:• for each eisTTxTxf nn :):,,:( 11 in , check that

okeisTTxTxf nn :):,,:( 11

• check that e:T.

Then evaluate e (we need to define reductions for functionapplications).

Page 10: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 10

Program with functions: example

f(x:int):int is if x==0 then 1 else x * f(x-1)

f(5):int

Assume that we include multiplication and subtraction in theobvious way:

Exercises:• What is the “obvious way”?• What is when we typecheck this program?• Show a complete derivation of

okxfxelsethenxifisintTxf )1(*10:):(

Page 11: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 11

Functions: operational semantics

The idea is that a function applied to values reduces bysubstituting the values (actual parameters) for the arguments(formal parameters) in the function body.

],,[),(:):,:(

111

11

nnn

nnxvxvevvf

eisTTxTxf

Substitution is defined recursively on the structure of terms:vxvxvv nn ],,[ 11

inni vxvxvx ],,[ 11

inn xxifxxvxvx ],,[ 11 .]~~[']~~[]~~)['( etcxvexvexvee ])~~[,],~~[(]~~)[,,( 11 xvexvefxveef nn

(R-App)

Page 12: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 12

Functions: operational semantics

We also need rules defining reductions of function arguments:

),,,'(),,('

22 nn eeefeeefee

),,,',(),,,('

33 nn eeevfeeevfee

and so on.

This is call by value semantics: function arguments are reducedto values before being substituted into the function body.

Variables do not reduce: they are stuck. (Remember that we areonly interested in evaluating expressions without variables.)

(R-Arg1)

(R-Arg2)

Page 13: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 13

Example

f(x:int):int is if x==0 then 1 else x * f(x-1)

f(5):int

f(3) if 3==0 then 1 else 3*f(3-1) if false then 1 else 3*f(3-1) 3*f(3-1) 3*f(2) 3*(if 2==0 then 1 else 2*f(2-1)) 3*(if false then 1 else 2*f(2-1)) 3*2*f(2-1) 3*2*f(1) 3*2*(if 1==0 then 1 else 1*f(1-1)) 3*2*(if false then 1 else 1*f(1-1)) 3*2*1*f(1-1) 3*2*1*f(0) 3*2*1*(if 0==0 then 1 else 0*f(0-1)) 3*2*1*(if true then 1 else 0*f(0-1)) 3*2*1*1 3*2*1 3*2 6

Page 14: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 14

Type preservation

Before proving the type preservation theorem, we need

Substitution Lemma

If TeTxTx nn ::,:, 11 and 11 :Tv … nn Tv :

then Txvxve nn :],,[ 11

because otherwise we don’t know anything about the type of],,[ 11 nn xvxve and the proof of type preservation will

break down when we get to the reduction of function applications.

Page 15: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 15

Proof of the Substitution Lemma

By induction on the structure of e.

Case 1. e is a value v.

vxvxve nn ],,[ 11 so v is an integer or boolean literal,

T is either int or bool, and we have Tv : directly.

Case 2. e is a variable, one of the ix

inn vxvxve ],,[ 11 and T is iT

so we have ii Tv : by assumption.

Case 3. e is a variable x, not one of the ix

xxvxve nn ],,[ 11 and x must occur in so we have Tx :

Page 16: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 16

Proof of the Substitution Lemma

Case 4. e is t+u (the other binary operators, and if, are similar)

intutTxTxintuTxTxinttTxTx

nn

nnnn::,:,

::,:,::,:,

11

1111

Using the induction hypothesis we have

intxvxvt nn :],,[ 11 intxvxvu nn :],,[ 11

from which we can buildintxvxvuxvxvt nnnn :],,[],,[ 1111

which is what we want, because],,[],,[],,)[( 111111 nnnnnn xvxvuxvxvtxvxvut

Page 17: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 17

Proof of the Substitution Lemma

Case 5. e is

TeefTxTxUeTxTxeisTUyUyf

mnn

iinnmm:),(:,:,

::,:,:):,:(

111

1111

Using the induction hypothesis we have, for each i,

ii Uxve :]~~[

from which we can buildTxvexvef m :])~~[,],~~[( 1

which is what we want, because

),,( 1 meef

])~~[,],~~[(]~~)[,,( 11 xvexvefxveef mm

Page 18: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 18

Proof of the Substitution Lemma: Comment

We would really like to be able to give a direct proof of theSubstitution Lemma:

in a derivation tree for TeTxTx nn ::,:, 11

replace every leaf iinn TxTxTx ::,:, 11

by a derivation tree for ii Tv :

to obtain a derivation tree for Txvxve nn :],,[ 11

But this doesn’t quite work because the environments get outof step. To do it properly we have to use proof by induction.

Page 19: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 19

Type Preservation

Now that we have environments, we have

Type Preservation Theorem

If e:T and ee’ then e’:T.

Proof

Similar to before, with a new base case for function application.

],,[),( 111 nnn xvxvevvf

TeTxTx nn ::,:, 11 and we assume that all function definitions are ok, so

eisTTxTxf nn :):,:( 11 Given the reduction

we know that

Page 20: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 20

Proof of Type Preservation

We have Tvvf n :),,( 1 so, for each i, ii Tv :

Now the Substitution Lemma tells us that

Txvxve nn :],,[ 11

as required.

There are also new inductive cases corresponding to the rulesfor reducing function arguments, but these cases are similar tothe corresponding cases for +.

Page 21: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 21

Typable stuck expressions are values

The proof of this theorem is similar to before, but we have to dealwith the case of variables, which are stuck. We do this byrestricting attention to terms which contain no variables(recall that in a program we have e:T where only containsfunction definitions).

Theorem

If e:T and contains no variables and e is stuck thene is a value.

Proof

As before. The assumptions mean that e cannot be a variable.

Page 22: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 22

We have a Simple Functional Language

The type system guarantees that computation does not get stuck.

It’s a call by value language. An alternative is call by name:

],,[),(:):,:(

111

11

nnn

nnxexeeeef

eisTTxTxf

and no reduction of function arguments.

If we wanted to, we could allow function definitions to specifycbv or cbn for each argument. Real languages usually chooseone or the other throughout. Haskell: call by name (lazy).ML: call by value (strict).

Page 23: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 23

Why is it so simple?

Functions are not expressions. The only thing we can do with afunction is apply it. This means that we do not need functiontypes. We have a first order language.

There are no nested functions. Function definitions occur onlyat the top level.

However, we didn’t simplify it all the way: working withnamed function definitions and allowing them to be recursiveintroduced some complications.

Page 24: Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07

2006/07 Types and Programming Languages Lecture 4 - Simon Gay 24

Reading

Pierce: 8

Exercises

Pierce: 8.2.3, 8.3.4, 8.3.5, 8.3.6, 8.3.7, 8.3.8

Exercise sheet 2

Section 2 of Linear Types for Packet Processing (again)