heap_lang.v 12.4 KB
Newer Older
1
Require Export program_logic.language prelude.strings.
2
Require Import prelude.gmap.
3

4
5
Module heap_lang.
(** Expressions and vals. *)
6
Definition loc := positive. (* Really, any countable type. *)
Ralf Jung's avatar
Ralf Jung committed
7

8
9
10
11
12
13
14
Inductive base_lit : Set :=
  | LitNat (n : nat) | LitBool (b : bool) | LitUnit.
Inductive un_op : Set :=
  | NegOp.
Inductive bin_op : Set :=
  | PlusOp | MinusOp | LeOp | LtOp | EqOp.

Ralf Jung's avatar
Ralf Jung committed
15
Inductive expr :=
16
  (* Base lambda calculus *)
17
18
  | Var (x : string)
  | Rec (f x : string) (e : expr)
19
  | App (e1 e2 : expr)
20
21
22
23
24
  (* Base types and their operations *)
  | Lit (l : base_lit)
  | UnOp (op : un_op) (e : expr)
  | BinOp (op : bin_op) (e1 e2 : expr)
  | If (e0 e1 e2 : expr)
25
26
27
28
29
30
31
  (* Products *)
  | Pair (e1 e2 : expr)
  | Fst (e : expr)
  | Snd (e : expr)
  (* Sums *)
  | InjL (e : expr)
  | InjR (e : expr)
32
  | Case (e0 : expr) (x1 : string) (e1 : expr) (x2 : string) (e2 : expr)
33
34
35
36
37
38
39
40
  (* Concurrency *)
  | Fork (e : expr)
  (* Heap *)
  | Loc (l : loc)
  | Alloc (e : expr)
  | Load (e : expr)
  | Store (e1 : expr) (e2 : expr)
  | Cas (e0 : expr) (e1 : expr) (e2 : expr).
Ralf Jung's avatar
Ralf Jung committed
41

42
Inductive val :=
43
  | RecV (f x : string) (e : expr) (* e should be closed *)
44
  | LitV (l : base_lit)
45
46
47
48
  | PairV (v1 v2 : val)
  | InjLV (v : val)
  | InjRV (v : val)
  | LocV (l : loc).
Ralf Jung's avatar
Ralf Jung committed
49

50
Fixpoint of_val (v : val) : expr :=
Ralf Jung's avatar
Ralf Jung committed
51
  match v with
52
  | RecV f x e => Rec f x e
53
  | LitV l => Lit l
54
55
56
  | PairV v1 v2 => Pair (of_val v1) (of_val v2)
  | InjLV v => InjL (of_val v)
  | InjRV v => InjR (of_val v)
57
  | LocV l => Loc l
Ralf Jung's avatar
Ralf Jung committed
58
  end.
59
Fixpoint to_val (e : expr) : option val :=
60
  match e with
61
  | Rec f x e => Some (RecV f x e)
62
  | Lit l => Some (LitV l)
63
64
65
  | Pair e1 e2 => v1  to_val e1; v2  to_val e2; Some (PairV v1 v2)
  | InjL e => InjLV <$> to_val e
  | InjR e => InjRV <$> to_val e
66
  | Loc l => Some (LocV l)
Ralf Jung's avatar
Ralf Jung committed
67
  | _ => None
68
69
  end.

70
71
(** The state: heaps of vals. *)
Definition state := gmap loc val.
Ralf Jung's avatar
Ralf Jung committed
72

73
(** Evaluation contexts *)
74
75
76
Inductive ectx_item :=
  | AppLCtx (e2 : expr)
  | AppRCtx (v1 : val)
77
78
79
80
  | UnOpCtx (op : un_op)
  | BinOpLCtx (op : bin_op) (e2 : expr)
  | BinOpRCtx (op : bin_op) (v1 : val)
  | IfCtx (e1 e2 : expr)
81
82
83
84
85
86
  | PairLCtx (e2 : expr)
  | PairRCtx (v1 : val)
  | FstCtx
  | SndCtx
  | InjLCtx
  | InjRCtx
87
  | CaseCtx (x1 : string) (e1 : expr) (x2 : string) (e2 : expr)
