heap_lang.v 16.4 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
Require Import Autosubst.Autosubst.
2
Require Import prelude.option prelude.gmap iris.language.
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
(** Some tactics useful when dealing with equality of sigma-like types: existT T0 t0 = existT T1 t1.
    They all assume such an equality is the first thing on the "stack" (goal). *)
Ltac case_depeq1 := let Heq := fresh "Heq" in
  case=>_ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,<-).
Ltac case_depeq2 := let Heq := fresh "Heq" in
  case=>_ _ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,Heq);
  case:Heq=>_ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,<-).
Ltac case_depeq3 := let Heq := fresh "Heq" in
  case=>_ _ _ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,Heq);
  case:Heq=>_ _ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,Heq);
  case:Heq=>_ /EqdepFacts.eq_sigT_sig_eq=>Heq;
  destruct Heq as (->,<-).

(** Expressions and values. *)
Ralf Jung's avatar
Ralf Jung committed
23 24
Definition loc := nat. (* Any countable type. *)

Ralf Jung's avatar
Ralf Jung committed
25
Inductive expr :=
26
(* Base lambda calculus *)
Ralf Jung's avatar
Ralf Jung committed
27
| Var (x : var)
Ralf Jung's avatar
Ralf Jung committed
28
| Rec (e : {bind 2 of expr}) (* These are recursive lambdas. *)
29
| App (e1 e2 : expr)
30
(* Embedding of Coq values and operations *)
31 32 33
| Lit {T : Type} (t: T)  (* arbitrary Coq values become literals *)
| Op1  {T1 To : Type} (f : T1 -> To) (e1 : expr)
| Op2  {T1 T2 To : Type} (f : T1 -> T2 -> To) (e1 : expr) (e2 : expr)
34
(* Products *)
Ralf Jung's avatar
Ralf Jung committed
35 36 37
| Pair (e1 e2 : expr)
| Fst (e : expr)
| Snd (e : expr)
38
(* Sums *)
Ralf Jung's avatar
Ralf Jung committed
39 40
| InjL (e : expr)
| InjR (e : expr)
Ralf Jung's avatar
Ralf Jung committed
41
| Case (e0 : expr) (e1 : {bind expr}) (e2 : {bind expr})
42 43 44 45
(* Concurrency *)
| Fork (e : expr)
(* Heap *)
| Loc (l : loc)
Ralf Jung's avatar
Ralf Jung committed
46
| Alloc (e : expr)
Ralf Jung's avatar
Ralf Jung committed
47 48 49 50
| Load (e : expr)
| Store (e1 : expr) (e2 : expr)
| Cas (e0 : expr) (e1 : expr) (e2 : expr)
.
Ralf Jung's avatar
Ralf Jung committed
51 52 53 54 55 56

Instance Ids_expr : Ids expr. derive. Defined.
Instance Rename_expr : Rename expr. derive. Defined.
Instance Subst_expr : Subst expr. derive. Defined.
Instance SubstLemmas_expr : SubstLemmas expr. derive. Qed.

Ralf Jung's avatar
Ralf Jung committed
57
Definition Lam (e: {bind expr}) := Rec (e.[up ids]).
58 59 60
Definition Let' (e1: expr) (e2: {bind expr}) := App (Lam e2) e1.
Definition Seq (e1 e2: expr) := Let' e1 (e2.[up ids]).

Ralf Jung's avatar
Ralf Jung committed
61
Inductive value :=
Ralf Jung's avatar
Ralf Jung committed
62
| RecV (e : {bind 2 of expr})
Ralf Jung's avatar
Ralf Jung committed
63
| LitV {T : Type} (t : T)  (* arbitrary Coq values become literal values *)
Ralf Jung's avatar
Ralf Jung committed
64 65
| PairV (v1 v2 : value)
| InjLV (v : value)
66
| InjRV (v : value)
Ralf Jung's avatar
Ralf Jung committed
67 68
| LocV (l : loc)
.
Ralf Jung's avatar
Ralf Jung committed
69

Ralf Jung's avatar
Ralf Jung committed
70 71 72 73 74 75 76
Definition LitUnit := Lit tt.
Definition LitVUnit := LitV tt.
Definition LitTrue := Lit true.
Definition LitVTrue := LitV true.
Definition LitFalse := Lit false.
Definition LitVFalse := LitV false.

