1 / 20

# Types and Programming Languages - PowerPoint PPT Presentation

Types and Programming Languages. Lecture 9. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Reference Types. Up to now we have looked at a functional language, but we can also describe variables and assignment, looking at both typing and reductions.

Related searches for Types and Programming Languages

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Types and Programming Languages' - dionne

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Types and Programming Languages

Lecture 9

Simon Gay

Department of Computing Science

University of Glasgow

2006/07

Up to now we have looked at a functional language, but we can

also describe variables and assignment, looking at both typing

and reductions.

The key idea is a reference (assignable variable, or storage cell),

and we need to distinguish carefully between a reference and

the value which it stores.

Ref T is the type of a reference which stores values of

type T

!r is the value stored in r, if r is a reference

r := v updates the value stored in r

ref e creates a new reference which stores the value of e

Warning: !r is potentially confusing syntax, but follows Pierce.

Types and Programming Languages Lecture 9 - Simon Gay

Of course it is important to check the types of references.

Example: the following code should not typecheck:

let val r = ref 2

in if !r then … else …

end

The following code is correct:

let val r = ref 2

in r := (!r) + 1

end

Types and Programming Languages Lecture 9 - Simon Gay

Mainstream imperative languages do not use ref and ! explicitly.

Example: this Java code

{ int x = 2;

x = x + 1;

}

really means:

let val x = ref 2

in x := (!x) + 1

end

Types and Programming Languages Lecture 9 - Simon Gay

When we create a reference, its type is determined by the type

of its contents:

(T-Ref)

Dereferencing (extracting the value) is the reverse:

(T-Deref)

Assignment is evaluated for its side effect. We use type unit.

(T-Assign)

Types and Programming Languages Lecture 9 - Simon Gay

To define the operational semantics of programs which use

references, we need to formalize the idea of the store.

The store consists of a set of locations (m, n, …), each with a

value. We’ll use  (sigma) to stand for a general store. We

write stores as, for example, m = 2, n = true, o = “hello”.

We write (m) to refer to the value of a particular location, and

write [m := v] for “the store  updated by putting value v in

location m”.

Now we use reductions of the form

, e  ’, e’

because it is now possible for the store to change during

evaluation of an expression.

Types and Programming Languages Lecture 9 - Simon Gay

Dereferencing extracts the value of a location:

, !m  , (m) (R-DerefLoc)

Assignment changes the value of a location:

, m := v  [m:=v], ( ) (R-AssignLoc)

The ref operator creates a new location in the store:

, ref v  +[m=v], m where m is not in  (R-RefVal)

Types and Programming Languages Lecture 9 - Simon Gay

As usual, we also need to say what are the values of type Ref T:

they are locations m. We need rules of the usual form, which

specify how to reduce expressions ref e, !e and e := f.

(R-Ref)

(R-Deref)

(R-AssignL)

(R-AssignR)

Notice that the rules now involve reductions involving the store:

, e  ’, e’. Existing rules must also be modified, e.g.:

(R-PlusL)

Types and Programming Languages Lecture 9 - Simon Gay

Store

let val x = ref 2+3

in x := (!x) + 1

end

empty

reducing inside the ref

let val x = ref 5

in x := (!x) + 1

end

empty

creating a new location m

let val x = m

in x := (!x) + 1

end

m=5

reducing the let

Types and Programming Languages Lecture 9 - Simon Gay

Store

m=5

m := (!m) + 1

obtaining the value of m

m=5

m := 5 + 1

reducing on the right of :=

m=5

m := 6

updating the value of m

m=6

()

Types and Programming Languages Lecture 9 - Simon Gay

Now that we have side-effecting operations (e.g. := ) it’s natural

to want to write sequences of operations.

Example:

let val x = ref 2+3

in x := (!x) + 1;

x := (!x)*2;

!x

end

(this expression should reduce to 12).

It’s easy to formalize the sequencing operator ; .

Types and Programming Languages Lecture 9 - Simon Gay

We could restrict sequencing to expressions of type unit, but it’s

easiest not to bother.

(T-Seq)

This means that expressions such as 2;3 true;2 x:=4; 5

all make sense. (Assume that ; has low precedence.)

Types and Programming Languages Lecture 9 - Simon Gay

First evaluate the left expression:

(R-SeqL)

when the left expression is a value, discard it:

, v;e  , e (R-SeqVal)

Alternatively we can regard e;f as an abbreviation for

let x = e in f end where x is a fresh variable.

Types and Programming Languages Lecture 9 - Simon Gay

If we add references to BFL with the typing rules which we have

defined, then we still have a safe language: execution of a

correctly typed program does not result in any runtime type

errors.

Proving this requires some more definitions. We will cover them

briefly here; a more complete discussion can be found in

Pierce (Chapter 13).

Types and Programming Languages Lecture 9 - Simon Gay

As usual we aim to prove a type preservation theorem.

We have a typing rule for expressions which create references:

(T-Ref)

ref e reduces (eventually) to a store location m, so to prove

type preservation we need a rule assigning type Ref T to m.

This leads to the idea of a store typing , which is a mapping

from store locations to types.

We use a new form of typing judgement:

Types and Programming Languages Lecture 9 - Simon Gay

We add a store typing  to every typing judgement in every

typing rule that we have.

For example:

(T-App)

(T-Assign)

Also (the point!) we add a typing rule for store locations:

(T-Loc)

Types and Programming Languages Lecture 9 - Simon Gay

Definition: A store  is well-typed with respect to a type

environment  and a store typing , written  |    ,

if dom() = dom() and for every mdom() ,

 |   (m) : (m) .

This enables us to extend the type preservation theorem:

as the store changes, it must remain well-typed.

Theorem (Type Preservation):

If  |   e : T and  |    and , e  ’, e’

then, for some ’ such that   ’ ,

 | ’  e’ : T and  | ’  ’ .

Types and Programming Languages Lecture 9 - Simon Gay

Lemma (Substitution)

If , x:S |   e : T and  |   e’ : S then  |   e[e’/x] : T .

Lemma

If  |    and (m) = T and  |   v : T

then  |   [m:=v] .

Lemma

If  |   e : T and   ’ then  | ’  e : T .

Types and Programming Languages Lecture 9 - Simon Gay

(Compare the formulation of this progress theorem with our

earlier formulation of a type safety theorem.)

Theorem (Progress) (Pierce, Chapter 13)

Suppose that  |   e : T . Then either e is a value,

or for any store  such that  |    , there is some term e’

and store ’ with , e  ’, e’ .

Exercise: Prove all of these lemmas and theorems.

Types and Programming Languages Lecture 9 - Simon Gay

Pierce: 13

Exercises

Pierce: 13.1.1, 13.1.2, 13.4.1, 13.5.2, 13.5.8

Types and Programming Languages Lecture 9 - Simon Gay