Types and programming languages
Download
1 / 20

Types and Programming Languages - PowerPoint PPT Presentation


  • 151 Views
  • Updated On :

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.

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

PowerPoint Slideshow about 'Types and Programming Languages' - dionne


An Image/Link below is provided (as is) to download presentation

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 l.jpg

Types and Programming Languages

Lecture 9

Simon Gay

Department of Computing Science

University of Glasgow

2006/07


Reference types l.jpg
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.

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


Reference types3 l.jpg
Reference Types

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


Reference types and standard variables l.jpg
Reference Types and Standard Variables

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


Typing rules for references l.jpg
Typing Rules for References

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


Reduction rules for references l.jpg
Reduction Rules for References

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


Reduction rules for references7 l.jpg
Reduction Rules for References

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


Reduction rules for references8 l.jpg
Reduction Rules for References

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


Example l.jpg
Example

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


Example10 l.jpg
Example

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


Sequencing l.jpg
Sequencing

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


Sequencing typing rules l.jpg
Sequencing: Typing Rules

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


Sequencing reduction rules l.jpg
Sequencing: Reduction Rules

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


Type safety with references l.jpg
Type Safety with References

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


Store typings l.jpg
Store Typings

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


Typing rules with store typings l.jpg
Typing Rules with Store Typings

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


Stores must be well typed l.jpg
Stores must be well-typed

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


Additional lemmas l.jpg
Additional Lemmas

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


Progress theorem l.jpg
Progress Theorem

(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


Reading l.jpg
Reading

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


ad