Types and Programming Languages PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Types and Programming Languages


1
Types and Programming Languages
Lecture 9
Simon Gay Department of Computing
Science University of Glasgow
2006/07
2
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.
3
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
4
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
5
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)
6
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. Well 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.
7
Reduction Rules for References
Dereferencing extracts the value of a location
?, !m ? ?, ?(m) (R-DerefLoc)
Assignment changes the value of a location
?, m v ? ?mv, ( ) (R-AssignLoc)
The ref operator creates a new location in the
store
?, ref v ? ?mv, m where m is not in ?
(R-RefVal)
8
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)
9
Example
let val x ref 23 in x (!x) 1 end
Store
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
m5
reducing the let
10
Example
Store
m (!m) 1
m5
obtaining the value of m
m 5 1
m5
reducing on the right of
m 6
m5
updating the value of m
()
m6
11
Sequencing
Now that we have side-effecting operations (e.g.
) its natural to want to write sequences of
operations.
Example
let val x ref 23 in x (!x) 1 x
(!x)2 !x end
(this expression should reduce to 12).
Its easy to formalize the sequencing operator
.
12
Sequencing Typing Rules
We could restrict sequencing to expressions of
type unit, but its easiest not to bother.
(T-Seq)
This means that expressions such as 23
true2 x4 5 all make sense. (Assume that
has low precedence.)
13
Sequencing Reduction Rules
First evaluate the left expression
(R-SeqL)
when the left expression is a value, discard it
?, ve ? ?, e (R-SeqVal)
Alternatively we can regard ef as an
abbreviation for let x e in f end where x is a
fresh variable.
14
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).
15
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
16
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)
17
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 ? ? ? ? .
18
Additional Lemmas
Lemma (Substitution)
If ?, xS ? ? e T and ? ? ? e S then
? ? ? ee/x T .
Lemma
If ? ? ? ? and ?(m) T and ? ? ? v T
then ? ? ? ?mv .
Lemma
If ? ? ? e T and ? ? ? then ? ? ? e
T .
19
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.
20
Reading
Pierce 13
Exercises
Pierce 13.1.1, 13.1.2, 13.4.1, 13.5.2, 13.5.8
Write a Comment
User Comments (0)
About PowerShow.com