88
89
90
91
92
93
94
  | AllocCtx
  | LoadCtx
  | StoreLCtx (e2 : expr)
  | StoreRCtx (v1 : val)
  | CasLCtx (e1 : expr)  (e2 : expr)
  | CasMCtx (v0 : val) (e2 : expr)
  | CasRCtx (v0 : val) (v1 : val).
95

96
Notation ectx := (list ectx_item).
97

98
Definition fill_item (Ki : ectx_item) (e : expr) : expr :=
99
100
101
  match Ki with
  | AppLCtx e2 => App e e2
  | AppRCtx v1 => App (of_val v1) e
102
103
104
105
  | UnOpCtx op => UnOp op e
  | BinOpLCtx op e2 => BinOp op e e2
  | BinOpRCtx op v1 => BinOp op (of_val v1) e
  | IfCtx e1 e2 => If e e1 e2
106
107
108
109
110
111
  | PairLCtx e2 => Pair e e2
  | PairRCtx v1 => Pair (of_val v1) e
  | FstCtx => Fst e
  | SndCtx => Snd e
  | InjLCtx => InjL e
  | InjRCtx => InjR e
112
  | CaseCtx x1 e1 x2 e2 => Case e x1 e1 x2 e2
113
114
115
116
117
118
119
  | AllocCtx => Alloc e
  | LoadCtx => Load e
  | StoreLCtx e2 => Store e e2
  | StoreRCtx v1 => Store (of_val v1) e
  | CasLCtx e1 e2 => Cas e e1 e2
  | CasMCtx v0 e2 => Cas (of_val v0) e e2
  | CasRCtx v0 v1 => Cas (of_val v0) (of_val v1) e
Ralf Jung's avatar
Ralf Jung committed
120
  end.
121
Definition fill (K : ectx) (e : expr) : expr := fold_right fill_item e K.
Ralf Jung's avatar
Ralf Jung committed
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(** Substitution *)
(** We have [subst e "" v = e] to deal with anonymous binders *)
Fixpoint subst (e : expr) (x : string) (v : val) : expr :=
  match e with
  | Var y => if decide (x = y  x  "") then of_val v else Var y
  | Rec f y e => Rec f y (if decide (x  f  x  y) then subst e x v else e)
  | App e1 e2 => App (subst e1 x v) (subst e2 x v)
  | Lit l => Lit l
  | UnOp op e => UnOp op (subst e x v)
  | BinOp op e1 e2 => BinOp op (subst e1 x v) (subst e2 x v)
  | If e0 e1 e2 => If (subst e0 x v) (subst e1 x v) (subst e2 x v)
  | Pair e1 e2 => Pair (subst e1 x v) (subst e2 x v)
  | Fst e => Fst (subst e x v)
  | Snd e => Snd (subst e x v)
  | InjL e => InjL (subst e x v)
  | InjR e => InjR (subst e x v)
  | Case e0 x1 e1 x2 e2 =>
     Case (subst e0 x v)
       x1 (if decide (x  x1) then subst e1 x v else e1)
       x2 (if decide (x  x2) then subst e2 x v else e2)
  | Fork e => Fork (subst e x v)
  | Loc l => Loc l
  | Alloc e => Alloc (subst e x v)
  | Load e => Load (subst e x v)
  | Store e1 e2 => Store (subst e1 x v) (subst e2 x v)
  | Cas e0 e1 e2 => Cas (subst e0 x v) (subst e1 x v) (subst e2 x v)
  end.

151
(** The stepping relation *)
152
153
Definition un_op_eval (op : un_op) (l : base_lit) : option base_lit :=
  match op, l with
154
  | NegOp, LitBool b => Some (LitBool (negb b))
155
156
157
158
159
  | _, _ => None
  end.

Definition bin_op_eval (op : bin_op) (l1 l2 : base_lit) : option base_lit :=
  match op, l1, l2 with