77
Fixpoint v2e (v : value) : expr :=
Ralf Jung's avatar
Ralf Jung committed
78
  match v with
79
  | LitV _ t => Lit t
Ralf Jung's avatar
Ralf Jung committed
80
  | RecV e   => Rec e
Ralf Jung's avatar
Ralf Jung committed
81 82 83
  | PairV v1 v2 => Pair (v2e v1) (v2e v2)
  | InjLV v => InjL (v2e v)
  | InjRV v => InjR (v2e v)
84
  | LocV l => Loc l
Ralf Jung's avatar
Ralf Jung committed
85 86
  end.

87 88
Fixpoint e2v (e : expr) : option value :=
  match e with
Ralf Jung's avatar
Ralf Jung committed
89
  | Rec e => Some (RecV e)
Ralf Jung's avatar
Ralf Jung committed
90
  | Lit _ t => Some (LitV t)
91 92 93 94 95
  | Pair e1 e2 => v1  e2v e1;
                  v2  e2v e2;
                  Some (PairV v1 v2)
  | InjL e => InjLV <$> e2v e
  | InjR e => InjRV <$> e2v e
96
  | Loc l => Some (LocV l)
Ralf Jung's avatar
Ralf Jung committed
97
  | _ => None
98 99 100 101 102
  end.

Lemma v2v v:
  e2v (v2e v) = Some v.
Proof.
103
  induction v; simpl; rewrite ?IHv ?IHv1 /= ?IHv2; reflexivity.
104 105
Qed.

Ralf Jung's avatar
Ralf Jung committed
106
Section e2e. (* To get local tactics. *)
107
Lemma e2e e v:
108
  e2v e = Some v -> v2e v = e.
109
Proof.
Ralf Jung's avatar
Ralf Jung committed
110 111 112 113 114 115 116
  Ltac case0 := case =><-; simpl; eauto using f_equal, f_equal2.
  Ltac case1 e1 := destruct (e2v e1); simpl; [|discriminate];
                   case0.
  Ltac case2 e1 e2 := destruct (e2v e1); simpl; [|discriminate];
                      destruct (e2v e2); simpl; [|discriminate];
                      case0.

Ralf Jung's avatar
Ralf Jung committed
117 118
  revert v; induction e; intros v; simpl; try discriminate;
    by (case2 e1 e2 || case1 e || case0).
Ralf Jung's avatar
Ralf Jung committed
119 120 121 122 123 124
Qed.
End e2e.

Lemma v2e_inj v1 v2:
  v2e v1 = v2e v2 -> v1 = v2.
Proof.
125
  revert v2; induction v1=>v2; destruct v2; simpl; try discriminate;
Ralf Jung's avatar
Ralf Jung committed
126
    first [case_depeq1 | case]; eauto using f_equal, f_equal2.
127
Qed.
128

129 130
(** The state: heaps of values. *)
Definition state := gmap loc value.
Ralf Jung's avatar
Ralf Jung committed
131

132
(** Evaluation contexts *)
Ralf Jung's avatar
Ralf Jung committed
133 134
Inductive ectx :=
| EmptyCtx
Ralf Jung's avatar
Ralf Jung committed
135
| AppLCtx (K1 : ectx)  (e2 : expr)
Ralf Jung's avatar
Ralf Jung committed
136
| AppRCtx (v1 : value) (K2 : ectx)
137
| Op1Ctx {T1 To : Type} (f : T1 -> To) (K : ectx)
Ralf Jung's avatar
Ralf Jung committed
138
| Op2LCtx {T1 T2 To : Type} (f : T1 -> T2 -> To) (K1 : ectx)  (e2 : expr)
139
| Op2RCtx {T1 T2 To : Type} (f : T1 -> T2 -> To) (v1 : value) (K2 : ectx)
Ralf Jung's avatar
Ralf Jung committed
140
| PairLCtx (K1 : ectx)  (e2 : expr)
Ralf Jung's avatar
Ralf Jung committed
141 142 143 144 145
| PairRCtx (v1 : value) (K2 : ectx)
| FstCtx (K : ectx)
| SndCtx (K : ectx)
| InjLCtx (K : ectx)
| InjRCtx (K : ectx)
146
| CaseCtx (K : ectx) (e1 : {bind expr}) (e2 : {bind expr})
Ralf Jung's avatar
Ralf Jung committed
147
| AllocCtx (K : ectx)
Ralf Jung's avatar
Ralf Jung committed
148 149 150 151 152 153 154
| LoadCtx (K : ectx)
| StoreLCtx (K1 : ectx)  (e2 : expr)
| StoreRCtx (v1 : value) (K2 : ectx)
| CasLCtx (K0 : ectx)  (e1 : expr)  (e2 : expr)
| CasMCtx (v0 : value) (K1 : ectx)  (e2 : expr)
| CasRCtx (v0 : value) (v1 : value) (K2 : ectx)
.
Ralf Jung's avatar
Ralf Jung committed
155 156 157 158 159 160

