Types 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

COMPSCI 3MI3 - Principles of Programming Languages

Types for references and memory

J. Carette

McMaster University

Fall 2023

Adapted from “Types and Programming Languages” by Benjamin C. Pierce

J. Carette (McMaster University) Types for references and memory Fall 2023 1 / 18
About :=

The left-hand-side of := is a location of a memory cell.

J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
About :=

The left-hand-side of := is a location of a memory cell.


When these can be stored to multiple times (without allocation), called
mutable references.

J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
About :=

The left-hand-side of := is a location of a memory cell.


When these can be stored to multiple times (without allocation), called
mutable references.
In general, three operations:
Memory allocation, aka creating a reference.
A “store” operation, aka assignment.
A “retrieve” operation, aka dereferencing.
Depending on the programming language, some or all of these operations
may be implicit in the grammar.
Python hides allocation and retrieval, but storage is explicit.
C/C++ hides retrieval, with allocation and storage being explicit.
In ML, all three operations are explicit.
Haskell buries these very deeply in a library.

J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
References

⟨t⟩ ::= ... ⟨v ⟩ ::= λ x:⟨T ⟩.⟨t⟩


| ref t | unit
| !t | l
| t := t ⟨T ⟩ ::= ...
| l | Ref ⟨T ⟩
Types (to be refined):
Γ ⊢ t1 : T1
(T-Ref)
Γ ⊢ ref t1 : Ref T1

Γ ⊢ t1 : Ref T1
(T-Deref)
Γ ⊢!t1 : T1

Γ ⊢ t1 : Ref T1 Γ ⊢ t2 : T1
(T-Assign)
Γ ⊢ t1 := t2 : Unit

J. Carette (McMaster University) Types for references and memory Fall 2023 3 / 18
A Sample Program

1 let x = ref 0 in
2 let y = ref 0 in
3 let z = ref 1 in
4 x := 2 ;
5 y := 3 ;
6 z := ! x + !y;
7 !z
8
9 >> 5

ref is like new in Java.

J. Carette (McMaster University) Types for references and memory Fall 2023 4 / 18
Aliasing

1 let x = ref 5 in
2 let y = x in
3 x := 1 0 ;
4 !y
5
6 >> 10

J. Carette (McMaster University) Types for references and memory Fall 2023 5 / 18
Sharing is not necessarily bad

Aliases cells as implicit communication channels:


1 let c = ref 0 in
2 l e t inc c = λx : U n i t . ( c := s u c c ( ! c ) ; ! c ) i n
3 l e t dec c = λx : U n i t . ( c := p r e d ( ! c ) ; ! c ) i n
4 inc c unit ;
5 inc c unit ;
6 dec c unit

The values of c are 1 then 2 then 1 again.


Shades of OO...

J. Carette (McMaster University) Types for references and memory Fall 2023 6 / 18
Heap / Store

Heap array of (typed) values, ’memory store’ with ’locations’. Let L denote
some set of store locations. Use l to range over L.

J. Carette (McMaster University) Types for references and memory Fall 2023 7 / 18
Heap / Store

Heap array of (typed) values, ’memory store’ with ’locations’. Let L denote
some set of store locations. Use l to range over L.

A memory store is then a (partial) function from L to values.

Vocabulary:
We will use µ to denote memory stores.
References will be called locations.
“memory store” wil be just store.

J. Carette (McMaster University) Types for references and memory Fall 2023 7 / 18
Store passing style
Attach µ directly to terms:
t |µ
Evaluation might affect the store; change evaluation relation:

t | µ → t ′ | µ′

New evaluation rules:

(λx : T11 .t12 )v2 | µ → [x 7→ v2 ]t12 | µ (E-AppAbs)

t1 | µ → t1′ | µ′
(E-App1)
t1 t2 | µ → t1′ t2 | µ′

t2 | µ → t2′ | µ′
(E-App2)
t1 t2 | µ → t1 t2′ | µ′

J. Carette (McMaster University) Types for references and memory Fall 2023 8 / 18
Dereferencing

New evaluation rules for dereferencing.

µ(l) = v
(E-DerefLoc)
!l | µ → v | µ

µ(l) = v
(E-DerefLoc)
!l | µ → v | µ

t1 | µ → t1′ | µ′
(E-Deref)
!t1 | µ → !t1′ | µ′

Dereferencing a location: if we have a value for that location, return it.


Otherwise evaluate t1 (possibly with an effect)
Note: ! 5 is stuck.

J. Carette (McMaster University) Types for references and memory Fall 2023 9 / 18
Assignment

l := v2 | µ → unit | [l 7→ v2 ]µ (E-Assign)

t1 | µ → t1′ | µ′
(E-Assign1)
t1 := t2 | µ → t1′ := t2 | µ′

