Professional Documents
Culture Documents
Types 4
Types 4
Types 4
J. Carette
McMaster University
Fall 2023
J. Carette (McMaster University) Types for references and memory Fall 2023 1 / 18
About :=
J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
About :=
J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
About :=
J. Carette (McMaster University) Types for references and memory Fall 2023 2 / 18
References
Γ ⊢ 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
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
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.
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 ′ | µ′
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
µ(l) = v
(E-DerefLoc)
!l | µ → v | µ
µ(l) = v
(E-DerefLoc)
!l | µ → v | µ
t1 | µ → t1′ | µ′
(E-Deref)
!t1 | µ → !t1′ | µ′
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′ | µ′
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′ | µ′
J. Carette (McMaster University) Types for references and memory Fall 2023 11 / 18
Typing the store
J. Carette (McMaster University) Types for references and memory Fall 2023 12 / 18
Typing the store
J. Carette (McMaster University) Types for references and memory Fall 2023 12 / 18
Typing the store
Σ(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
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 )
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
(Γ | Σ ⊢ 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