Fixpoint fill (K : ectx) (e : expr) :=
  match K with
  | EmptyCtx => e
  | AppLCtx K1 e2 => App (fill K1 e) e2
  | AppRCtx v1 K2 => App (v2e v1) (fill K2 e)
161 162 163
  | Op1Ctx _ _ f K => Op1 f (fill K e)
  | Op2LCtx _ _ _ f K1 e2 => Op2 f (fill K1 e) e2
  | Op2RCtx _ _ _ f v1 K2 => Op2 f (v2e v1) (fill K2 e)
Ralf Jung's avatar
Ralf Jung committed
164 165 166 167 168 169 170
  | PairLCtx K1 e2 => Pair (fill K1 e) e2
  | PairRCtx v1 K2 => Pair (v2e v1) (fill K2 e)
  | FstCtx K => Fst (fill K e)
  | SndCtx K => Snd (fill K e)
  | InjLCtx K => InjL (fill K e)
  | InjRCtx K => InjR (fill K e)
  | CaseCtx K e1 e2 => Case (fill K e) e1 e2
Ralf Jung's avatar
Ralf Jung committed
171
  | AllocCtx K => Alloc (fill K e)
172
  | LoadCtx K => Load (fill K e)
Ralf Jung's avatar
Ralf Jung committed
173 174 175 176 177
  | StoreLCtx K1 e2 => Store (fill K1 e) e2
  | StoreRCtx v1 K2 => Store (v2e v1) (fill K2 e)
  | CasLCtx K0 e1 e2 => Cas (fill K0 e) e1 e2
  | CasMCtx v0 K1 e2 => Cas (v2e v0) (fill K1 e) e2
  | CasRCtx v0 v1 K2 => Cas (v2e v0) (v2e v1) (fill K2 e)
Ralf Jung's avatar
Ralf Jung committed
178 179 180 181 182 183 184
  end.

Fixpoint comp_ctx (Ko : ectx) (Ki : ectx) :=
  match Ko with
  | EmptyCtx => Ki
  | AppLCtx K1 e2 => AppLCtx (comp_ctx K1 Ki) e2
  | AppRCtx v1 K2 => AppRCtx v1 (comp_ctx K2 Ki)
185 186 187
  | Op1Ctx _ _ f K => Op1Ctx f (comp_ctx K Ki)
  | Op2LCtx _ _ _ f K1 e2 => Op2LCtx f (comp_ctx K1 Ki) e2
  | Op2RCtx _ _ _ f v1 K2 => Op2RCtx f v1 (comp_ctx K2 Ki)
Ralf Jung's avatar
Ralf Jung committed
188 189 190 191 192 193 194
  | PairLCtx K1 e2 => PairLCtx (comp_ctx K1 Ki) e2
  | PairRCtx v1 K2 => PairRCtx v1 (comp_ctx K2 Ki)
  | FstCtx K => FstCtx (comp_ctx K Ki)
  | SndCtx K => SndCtx (comp_ctx K Ki)
  | InjLCtx K => InjLCtx (comp_ctx K Ki)
  | InjRCtx K => InjRCtx (comp_ctx K Ki)
  | CaseCtx K e1 e2 => CaseCtx (comp_ctx K Ki) e1 e2
Ralf Jung's avatar
Ralf Jung committed
195
  | AllocCtx K => AllocCtx (comp_ctx K Ki)
196
  | LoadCtx K => LoadCtx (comp_ctx K Ki)