t2 | µ → t2′ | µ′
(E-Assign2)
v1 := t2 | µ → v1 := t2′ | µ′

[l 7→ v2 ]µ means “a store which maps l to v , with all other locations


mapping to the same things as in µ.”

J. Carette (McMaster University) Types for references and memory Fall 2023 10 / 18
Allocation

l∈
/ dom(µ)
(E-RefV)
ref v1 | µ → l | µ ⊕ l 7→ v1

t1 | µ → t1′ | µ′
(E-Ref)
ref t1 | µ → ref t1′ | µ′

E-RefV: we select a fresh location l not already used in µ.


Extend µ with the new mapping
The term ref v evaluates to this fresh location l.

J. Carette (McMaster University) Types for references and memory Fall 2023 11 / 18
Typing the store

Skip entirely: a first “simple” approach that does not scale.

J. Carette (McMaster University) Types for references and memory Fall 2023 12 / 18
Typing the store

Skip entirely: a first “simple” approach that does not scale.

Recall: type of a location is derivable at allocation from the type of the


instantiating value.

J. Carette (McMaster University) Types for references and memory Fall 2023 12 / 18
Typing the store

Skip entirely: a first “simple” approach that does not scale.

Recall: type of a location is derivable at allocation from the type of the


instantiating value.

Create a typing store Σ in parallel with contexts Γ.

Σ(l) = T1
(T-Loc)
Γ | Σ ⊢ l : Ref T1

Γ starts off empty, and has typings added as the program is traversed.
Σ will be the same
Write empty Γ and emptr Σ as Ø.

J. Carette (McMaster University) Types for references and memory Fall 2023 12 / 18
Typing Allocation

Σ(l) = T1
(T-Loc)
Γ | Σ ⊢ l : Ref T1

Γ | Σ ⊢ t1 : T1
(T-Ref)
Γ | Σ ⊢ ref t1 : Ref T1

Γ | Σ ⊢ t1 : Ref T11
(T-Deref)
Γ | Σ ⊢!t1 : T11

Γ | Σ ⊢ t1 : Ref T11 Γ | Σ ⊢ t2 : T11


(T-Assign)
Γ | Σ ⊢ t1 := t2 : Unit

J. Carette (McMaster University) Types for references and memory Fall 2023 13 / 18
Typed Stores

Definition
A store µ is said to be well typed with respect to a typing context Γ and a
store typing Σ if:
dom(µ) = dom(Σ)
∀l ∈ dom(µ) | µ(l) : Σ(l)

We write this Γ | Σ ⊢ µ

J. Carette (McMaster University) Types for references and memory Fall 2023 14 / 18
Preservation?

(Γ | Σ ⊢ t : T )
∧ (t | µ → t ′ | µ′ )
∧ (Γ | Σ ⊢ µ)
=⇒ (Γ | Σ ⊢ t ′ : T )

But stepping can change µ and thus also Σ.

J. Carette (McMaster University) Types for references and memory Fall 2023 15 / 18
Preservation

THEOREM: [Preservation]

(Γ | Σ ⊢ t : T )
∧ (t | µ → t ′ | µ′ )
∧ (Γ | Σ ⊢ µ)
=⇒ (∃Σ′ ⊇ Σ |
(Γ | Σ′ ⊢ t ′ : T )
∧ (Γ | Σ′ ⊢ µ′ )
)

J. Carette (McMaster University) Types for references and memory Fall 2023 16 / 18
Technical lemmas

LEMMA: [Preservation Over Substitution]

(Γ, x : S | Σ ⊢ t : T ) ∧ (Γ | Σ ⊢ s : S) =⇒ (Γ | Σ ⊢ [x 7→ s]t : T ]) (1)

LEMMA: [Preservation Over Storage]

(Γ | Σ ⊢ µ) ∧ (Σ(l) = T ) ∧ (Γ | Σ ⊢ v : T ) =⇒ (Γ | Σ ⊢ [l 7→ v ]µ)) (2)

LEMMA: [Weakening Over Typing Stores]

(Γ | Σ ⊢ t : T ) ∧ (Σ′ ⊇ Σ) =⇒ (Γ | Σ′ ⊢ t : T ) (3)

J. Carette (McMaster University) Types for references and memory Fall 2023 17 / 18
Progress

THEOREM: [Progress]
Suppose Ø | Σ ⊢ t : T for some T and Σ. Then either t is a value, or else,
for any store µ such that Ø | Σ ⊢ µ, there is some term t ′ and store µ′ such
that t | µ → t ′ | µ′ .

Proof Sketch
Induction on typing derivations.
The canonical forms lemma needs two additional cases, stating that all
values of type Ref T are locations, and similarly for Unit.

J. Carette (McMaster University) Types for references and memory Fall 2023 18 / 18

You might also like