Hux: this chapter is very similar to TAPL - ch13 References
But under a “formal verification” concept, it’s more interesting and practical and push you to think about it!
computational effects - “side effects” of computation - impure features
- assign to mutable variables (reference cells, arrays, mutable record fields, etc.)
- perform input and output to files, displays, or network connections;
- make non-local transfers of control via exceptions, jumps, or continuations;
- engage in inter-process synchronization and communication
The main extension will be dealing explicitly with a
- store (or heap) and
- pointers (or reference) that name store locations, or address…
interesting refinement: type preservation
Definition
forms of assignments:
- rare : Gallina - No
- some : ML family - Explicit reference and dereference
- most : C family - Implicit …
For formal study, use ML’s model.
Syntax
Types & Terms
1 | T ::= |
1 | Inductive ty : Type := |
Typing
Gamma |- t1 : T1
------------------------ (T_Ref)
Gamma |- ref t1 : Ref T1
Gamma |- t1 : Ref T11
--------------------- (T_Deref)
Gamma |- !t1 : T11
Gamma |- t1 : Ref T11
Gamma |- t2 : T11
------------------------ (T_Assign)
Gamma |- t1 := t2 : Unit
Values and Substitution
1 | Inductive value : tm → Prop := |
1 | Fixpoint subst (x:string) (s:tm) (t:tm) : tm := |
Pragmatics
Side Effects and Sequencing
r:=succ(!r); !r
can be desugar to
(\x:Unit. !r) (r:=succ(!r)).
then we can write some “imperative programming”
r:=succ(!r);
r:=succ(!r);
r:=succ(!r);
!r
References and Aliasing
shared reference brings _shared state
let r = ref 5 in
let s = r in
s := 82;
(!r)+1
Shared State
thunks as methods
1 |
|
Objects
constructor and encapsulation!
1 |
|
References to Compound Types (e.g. Function Type)
Previously, we use closure to represent map, with functional update
这里的”数组” (这个到底算不算数组估计都有争议,虽然的确提供了 index 但是这个显然是 O(n) 都不知道算不算 random access…
并不是 in-place update 里面的数据的,仅仅是一个 ref
包住的 map 而已 (仅仅是多了可以 shared
其实或许 list (ref nat)
也可以表达数组? 反正都是 O(n) 每次都 linear search 也一样……
1 |
|
Null References
nullptr!
Deref a nullptr:
- exception in Java/C#
- insecure in C/C++ <– violate memory safety!!
1 |
|
Why is Option
outside?
think about C, nullptr
is A special const location, like Unit
(None
in terms of datacon) here.
Garbage Collection
last issue: store de-allocation
w/o GC, extremely difficult to achieve type safety…if a primitive for “explicit deallocation” provided
one can easily create dangling reference i.e. references -> deleted
One type-unsafe example: (pseudo code)
1 |
|
Operational Semantics
Locations
what should be the values of type
Ref T
?
ref
allocate some memory/storage!
run-time store is essentially big array of bytes.
different datatype need to allocate different size of space (region)
we think store as array of values, abstracting away different size of different values
we use the word location here to prevent from modeling pointer arithmetic, which is un-trackable by most type system
location n
is float
doesn’t tell you anything about location n+4
…
Stores
we defined replace
as Fixpoint
since it’s computational and easier. The consequence is it has to be total.
Reduction
Typing
typing context:
1 | Definition context := partial_map ty. |
Store typings
why not just make a context a map of pair?
we don’t want to complicate the dynamics of language,
and this store typing is only for type check.
The Typing Relation
Properties
Well-Typed Stores
Extending Store Typings
Preservation, Finally
Substitution Lemma
Assignment Preserves Store Typing
Weakening for Stores
Preservation!
Progress
References and Nontermination
- 本文标题:「SF-PLF」13 References
- 创建时间:2021-03-28 00:00:00
- 本文链接:posts/ed68.html
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!