Ralf Jung's avatar
Ralf Jung committed
197 198 199 200 201
  | StoreLCtx K1 e2 => StoreLCtx (comp_ctx K1 Ki) e2
  | StoreRCtx v1 K2 => StoreRCtx v1 (comp_ctx K2 Ki)
  | CasLCtx K0 e1 e2 => CasLCtx (comp_ctx K0 Ki) e1 e2
  | CasMCtx v0 K1 e2 => CasMCtx v0 (comp_ctx K1 Ki) e2
  | CasRCtx v0 v1 K2 => CasRCtx v0 v1 (comp_ctx K2 Ki)
Ralf Jung's avatar
Ralf Jung committed
202 203 204 205 206 207 208 209 210 211 212
  end.

Lemma fill_empty e :
  fill EmptyCtx e = e.
Proof.
  reflexivity.
Qed.

Lemma fill_comp K1 K2 e :
  fill K1 (fill K2 e) = fill (comp_ctx K1 K2) e.
Proof.
213
  revert K2 e; induction K1 => K2 e /=; rewrite ?IHK1 ?IHK2; reflexivity.
Ralf Jung's avatar
Ralf Jung committed
214 215 216 217 218
Qed.

Lemma fill_inj_r K e1 e2 :
  fill K e1 = fill K e2 -> e1 = e2.
Proof.
219 220
  revert e1 e2; induction K => el er /=;
     (move=><-; reflexivity) || (case => /IHK <-; reflexivity).
Ralf Jung's avatar
Ralf Jung committed
221 222
Qed.

Ralf Jung's avatar
Ralf Jung committed
223
Lemma fill_value K e v':
224
  e2v (fill K e) = Some v' -> is_Some (e2v e).
Ralf Jung's avatar
Ralf Jung committed
225
Proof.
226
  revert v'; induction K => v' /=; try discriminate;
Ralf Jung's avatar
Ralf Jung committed
227 228 229
    try destruct (e2v (fill K e)); rewrite ?v2v; eauto.
Qed.

230 231 232
Lemma fill_not_value e K :
  e2v e = None -> e2v (fill K e) = None.
Proof.
233
  intros Hnval.  induction K =>/=; by rewrite ?v2v /= ?IHK /=.
234
Qed.
235

236 237 238 239 240 241
Lemma fill_not_value2 e K v :
  e2v e = None -> e2v (fill K e) = Some v -> False.
Proof.
  intros Hnval Hval. erewrite fill_not_value in Hval by assumption. discriminate.
Qed.

242 243 244 245 246 247 248 249
Lemma comp_empty K K' :
  EmptyCtx = comp_ctx K K' 
  K = EmptyCtx  K' = EmptyCtx.
Proof.
  destruct K; try discriminate.
  destruct K'; try discriminate.
  done.
Qed.
250 251

(** The stepping relation *)
Ralf Jung's avatar
Ralf Jung committed
252
Inductive prim_step : expr -> state -> expr -> state -> option expr -> Prop :=
Ralf Jung's avatar
Ralf Jung committed
253
| BetaS e1 e2 v2 σ (Hv2 : e2v e2 = Some v2):
Ralf Jung's avatar
Ralf Jung committed
254
    prim_step (App (Rec e1) e2) σ (e1.[e2.:(Rec e1).:ids]) σ None
255 256 257 258
| Op1S T1 To (f : T1 -> To) t σ:
    prim_step (Op1 f (Lit t)) σ (Lit (f t)) σ None
| Op2S T1 T2 To (f : T1 -> T2 -> To) t1 t2 σ:
    prim_step (Op2 f (Lit t1) (Lit t2)) σ (Lit (f t1 t2)) σ None
259 260 261 262
| FstS e1 v1 e2 v2 σ (Hv1 : e2v e1 = Some v1) (Hv2 : e2v e2 = Some v2):
    prim_step (Fst (Pair e1 e2)) σ e1 σ None
| SndS e1 v1 e2 v2 σ (Hv1 : e2v e1 = Some v1) (Hv2 : e2v e2 = Some v2):
    prim_step (Snd (Pair e1 e2)) σ e2 σ None
Ralf Jung's avatar
Ralf Jung committed
263
| CaseLS e0 v0 e1 e2 σ (Hv0 : e2v e0 = Some v0):
264
    prim_step (Case (InjL e0) e1 e2) σ (e1.[e0/]) σ None
Ralf Jung's avatar
Ralf Jung committed
265 266 267
| CaseRS e0 v0 e1 e2 σ (Hv0 : e2v e0 = Some v0):
    prim_step (Case (InjR e0) e1 e2) σ (e2.[e0/]) σ None
