Adjustable References Viktor Vafeiadis Max Planck Institute for - - PowerPoint PPT Presentation

adjustable references
SMART_READER_LITE
LIVE PREVIEW

Adjustable References Viktor Vafeiadis Max Planck Institute for - - PowerPoint PPT Presentation

Adjustable References Viktor Vafeiadis Max Planck Institute for Software Systems (MPI-SWS) Viktor Vafeiadis Adjustable References Introduction Reasoning about imperative programs How to represent mutable state? Two standard approaches Deep


slide-1
SLIDE 1

Adjustable References

Viktor Vafeiadis

Max Planck Institute for Software Systems (MPI-SWS)

Viktor Vafeiadis Adjustable References

slide-2
SLIDE 2

Introduction Reasoning about imperative programs How to represent mutable state? Two standard approaches Deep embedding Monadic What if state is only used as an optimisation?

Viktor Vafeiadis Adjustable References

slide-3
SLIDE 3

Memoization let memo f = let h = Hashtbl.create() in λa. match Hashtbl.get h a with | Some b → b | None → let b = f a in Hashtbl.add h a b; b

Viktor Vafeiadis Adjustable References

slide-4
SLIDE 4

Memoization let memo f = let h = Hashtbl.create() in λa. match Hashtbl.get h a with | Some b → b | None → let b = f a in Hashtbl.add h a b; b Intuitively, memo f = f.

Viktor Vafeiadis Adjustable References

slide-5
SLIDE 5

Memoization let memo f = let h = Hashtbl.create() in λa. match Hashtbl.get h a with | Some b → b | None → let b = f a in Hashtbl.add h a b; b But with monads, f : A → B and memo f : A → State B.

Viktor Vafeiadis Adjustable References

slide-6
SLIDE 6

Adjustable References Like mutable references, but mutation is not observable. Internal representation type, R. Observation type, T. Observation function, f : R → T. Parameter aref : ∀R T, (R → T) → Type.

Viktor Vafeiadis Adjustable References

slide-7
SLIDE 7

Adjustable References (2) Parameter aval : ∀R T (f : R → T), R → aref f . Parameter aget : ∀R T (f : R → T), aref f → T. Axiom aref inhabited : ∀R T (f : R → T) (r : aref f ), ∃v, r = aval f v. Axiom agetval : ∀R T (f : R → T) v, aget(aval f v) = f v.

Viktor Vafeiadis Adjustable References

slide-8
SLIDE 8

Unobservable Updates Naively, adjupd : ∀R T (f : R → T) (r : aref f ) (v : R), f v = aget r → unit . Problems:

1 Evaluation order unknown & Coq can discard

unused results

2 The new internal value cannot depend on the

  • ld internal value.

3 Not useful unless T is a function type. Viktor Vafeiadis Adjustable References

slide-9
SLIDE 9

Adjusting Reads Parameter agetu : ∀R A B (f : R → A → B) (upd : R → A → R × B) (PF : ∀x a, f (fst (upd x a)) = f x) (PF ′ : ∀x a, snd (upd x a) = f x a), aref f → A → B. Axiom agetuE : ∀R A B (f : R → A → B) upd PF PF ′, agetu upd PF PF ′ = aget (f := f ).

Viktor Vafeiadis Adjustable References

slide-10
SLIDE 10

Allocations Parameter anew : ∀R1 T1 (f1 : R1 → T1) (r : aref f1) R2 T2 (f2 : R2 → T2) (g : ∀v, aget r = f1 v → R2) (PF : ∀x px y py, f2 (g x px) = f2 (g y py)), aref f2. Axiom anewval : ∀R1 T1 (f1 : R1 → T1) v R2 T2 (f2 : R2 → T2) g PF, anew (aval f1 v) g PF = aval f2 (g v (agetval f1 v)).

Viktor Vafeiadis Adjustable References

slide-11
SLIDE 11

Consistency Trivial; just ignore the adjustments. Definition aref R T f := R. Definition aval R T f v := v. Definition aget R T f r := f r. Definition agetu R A B f upd p p′ r := f r. Definition anew R1 T1 f1 r R2 T2 f2 g p := g r eq refl.

Viktor Vafeiadis Adjustable References

slide-12
SLIDE 12

Imperative Implementation type (′r, ′t) aref =

′r ref

let aval x = ref x let aget f r = f !r let agetu u r a = let (v, b) = u !r a in r := v; b let anew r g = ref (g !r ())

Viktor Vafeiadis Adjustable References

slide-13
SLIDE 13

Memoization Using Adjustable References (1) Section Memo. Variables (AB : Type) (f : A → B). Variable eqA : ∀x y : A, {x = y} + {x = y}. Variable hash : A → int. Definition cache := {c : Parray.t (option(A × B)) | ∀x a b, Parray.get c x = Some(a, b) → b = f a}.

Viktor Vafeiadis Adjustable References

slide-14
SLIDE 14

Memoization Using Adjustable References (2) Program Definition mupd(c : cache)(a : A) := let h := hash a in match Parray.get c h with | None ⇒ let b := f a in (Parray.set c h (Some(a, b)), b) | Some(a′, b) ⇒ if eqA a a′ then (c, b) else let b := f a in (Parray.set c h (Some(a, b)), b) end.... Program Definition memo := let r := aval (λc, f ) (Parray.create 100 None) in agetu mupd r....

Viktor Vafeiadis Adjustable References

slide-15
SLIDE 15

Memoization Using Adjustable References (3) Lemma memo eq : memo = f . Proof. by unfold memo; rewrite arefgetuE, agetval. Qed. End Memo.

Viktor Vafeiadis Adjustable References

slide-16
SLIDE 16

Other examples Data structures that optimize their shape even when performing read-only operations. Union-find path compression (in the paper) Splay trees

Viktor Vafeiadis Adjustable References

slide-17
SLIDE 17

Limitation Cannot have any observable updates. But observable updates are fine... ...provided that nobody observes them. In a sequential setting, you can do a sequence of updates, whose total effect is unobservable.

  • cf. Conchon and Filliˆ

atre persistent arrays.

Viktor Vafeiadis Adjustable References

slide-18
SLIDE 18

Future work Overcome the limitation! runST : ∀c : AdjStateMonad A. c is logically pure → A . Formally justify the imperative implementation Develop applications of adjustable references

Viktor Vafeiadis Adjustable References