160
161
162
163
164
  | PlusOp, LitNat n1, LitNat n2 => Some $ LitNat (n1 + n2)
  | MinusOp, LitNat n1, LitNat n2 => Some $ LitNat (n1 - n2)
  | LeOp, LitNat n1, LitNat n2 => Some $ LitBool $ bool_decide (n1  n2)
  | LtOp, LitNat n1, LitNat n2 => Some $ LitBool $ bool_decide (n1 < n2)
  | EqOp, LitNat n1, LitNat n2 => Some $ LitBool $ bool_decide (n1 = n2)
165
166
167
  | _, _, _ => None
  end.

168
Inductive head_step : expr -> state -> expr -> state -> option expr -> Prop :=
169
  | BetaS f x e1 e2 v2 σ :
170
     to_val e2 = Some v2 
171
172
173
     head_step (App (Rec f x e1) e2) σ
       (subst (subst e1 f (RecV f x e1)) x v2) σ None
  | UnOpS op l l' σ :
174
175
     un_op_eval op l = Some l'  
     head_step (UnOp op (Lit l)) σ (Lit l') σ None
176
  | BinOpS op l1 l2 l' σ :
177
178
179
     bin_op_eval op l1 l2 = Some l'  
     head_step (BinOp op (Lit l1) (Lit l2)) σ (Lit l') σ None
  | IfTrueS e1 e2 σ :
Ralf Jung's avatar
Ralf Jung committed
180
     head_step (If (Lit $ LitBool true) e1 e2) σ e1 σ None
181
  | IfFalseS e1 e2 σ :
Ralf Jung's avatar
Ralf Jung committed
182
     head_step (If (Lit $ LitBool false) e1 e2) σ e2 σ None
183
184
185
186
187
188
  | FstS e1 v1 e2 v2 σ :
     to_val e1 = Some v1  to_val e2 = Some v2 
     head_step (Fst (Pair e1 e2)) σ e1 σ None
  | SndS e1 v1 e2 v2 σ :
     to_val e1 = Some v1  to_val e2 = Some v2 
     head_step (Snd (Pair e1 e2)) σ e2 σ None
189
  | CaseLS e0 v0 x1 e1 x2 e2 σ :
190
     to_val e0 = Some v0 
191
192
     head_step (Case (InjL e0) x1 e1 x2 e2) σ (subst e1 x1 v0) σ None
  | CaseRS e0 v0 x1 e1 x2 e2 σ :
193
     to_val e0 = Some v0 
194
     head_step (Case (InjR e0) x1 e1 x2 e2) σ (subst e2 x2 v0) σ None
195
  | ForkS e σ:
196
     head_step (Fork e) σ (Lit LitUnit) σ (Some e)
197
198
199
200
201
202
203
204
  | AllocS e v σ l :
     to_val e = Some v  σ !! l = None 
     head_step (Alloc e) σ (Loc l) (<[l:=v]>σ) None
  | LoadS l v σ :
     σ !! l = Some v 
     head_step (Load (Loc l)) σ (of_val v) σ None
  | StoreS l e v σ :
     to_val e = Some v  is_Some (σ !! l) 
205
     head_step (Store (Loc l) e) σ (Lit LitUnit) (<[l:=v]>σ) None
206
207
208
  | CasFailS l e1 v1 e2 v2 vl σ :
     to_val e1 = Some v1  to_val e2 = Some v2 
     σ !! l = Some vl  vl  v1 
Ralf Jung's avatar
Ralf Jung committed
209
     head_step (Cas (Loc l) e1 e2) σ (Lit $ LitBool false) σ None
210
211
212
  | CasSucS l e1 v1 e2 v2 σ :
     to_val e1 = Some v1  to_val e2 = Some v2 
     σ !! l = Some v1 
Ralf Jung's avatar
Ralf Jung committed
213
     head_step (Cas (Loc l) e1 e2) σ (Lit $ LitBool true) (<[l:=v2]>σ) None.
Ralf Jung's avatar
Ralf Jung committed
214

215
(** Atomic expressions *)
216
Definition atomic (e: expr) : Prop :=
217
218
219
220
221
222
223
  match e with
  | Alloc e => is_Some (to_val e)
  | Load e => is_Some (to_val e)
  | Store e1 e2 => is_Some (to_val e1)  is_Some (to_val e2)
  | Cas e0 e1 e2 => is_Some (to_val e0)  is_Some (to_val e1)  is_Some (to_val e2)
  | _ => False
  end.
224

225
226
227
228
(** Close reduction under evaluation contexts.
We could potentially make this a generic construction. *)
Inductive prim_step
    (e1 : expr) (σ1 : state) (e2 : expr) (σ2: state) (ef: option expr) : Prop :=
229
  Ectx_step K e1' e2' :
230
231
232
233
234
235
    e1 = fill K e1'  e2 = fill K e2' 
    head_step e1' σ1 e2' σ2 ef  prim_step e1 σ1 e2 σ2 ef.

(** Basic properties about the language *)
Lemma to_of_val v : to_val (of_val v) = Some v.
Proof. by induction v; simplify_option_equality. Qed.
236

237
Lemma of_to_val e v : to_val e = Some v  of_val v = e.
238
Proof.
239
  revert v; induction e; intros; simplify_option_equality; auto with f_equal.
240
Qed.
241

242
243
Instance: Injective (=) (=) of_val.
Proof. by intros ?? Hv; apply (injective Some); rewrite -!to_of_val Hv. Qed.
244

245
Instance fill_item_inj Ki : Injective (=) (=) (fill_item Ki).
246
Proof. destruct Ki; intros ???; simplify_equality'; auto with f_equal. Qed.
247

248
249
Instance ectx_fill_inj K : Injective (=) (=) (fill K).
Proof. red; induction K as [|Ki K IH]; naive_solver. Qed.
250

251
252
Lemma fill_app K1 K2 e : fill (K1 ++ K2) e = fill K1 (fill K2 e).
Proof. revert e; induction K1; simpl; auto with f_equal. Qed.
253

254
Lemma fill_val K e : is_Some (to_val (fill K e))  is_Some (to_val e).
255
Proof.
256
257
  intros [v' Hv']; revert v' Hv'.
  induction K as [|[]]; intros; simplify_option_equality; eauto.
258
Qed.
259

260
261
Lemma fill_not_val K e : to_val e = None  to_val (fill K e) = None.
Proof. rewrite !eq_None_not_Some; eauto using fill_val. Qed.
262

263
264
265
Lemma values_head_stuck e1 σ1 e2 σ2 ef :
  head_step e1 σ1 e2 σ2 ef  to_val e1 = None.
Proof. destruct 1; naive_solver. Qed.
266

267
268
Lemma values_stuck e1 σ1 e2 σ2 ef : prim_step e1 σ1 e2 σ2 ef  to_val e1 = None.
Proof. intros [??? -> -> ?]; eauto using fill_not_val, values_head_stuck. Qed.
269

270
271
Lemma atomic_not_val e : atomic e  to_val e = None.
Proof. destruct e; naive_solver. Qed.
272

273
Lemma atomic_fill K e : atomic (fill K e)  to_val e = None  K = [].
274
Proof.
275
276
  rewrite eq_None_not_Some.
  destruct K as [|[]]; naive_solver eauto using fill_val.
277
Qed.
278

279
280
281
Lemma atomic_head_step e1 σ1 e2 σ2 ef :
  atomic e1  head_step e1 σ1 e2 σ2 ef  is_Some (to_val e2).
Proof. destruct 2; simpl; rewrite ?to_of_val; naive_solver. Qed.
282

283
284
Lemma atomic_step e1 σ1 e2 σ2 ef :
  atomic e1  prim_step e1 σ1 e2 σ2 ef  is_Some (to_val e2).
285
Proof.
286
287
288
  intros Hatomic [K e1' e2' -> -> Hstep].
  assert (K = []) as -> by eauto 10 using atomic_fill, values_head_stuck.
  naive_solver eauto using atomic_head_step.
Ralf Jung's avatar
Ralf Jung committed
289
Qed.
290

291
Lemma head_ctx_step_val Ki e σ1 e2 σ2 ef :
292
  head_step (fill_item Ki e) σ1 e2 σ2 ef  is_Some (to_val e).
293
Proof. destruct Ki; inversion_clear 1; simplify_option_equality; eauto. Qed.
294

295
Lemma fill_item_no_val_inj Ki1 Ki2 e1 e2 :
296
  to_val e1 = None  to_val e2 = None 
297
  fill_item Ki1 e1 = fill_item Ki2 e2  Ki1 = Ki2.
298
Proof.
299
  destruct Ki1, Ki2; intros; try discriminate; simplify_equality';
300
    repeat match goal with
301
302
    | H : to_val (of_val _) = None |- _ => by rewrite to_of_val in H
    end; auto.
Ralf Jung's avatar
Ralf Jung committed
303
Qed.
304

305
306
307
308
309
310
(* When something does a step, and another decomposition of the same expression
has a non-val [e] in the hole, then [K] is a left sub-context of [K'] - in
other words, [e] also contains the reducible expression *)
Lemma step_by_val K K' e1 e1' σ1 e2 σ2 ef :
  fill K e1 = fill K' e1'  to_val e1 = None  head_step e1' σ1 e2 σ2 ef 
  K `prefix_of` K'.
311
Proof.
312
313
314
  intros Hfill Hred Hnval; revert K' Hfill.
  induction K as [|Ki K IH]; simpl; intros K' Hfill; auto using prefix_of_nil.
  destruct K' as [|Ki' K']; simplify_equality'.
Ralf Jung's avatar
Ralf Jung committed
315
  { exfalso; apply (eq_None_not_Some (to_val (fill K e1)));
316
317
      eauto using fill_not_val, head_ctx_step_val. }
  cut (Ki = Ki'); [naive_solver eauto using prefix_of_cons|].
318
  eauto using fill_item_no_val_inj, values_head_stuck, fill_not_val.
319
Qed.
320

321
322
323
Lemma alloc_fresh e v σ :
  let l := fresh (dom _ σ) in
  to_val e = Some v  head_step (Alloc e) σ (Loc l) (<[l:=v]>σ) None.
324
Proof. by intros; apply AllocS, (not_elem_of_dom (D:=gset _)), is_fresh. Qed.
325

326
327
Lemma subst_empty e v : subst e "" v = e.
Proof. induction e; simpl; repeat case_decide; intuition auto with f_equal. Qed.
328
329
330
331
332
333
334
335
336
337
End heap_lang.

(** Language *)
Program Canonical Structure heap_lang : language := {|
  expr := heap_lang.expr; val := heap_lang.val; state := heap_lang.state;
  of_val := heap_lang.of_val; to_val := heap_lang.to_val;
  atomic := heap_lang.atomic; prim_step := heap_lang.prim_step;
|}.
Solve Obligations with eauto using heap_lang.to_of_val, heap_lang.of_to_val,
  heap_lang.values_stuck, heap_lang.atomic_not_val, heap_lang.atomic_step.
338

339
Global Instance heap_lang_ctx K : LanguageCtx heap_lang (heap_lang.fill K).
340
Proof.
341
342
  split.
  * eauto using heap_lang.fill_not_val.
343
  * intros ????? [K' e1' e2' Heq1 Heq2 Hstep].
344
    by exists (K ++ K') e1' e2'; rewrite ?heap_lang.fill_app ?Heq1 ?Heq2.
345
  * intros e1 σ1 e2 σ2 ? Hnval [K'' e1'' e2'' Heq1 -> Hstep].
346
347
348
    destruct (heap_lang.step_by_val
      K K'' e1 e1'' σ1 e2'' σ2 ef) as [K' ->]; eauto.
    rewrite heap_lang.fill_app in Heq1; apply (injective _) in Heq1.
Ralf Jung's avatar
Ralf Jung committed
349
    exists (heap_lang.fill K' e2''); rewrite heap_lang.fill_app; split; auto.
350
    econstructor; eauto.
351
Qed.
352
353
354
355
356
357
358

Global Instance heap_lang_ctx_item Ki :
  LanguageCtx heap_lang (heap_lang.fill_item Ki).
Proof.
  change (LanguageCtx heap_lang (heap_lang.fill [Ki])).
  by apply _.
Qed.