| ForkS e σ:
268
    prim_step (Fork e) σ LitUnit σ (Some e)
269
| AllocS e v σ l (Hv : e2v e = Some v) (Hfresh : σ !! l = None):
Ralf Jung's avatar
Ralf Jung committed
270
    prim_step (Alloc e) σ (Loc l) (<[l:=v]>σ) None
Ralf Jung's avatar
Ralf Jung committed
271 272 273 274 275 276 277 278 279 280 281
| LoadS l v σ (Hlookup : σ !! l = Some v):
    prim_step (Load (Loc l)) σ (v2e v) σ None
| StoreS l e v σ (Hv : e2v e = Some v) (Halloc : is_Some (σ !! l)):
    prim_step (Store (Loc l) e) σ LitUnit (<[l:=v]>σ) None
| CasFailS l e1 v1 e2 v2 vl σ (Hv1 : e2v e1 = Some v1) (Hv2 : e2v e2 = Some v2)
  (Hlookup : σ !! l = Some vl) (Hne : vl <> v1):
    prim_step (Cas (Loc l) e1 e2) σ LitFalse σ None
| CasSucS l e1 v1 e2 v2 σ (Hv1 : e2v e1 = Some v1) (Hv2 : e2v e2 = Some v2)
  (Hlookup : σ !! l = Some v1):
    prim_step (Cas (Loc l) e1 e2) σ LitTrue (<[l:=v2]>σ) None
.
282

283 284
Definition reducible e σ : Prop :=
   e' σ' ef, prim_step e σ e' σ' ef.
285

286 287
Lemma reducible_not_value e σ :
  reducible e σ  e2v e = None.
288
Proof.
289
  intros (e' & σ' & ef & Hstep). destruct Hstep; simpl in *; reflexivity.
290 291
Qed.

292 293
Definition stuck (e : expr) σ : Prop :=
   K e', e = fill K e'  ~reducible e' σ.
294

295 296
Lemma values_stuck v σ :
  stuck (v2e v) σ.
297
Proof.
Ralf Jung's avatar
Ralf Jung committed
298 299 300
  intros ?? Heq.
  edestruct (fill_value K) as [v' Hv'].
  { by rewrite <-Heq, v2v. }
