Types and Programming Languages Lecture 9 Simon Gay
- Slides: 20
Types and Programming Languages Lecture 9 Simon Gay Department of Computing Science University of Glasgow 2006/07 Types and Programming Languages Lecture 9 - Simon Gay
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 !r r : = v ref e is the type of a reference which stores values of type T is the value stored in r, if r is a reference updates the value stored in r creates a new reference which stores the value of e Warning: !r is potentially confusing syntax, but follows Pierce. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 2
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 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 3
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 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 4
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) 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 5
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. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 6
Reduction Rules for References Dereferencing extracts the value of a location: , !m , (m) (R-Deref. Loc) Assignment changes the value of a location: , m : = v [m: =v], ( ) (R-Assign. Loc) The ref operator creates a new location in the store: , ref v +[m=v], m 2006/07 where m is not in Types and Programming Languages Lecture 9 - Simon Gay (R-Ref. Val) 7
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-Assign. L) (R-Assign. R) Notice that the rules now involve reductions involving the store: , e ’, e’. Existing rules must also be modified, e. g. : (R-Plus. L) 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 8
Example Store empty m=5 let val x = ref 2+3 in x : = (!x) + 1 end reducing inside the ref let val x = ref 5 in x : = (!x) + 1 end creating a new location m let val x = m in x : = (!x) + 1 end reducing the let 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 9
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 2006/07 () Types and Programming Languages Lecture 9 - Simon Gay 10
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 ; . 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 11
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. ) 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 12
Sequencing: Reduction Rules First evaluate the left expression: (R-Seq. L) when the left expression is a value, discard it: , v; e , e (R-Seq. Val) Alternatively we can regard e; f as an abbreviation for let x = e in f end where x is a fresh variable. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 13
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). 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 14
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: 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 15
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) 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 16
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 | ’ ’. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 17
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. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 18
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. 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 19
Reading Pierce: 13 Exercises Pierce: 13. 1. 1, 13. 1. 2, 13. 4. 1, 13. 5. 2, 13. 5. 8 2006/07 Types and Programming Languages Lecture 9 - Simon Gay 20
- Simon and garfunkel gay
- Strongly typed vs weakly typed
- Integral data type
- Real-time systems and programming languages
- Elsa gunter uiuc
- Advantages of high level language
- Real time programming language
- Cs 421 programming languages and compilers
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Real time example of multithreading in java
- Programming languages levels
- Introduction to programming languages
- Plc coding language
- Procedural programming languages
- Imperative programming languages
- Alternative programming languages
- Transmission programming languages
- Adam doupe cse 340
- Xenia programming languages
- Mainstream programming languages
- Vineeth kashyap