301 302
  clear -Hv' => Hred. apply reducible_not_value in Hred.
  destruct (e2v e'); discriminate.
Ralf Jung's avatar
Ralf Jung committed
303
Qed.
304

Ralf Jung's avatar
Ralf Jung committed
305
Section step_by_value.
306 307 308 309
(* When something does a step, and another decomposition of the same
     expression has a non-value e in the hole, then K is a left
     sub-context of K' - in other words, e also contains the reducible
     expression *)
310
Lemma step_by_value {K K' e e' σ} :
311
  fill K e = fill K' e' ->
312
  reducible e' σ ->
313 314 315
  e2v e = None ->
  exists K'', K' = comp_ctx K K''.
Proof.
316
  Ltac bad_fill := intros; exfalso; subst;
Ralf Jung's avatar
Ralf Jung committed
317 318 319 320 321 322 323 324
                   (eapply values_stuck; eassumption) ||
                     (eapply fill_not_value2; first eassumption;
                      try match goal with
                        [ H : fill _ _ = _ |- _ ] => erewrite ->H
                      end;
                      by erewrite ?v2v).
  Ltac bad_red   Hfill e' Hred := exfalso; destruct e';
      try discriminate Hfill; [];
325
      case: Hfill; intros; subst; destruct Hred as (e'' & σ'' & ef & Hstep);
Ralf Jung's avatar
Ralf Jung committed
326 327 328
      inversion Hstep; done || (clear Hstep; subst;
      eapply fill_not_value2; last (
        try match goal with [ H : _ = fill _ _ |- _ ] => erewrite <-H end; simpl;
Ralf Jung's avatar
Ralf Jung committed
329 330 331
        repeat match goal with [ H : e2v _ = _ |- _ ] =>
          erewrite H; clear H; simpl
        end
Ralf Jung's avatar
Ralf Jung committed
332
      ); eassumption || done).
333
  Ltac good IH := intros; subst; 
Ralf Jung's avatar
Ralf Jung committed
334 335 336 337
    let K'' := fresh "K''" in edestruct IH as [K'' Hcomp]; first eassumption;
    exists K''; by eauto using f_equal, f_equal2, f_equal3, v2e_inj.

  intros Hfill Hred Hnval. 
338
  revert K' Hfill; induction K=>K' /= Hfill;
339
    first (now eexists; reflexivity);
340 341 342 343 344
    (destruct K'; simpl;
      (* The first case is: K' is EmpyCtx. *)
      first (by bad_red Hfill e' Hred);
      (* Many of the other cases result in contradicting equalities. *)
      try discriminate Hfill;
Ralf Jung's avatar
Ralf Jung committed
345 346 347 348 349
      (* The remaining cases are "compatible" contexts - that result in the same
         head symbol of the expression.
         Test whether the context als has the same head, and use the appropriate
         tactic. Furthermore, the Op* contexts need special treatment due to the
         inhomogenuous equalities they induce. *)
350
      by match goal with
Ralf Jung's avatar
Ralf Jung committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
      | [ |- exists x, Op1Ctx _ _ = Op1Ctx _ _ ] =>
        move: Hfill; case_depeq2; good IHK
      | [ |- exists x, Op2LCtx _ _ _ = Op2LCtx _ _ _ ] =>
        move: Hfill; case_depeq3; good IHK
      | [ |- exists x, Op2RCtx _ _ _ = Op2RCtx _ _ _ ] =>
        move: Hfill; case_depeq3; good IHK
      | [ |- exists x, ?C _ = ?C _ ] =>
        case: Hfill; good IHK
      | [ |- exists x, ?C _ _ = ?C _ _ ] =>
        case: Hfill; good IHK
      | [ |- exists x, ?C _ _ _ = ?C _ _ _ ] =>
        case: Hfill; good IHK
      | [ |- exists x, Op2LCtx _ _ _ = Op2RCtx _ _ _ ] =>
        move: Hfill; case_depeq3; bad_fill
      | [ |- exists x, Op2RCtx _ _ _ = Op2LCtx _ _ _ ] =>
        move: Hfill; case_depeq3; bad_fill
367
      | _ => case: Hfill; bad_fill
368
      end).
Ralf Jung's avatar
Ralf Jung committed
369 370
Qed.
End step_by_value.
371

372 373 374
(** Atomic expressions *)
Definition atomic (e: expr) :=
  match e with
Ralf Jung's avatar
Ralf Jung committed
375
  | Alloc e => is_Some (e2v e)
376
  | Load e => is_Some (e2v e)
Ralf Jung's avatar
Ralf Jung committed
377 378
  | Store e1 e2 => is_Some (e2v e1) /\ is_Some (e2v e2)
  | Cas e0 e1 e2 => is_Some (e2v e0) /\ is_Some (e2v e1) /\ is_Some (e2v e2)
379 380 381
  | _ => False
  end.

382 383 384 385 386 387 388 389 390 391 392
Lemma atomic_not_value e :
  atomic e -> e2v e = None.
Proof.
  destruct e; simpl; contradiction || reflexivity.
Qed.

Lemma atomic_step e1 σ1 e2 σ2 ef :
  atomic e1 ->
  prim_step e1 σ1 e2 σ2 ef ->
  is_Some (e2v e2).
Proof.
Ralf Jung's avatar
Ralf Jung committed
393 394
  destruct e1; simpl; intros Hatomic Hstep; inversion Hstep;
    try contradiction Hatomic; rewrite ?v2v /=; eexists; reflexivity.
395 396 397 398 399 400 401 402
Qed.

(* Atomics must not contain evaluation positions. *)
Lemma atomic_fill e K :
  atomic (fill K e) ->
  e2v e = None ->
  K = EmptyCtx.
Proof.
Ralf Jung's avatar
Ralf Jung committed
403 404
  destruct K; simpl; first reflexivity; unfold is_Some; intros Hatomic Hnval;
    exfalso; try assumption;
Ralf Jung's avatar
Ralf Jung committed
405
    try (destruct_conjs; eapply fill_not_value2; eassumption).
406 407
Qed.

408
(** Tests, making sure that stuff works. *)
409
Module Tests.
Ralf Jung's avatar
Ralf Jung committed
410
  Definition add := Op2 plus (Lit 21) (Lit 21).
411

Ralf Jung's avatar
Ralf Jung committed
412
  Goal forall σ, prim_step add σ (Lit 42) σ None.
413 414 415
  Proof.
    apply Op2S.
  Qed.
Ralf Jung's avatar
Ralf Jung committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429

  Definition rec := Rec (App (Var 1) (Var 0)). (* fix f x => f x *)
  Definition rec_app := App rec (Lit 0).
  Goal forall σ, prim_step rec_app σ rec_app σ None.
  Proof.
    move=>?. eapply BetaS. (* Honestly, I have no idea why this works. *)
    reflexivity.
  Qed.

  Definition lam := Lam (Op2 plus (Var 0) (Lit 21)).
  Goal forall σ, prim_step (App lam (Lit 21)) σ add σ None.
  Proof.
    move=>?. eapply BetaS. reflexivity.
  Qed.
430
End Tests.
431

Ralf Jung's avatar
Ralf Jung committed
432 433
(** Instantiate the Iris language interface. This closes reduction under
    evaluation contexts.
434 435 436
    We could potentially make this a generic construction. *)
Section Language.

437
  Definition ectx_step e1 σ1 e2 σ2 (ef: option expr) :=
Ralf Jung's avatar
Ralf Jung committed
438 439
    exists K e1' e2', e1 = fill K e1' /\ e2 = fill K e2' /\
                      prim_step e1' σ1 e2' σ2 ef.
440

441
  Global Program Instance heap_lang : Language expr value state := {|
442 443 444 445 446 447 448 449 450 451
    of_val := v2e;
    to_val := e2v;
    language.atomic := atomic;
    language.prim_step := ectx_step;
    to_of_val := v2v;
    of_to_val := e2e;
    language.atomic_not_value := atomic_not_value
  |}.
  Next Obligation.
    intros e1 σ1 e2 σ2 ef (K & e1' & e2' & He1 & He2 & Hstep). subst e1 e2.
452
    eapply fill_not_value. eapply reducible_not_value. do 3 eexists; eassumption.
453 454 455 456 457 458
  Qed.
  Next Obligation.
    intros ? ? ? ? ? Hatomic (K & e1' & e2' & Heq1 & Heq2 & Hstep).
    subst. move: (Hatomic). rewrite (atomic_fill e1' K).
    - rewrite !fill_empty. eauto using atomic_step.
    - assumption.
459
    - clear Hatomic. eapply reducible_not_value. do 3 eexists; eassumption.
460
  Qed.
461 462 463 464

  (** We can have bind with arbitrary evaluation contexts **)
  Lemma fill_is_ctx K: is_ctx (fill K).
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
465
    split.
466
    - intros ? Hnval. by eapply fill_not_value.
467 468 469 470
    - intros ? ? ? ? ? (K' & e1' & e2' & Heq1 & Heq2 & Hstep).
      exists (comp_ctx K K'), e1', e2'. rewrite -!fill_comp Heq1 Heq2.
      split; last split; reflexivity || assumption.
    - intros ? ? ? ? ? Hnval (K'' & e1'' & e2'' & Heq1 & Heq2 & Hstep).
471 472
      destruct (step_by_value (σ:=σ1) Heq1) as [K' HeqK].
      + do 3 eexists. eassumption.
473
      + assumption.
Ralf Jung's avatar
Ralf Jung committed
474 475
      + subst e2 K''. rewrite -fill_comp in Heq1. apply fill_inj_r in Heq1.
        subst e1'. exists (fill K' e2''). split; first by rewrite -fill_comp.
476
        do 3 eexists. split; last split; eassumption || reflexivity.
477
  Qed.
478

479
  Lemma prim_ectx_step e1 σ1 e2 σ2 ef :
480
    reducible e1 σ1 
481 482 483 484
    ectx_step e1 σ1 e2 σ2 ef 
    prim_step e1 σ1 e2 σ2 ef.
  Proof.
    intros Hred (K' & e1' & e2' & Heq1 & Heq2 & Hstep).
485
    destruct (@step_by_value K' EmptyCtx e1' e1 σ1) as [K'' [HK' HK'']%comp_empty].
486 487
    - by rewrite fill_empty.
    - done.
488
    - eapply reducible_not_value. do 3 eexists; eassumption.
489 490
    - subst K' K'' e1 e2. by rewrite !fill_empty.
  Qed.
491

492
End Language.