Translation.v 19.1 KB
Newer Older
1
From iris_time.heap_lang Require Export notation proofmode.
MEVEL Glen's avatar
MEVEL Glen committed
2 3
From stdpp Require Import fin_maps.

4
From iris_time Require Import Reduction.
MEVEL Glen's avatar
MEVEL Glen committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18

Implicit Type e : expr.
Implicit Type v : val.
Implicit Type σ : state.
Implicit Type t : list expr.
Implicit Type K : ectx heap_ectx_lang.
Implicit Type m n : nat.



(*
 * Translation with any arbitrary expression for [tick]
 *)

MEVEL Glen's avatar
MEVEL Glen committed
19
(* “tick” is a typeclass so that it can be made an implicit argument of the
20 21 22
 * translation and be inferred automatically from the context.
   This also make it possible to share notations. *)
Class Tick := tick : val.
23

MEVEL Glen's avatar
MEVEL Glen committed
24 25
Section Translation.

26 27
  Context {Htick : Tick}.

MEVEL Glen's avatar
MEVEL Glen committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
  (* Unfortunately, the operational semantics of [match] in our language is
   * ad-hoc somehow, as the rule is:
   *     Case (InjL v) e1 e2  →  e1 v
   * Instead of the more canonical rule:
   *     Case (InjL v) (x1. e1) (x2. e2)  →  e1 [x1 := v]
   * This means that the reduction of a match construct really takes two steps,
   * since we still have to reduce the application; hence, in our time credit
   * model, a match construct will cost two credits.
   * It also means that the “branch” [e1] is not constrained be a λ-abstraction,
   * and can reduce after the reduction of the [match] but before it is applied
   * to [v]!
   * This ad-hoc semantics calls for an ad-hoc definition of the translation in
   * that case because, to observe the simulation lemma, the translation of
   * [Case (InjL v) e1 e2] must reduce to the translation of [e1 v], which is
42
   * [(tick «e1») «v»]. To do so --which means preserving the weird evaluation
MEVEL Glen's avatar
MEVEL Glen committed
43 44
   * order--, we use the helper term below: *)
  Definition tick_case_branch : val :=
45
    (λ: "f" "x", tick ("f" #()) "x")%V.
MEVEL Glen's avatar
MEVEL Glen committed
46 47 48 49 50

  Fixpoint translation (e : expr) : expr :=
    match e with
    (* Base lambda calculus *)
    | Var _           => e
51
    | Rec f x e       => tick $ Rec f x (translation e)
52
    | App e1 e2       => (tick $ translation e1) (translation e2)
53
    | Val v           => Val $ translationV v
MEVEL Glen's avatar
MEVEL Glen committed
54 55
    (* Base types and their operations *)
    | UnOp op e       => UnOp op (tick $ translation e)
56
    | BinOp op e1 e2  => BinOp op (tick $ translation e1) (translation e2)
MEVEL Glen's avatar
MEVEL Glen committed
57 58
    | If e0 e1 e2     => If (tick $ translation e0) (translation e1) (translation e2)
    (* Products *)
59
    | Pair e1 e2      => Pair (tick $ translation e1) (translation e2)
MEVEL Glen's avatar
MEVEL Glen committed
60 61 62
    | Fst e           => Fst (tick $ translation e)
    | Snd e           => Snd (tick $ translation e)
    (* Sums *)
63 64
    | InjL e          => InjL (tick $ translation e)
    | InjR e          => InjR (tick $ translation e)
MEVEL Glen's avatar
MEVEL Glen committed
65 66
    | Case e0 e1 e2   =>
        Case (tick $ translation e0)
MEVEL Glen's avatar
MEVEL Glen committed
67 68
          (tick_case_branch (λ: <>, translation e1))
          (tick_case_branch (λ: <>, translation e2))
MEVEL Glen's avatar
MEVEL Glen committed
69 70 71 72 73
    (* Concurrency *)
    | Fork e          => tick $ Fork (translation e)
    (* Heap *)
    | Alloc e         => Alloc (tick $ translation e)
    | Load e          => Load (tick $ translation e)
74 75 76
    | Store e1 e2     => Store (tick $ translation e1) (translation e2)
    | CAS e0 e1 e2    => CAS (tick $ translation e0) (translation e1) (translation e2)
    | FAA e1 e2       => FAA (tick $ translation e1) (translation e2)
77 78
    end %E
  with translationV (v : val) : val :=
MEVEL Glen's avatar
MEVEL Glen committed
79
    match v with
80
    | RecV f x e    => RecV f x (translation e)
MEVEL Glen's avatar
MEVEL Glen committed
81 82 83 84 85 86 87 88 89 90 91 92 93
    | LitV _        => v
    | PairV v1 v2   => PairV (translationV v1) (translationV v2)
    | InjLV v       => InjLV (translationV v)
    | InjRV v       => InjRV (translationV v)
    end.

  Definition translationS (σ : state) : state :=
    translationV <$> σ.

  Let tickCtx : ectx_item := AppRCtx tick.

  Definition translationKi (Ki : ectx_item) : ectx_item :=
    match Ki with
94 95
    | AppLCtx v2      => AppLCtx (translationV v2)
    | AppRCtx e1      => AppRCtx (tick $ translation e1)
MEVEL Glen's avatar
MEVEL Glen committed
96
    | UnOpCtx op      => UnOpCtx op
97 98
    | BinOpLCtx op v2 => BinOpLCtx op (translationV v2)
    | BinOpRCtx op e1 => BinOpRCtx op (tick $ translation e1)
MEVEL Glen's avatar
MEVEL Glen committed
99
    | IfCtx e1 e2     => IfCtx (translation e1) (translation e2)
100
    | PairLCtx v2     => PairLCtx (translationV v2)
101
    | PairRCtx e1     => PairRCtx (tick $ translation e1)
MEVEL Glen's avatar
MEVEL Glen committed
102 103 104 105 106 107
    | FstCtx          => FstCtx
    | SndCtx          => SndCtx
    | InjLCtx         => InjLCtx
    | InjRCtx         => InjRCtx
    | CaseCtx e1 e2   =>
        CaseCtx
MEVEL Glen's avatar
MEVEL Glen committed
108 109
          (tick_case_branch (λ: <>, translation e1))%E
          (tick_case_branch (λ: <>, translation e2))%E
MEVEL Glen's avatar
MEVEL Glen committed
110 111
    | AllocCtx        => AllocCtx
    | LoadCtx         => LoadCtx
112 113 114 115 116 117 118
    | StoreLCtx v2    => StoreLCtx (translationV v2)
    | StoreRCtx e1    => StoreRCtx (tick $ translation e1)
    | CasLCtx v0 v1   => CasLCtx (translationV v0) (translationV v1)
    | CasMCtx e1 v1   => CasMCtx (tick $ translation e1) (translationV v1)
    | CasRCtx e1 e2   => CasRCtx (tick $ translation e1) (translation e2)
    | FaaLCtx v2      => FaaLCtx (translationV v2)
    | FaaRCtx e1      => FaaRCtx (tick $ translation e1)
MEVEL Glen's avatar
MEVEL Glen committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    end.

  Definition translationKi_aux (Ki : ectx_item) : ectx _ :=
    if ectx_item_is_active Ki then
      [ tickCtx ; translationKi Ki ]
    else
      [ translationKi Ki ].

  Definition translationK (K : ectx heap_ectx_lang) : ectx _ :=
    concat (translationKi_aux <$> K).

(*
 * Lemmas about translation
 *)

134 135
  Lemma translation_subst x e v :
    translation (subst x v e) = subst x (translationV v) (translation e).
MEVEL Glen's avatar
MEVEL Glen committed
136
  Proof.
137
    induction e ;
MEVEL Glen's avatar
MEVEL Glen committed
138 139 140
    unfold subst ; simpl ; fold subst ;
    try case_match ; (* ← this handles the cases of Var and Rec *)
    repeat f_equal ;
141
    assumption.
MEVEL Glen's avatar
MEVEL Glen committed
142 143
  Qed.

144 145
  Lemma translation_subst' x e v :
    translation (subst' x v e) = subst' x (translationV v) (translation e).
MEVEL Glen's avatar
MEVEL Glen committed
146 147 148 149 150 151
  Proof.
    destruct x.
    - reflexivity.
    - apply translation_subst.
  Qed.

152 153 154 155 156 157 158 159
  Lemma translation_injective :
     e1 e2,
      translation e1 = translation e2 
      e1 = e2
  with translationV_injective :
     v1 v2,
      translationV v1 = translationV v2 
      v1 = v2.
MEVEL Glen's avatar
MEVEL Glen committed
160
  Proof.
161 162 163 164
    destruct e1, e2; try discriminate; intros [=]; subst; f_equal;
      by (apply translation_injective || apply translationV_injective).
    destruct v1, v2; try discriminate; intros [=]; subst; f_equal;
      by (apply translation_injective || apply translationV_injective).
MEVEL Glen's avatar
MEVEL Glen committed
165 166 167 168 169
  Qed.

  Lemma translation_fill_item Ki e :
    translation (fill_item Ki e) = fill (translationKi_aux Ki) (translation e).
  Proof.
170
    destruct Ki ; reflexivity.
MEVEL Glen's avatar
MEVEL Glen committed
171 172 173 174 175 176
  Qed.

  Lemma translation_fill_item_active (ki : ectx_item) v :
    ectx_item_is_active ki 
    translation (fill_item ki v) = fill_item (translationKi ki) (tick (translationV v)).
  Proof.
177
    rewrite translation_fill_item.
MEVEL Glen's avatar
MEVEL Glen committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    unfold translationKi_aux ; destruct (ectx_item_is_active ki) ; last contradiction.
    reflexivity.
  Qed.

  Lemma is_active_translationKi ki :
    ectx_item_is_active ki 
    ectx_item_is_active (translationKi ki).
  Proof.
    by destruct ki.
  Qed.

  Lemma translation_fill K e :
    translation (fill K e) = fill (translationK K) (translation e).
  Proof.
    revert e ; induction K ; intros e.
    - done.
    - rewrite /= fill_app - translation_fill_item //.
  Qed.

  Lemma lookup_translationS_None σ l :
    σ !! l = None 
    translationS σ !! l = None.
  Proof.
    intros H. by rewrite lookup_fmap H.
  Qed.

  Lemma lookup_translationS_Some σ l v :
    σ !! l = Some v 
    translationS σ !! l = Some (translationV v).
  Proof.
    intros H. by rewrite lookup_fmap H.
  Qed.

  Lemma lookup_translationS_is_Some σ l :
    is_Some (σ !! l) 
    is_Some (translationS σ !! l).
  Proof.
    destruct 1. eauto using lookup_translationS_Some.
  Qed.

  Lemma lookup_translationS_None_inv σ l :
    translationS σ !! l = None 
    σ !! l = None.
  Proof.
    unfold translationS ; rewrite lookup_fmap.
    destruct (σ !! l) eqn:E ; rewrite E ; first discriminate.
    done.
  Qed.

  Lemma lookup_translationS_Some_inv σ l v' :
    translationS σ !! l = Some v' 
     v,  σ !! l = Some v    v' = translationV v.
  Proof.
    rewrite lookup_fmap.
    destruct (σ !! l) eqn:E ; rewrite E ; last discriminate.
    intros ? % Some_inj ; eauto.
  Qed.

  Lemma lookup_translationS_is_Some_inv σ l :
    is_Some (translationS σ !! l) 
    is_Some (σ !! l).
  Proof.
    intros [_ (? & ? & _) % lookup_translationS_Some_inv]. eauto.
  Qed.

  Lemma translationS_insert l v σ :
    translationS (<[l := v]> σ) = <[l := translationV v]> (translationS σ).
  Proof.
    apply fmap_insert.
  Qed.

  Lemma un_op_eval_translation op v v' :
    un_op_eval op v = Some v' 
    un_op_eval op (translationV v) = Some (translationV v').
  Proof.
    intros H.
    destruct op ;
    destruct v ; try discriminate H ;
256 257 258 259 260
    simpl ; case_match ; simpl in *;
      try discriminate H;
      try match goal with
          |- context [to_mach_int ?x] => destruct (to_mach_int x); [|discriminate] end;
      try by injection H as <-.
MEVEL Glen's avatar
MEVEL Glen committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  Qed.

  Local Lemma _eval_EqOp_bool_decide v1 v2 :
    bin_op_eval EqOp v1 v2 = Some $ LitV $ LitBool $ bool_decide (v1 = v2).
  Proof.
    destruct v1, v2 ; try done ;
    simpl ; case_match ; try done ;
    simpl ; case_match ; try done ;
    repeat f_equal ; apply bool_decide_iff ; naive_solver.
  Qed.

  Local Lemma _bool_decide_eq_translationV v1 v2 :
    bool_decide (v1 = v2) = bool_decide (translationV v1 = translationV v2).
  Proof.
    apply bool_decide_iff ; split ; intros H.
    - by rewrite H.
    - by eapply translationV_injective.
  Qed.

  Lemma bin_op_eval_translation op v1 v2 v' :
    bin_op_eval op v1 v2 = Some v' 
    bin_op_eval op (translationV v1) (translationV v2) = Some (translationV v').
  Proof.
    intros H.
    destruct op ; try (
      destruct v1, v2 ; try discriminate H ;
287 288
      unfold bin_op_eval in * ;
      do 3 try (case_match ; try discriminate H) ;
289 290 291
      simpl in *;
      try match goal with
          |- context [to_mach_int ?x] => destruct (to_mach_int x); [|discriminate] end;
MEVEL Glen's avatar
MEVEL Glen committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      by injection H as <-
    ).
    (* Remaining case: op = EqOp *)
    rewrite _eval_EqOp_bool_decide in H ; injection H as <-.
    by rewrite _eval_EqOp_bool_decide - _bool_decide_eq_translationV.
  Qed.

  Lemma un_op_eval_translation_inv op v v' :
    un_op_eval op (translationV v) = Some v' 
    un_op_eval op v = Some v'.
  Proof.
    intros H.
    destruct op ;
    destruct v ; try discriminate H ;
    done.
  Qed.

  Lemma bin_op_eval_translation_inv op v1 v2 v' :
    bin_op_eval op (translationV v1) (translationV v2) = Some v' 
    bin_op_eval op v1 v2 = Some v'.
  Proof.
    intros H.
    destruct op ; try (
      destruct v1, v2 ; try discriminate H ;
      done
    ).
    (* Remaining case: op = EqOp *)
    rewrite -> _eval_EqOp_bool_decide in *. injection H as <-.
    by rewrite - _bool_decide_eq_translationV.
  Qed.

  Lemma translationV_lit lit :
    translationV #lit = #lit.
  Proof.
    done.
  Qed.

  Lemma translationV_lit_inv v lit :
    translationV v = #lit 
    v = #lit.
  Proof.
    destruct v ; try discriminate. done.
  Qed.

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  Lemma vals_cas_compare_safe_translationV v1 v2 :
    vals_cas_compare_safe v1 v2 
    vals_cas_compare_safe (translationV v1) (translationV v2).
  Proof.
    intros [].
    - left. by destruct v1 as [| | |[]|[]].
    - right. by destruct v2 as [| | |[]|[]].
  Qed.
  Lemma vals_cas_compare_safe_translationV_inv v1 v2 :
    vals_cas_compare_safe (translationV v1) (translationV v2) 
    vals_cas_compare_safe v1 v2.
  Proof.
    intros [].
    - left. by destruct v1 as [| | |[]|[]].
    - right. by destruct v2 as [| | |[]|[]].
  Qed.

MEVEL Glen's avatar
MEVEL Glen committed
353 354
End Translation.

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
(*
 * Notations
 *)

Notation "E« e »" := (translation e%E).
Notation "V« v »" := (translationV v%V).
Notation "Ki« ki »" := (translationKi ki).
Notation "K« K »" := (translationK K).
Notation "S« σ »" := (translationS σ%V).
Notation "T« t »" := (translation <$> t%E).

Notation "« e »" := (translation e%E).
Notation "« e »" := (translation e%E) : expr_scope.
Notation "« v »" := (translationV v%V) : val_scope.

(* for some reason, these notations make parsing fail,
 * even if they only regard printing… *)
(*
Notation "« e »" := (translation e%E) (only printing).
Notation "« v »" := (translationV v%V) (only printing).
Notation "« ki »" := (translationKi ki) (only printing).
Notation "« K »" := (translationK K) (only printing).
Notation "« σ »" := (translationS σ%V) (only printing).
Notation "« t »" := (translation <$> t%E) (only printing).
*)

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
(* FIXME : the way coercions should or should not be included in
   notations so that notations are pretty-printed back is completely
   non-predictible. *)

Notation "'tickrec:' f x y .. z := e" :=
  (tick (Rec f%bind x%bind (tick (Lam y%bind .. (tick (Lam z%bind e%E)) ..))))
  (at level 200, f, x, y, z at level 1, e at level 200,
   format "'[' 'tickrec:'  f  x  y  ..  z  :=  '/  ' e ']'") : expr_scope.

Notation "tickλ: x , e" := (tick (Lam x%bind e%E))
  (at level 200, x at level 1, e at level 200,
   format "'[' 'tickλ:'  x ,  '/  ' e ']'") : expr_scope.
Notation "tickλ: x y .. z , e" :=
  (tick (Lam x%bind (tick (Lam y%bind .. (tick (Lam z%bind e%E)) ..))))
  (at level 200, x, y, z at level 1, e at level 200,
   format "'[' 'tickλ:'  x  y  ..  z ,  '/  ' e ']'") : expr_scope.

398
Notation "'lettick:' x := e1 'in' e2" :=
399
  ((tick (App (Val tick) (Lam x%bind e2%E))) e1%E)
400 401 402 403
  (at level 200, x at level 1, e1, e2 at level 200,
   format "'[' 'lettick:'  x  :=  '[' e1 ']'  'in'  '/' e2 ']'") : expr_scope.

Notation "e1 ;tick; e2" :=
404
  ((tick (App (Val tick) (Lam BAnon e2%E))) e1%E)
405 406 407 408
  (at level 100, e2 at level 200,
   format "'[' '[hv' '[' e1 ']'  ;tick;  ']' '/' e2 ']'") : expr_scope.

Notation "'tickmatch:' e0 'with' 'InjL' x1 => e1 | 'InjR' x2 => e2 'end'" :=
409 410
  (Case (App (Val tick) e0) (App (Val tick_case_branch) (λ: <>, App (Val tick) (λ: x1, e1))%E)
                            (App (Val tick_case_branch) (λ: <>, App (Val tick) (λ: x2, e2))%E))
411 412 413
  (e0, x1, e1, x2, e2 at level 200,
   format "'[hv' 'tickmatch:'  e0  'with'  '/  ' '[' 'InjL'  x1  =>  '/  ' e1 ']'  '/' '[' |  'InjR'  x2  =>  '/  ' e2 ']'  '/' 'end' ']'") : expr_scope.

414 415 416 417 418 419 420 421 422 423 424 425
(*
  Typeclass instance for the proofmode
 *)

Instance AsRecV_translationV `{Tick} v f x e :
  AsRecV v f x e 
  AsRecV « v » f x « e ».
Proof. by intros ->. Qed.

(*
 * Simplification tactic
 *)
426

427 428 429
(* simpl and cbn do not work well with mutual fixpoints... *)
Ltac simpl_trans :=
  cbn [translation translationV]; fold translation translationV.
430

MEVEL Glen's avatar
MEVEL Glen committed
431 432 433 434 435 436 437 438
(*
 * (Partial) Inverse translation
 *)

Section InvTranslation.

  Fixpoint invtranslation (e : expr) : expr :=
    match e with
439 440 441
    (* Concurrency -- This pattern has to appear before the pattern of
      App, since it conflicts with it. *)
    | App tick (Fork e)          => Fork (invtranslation e)
MEVEL Glen's avatar
MEVEL Glen committed
442 443
    (* Base lambda calculus *)
    | Var _                      => e
444
    | App tick (Rec f x e)       => Rec f x (invtranslation e)
445
    | App (App tick e1) e2       => (invtranslation e1) (invtranslation e2)
446
    | Val v                      => Val (invtranslationV v)
MEVEL Glen's avatar
MEVEL Glen committed
447 448
    (* Base types and their operations *)
    | UnOp op (App tick e)       => UnOp op (invtranslation e)
449
    | BinOp op (App tick e1) e2  => BinOp op (invtranslation e1) (invtranslation e2)
MEVEL Glen's avatar
MEVEL Glen committed
450 451
    | If (App tick e0) e1 e2     => If (invtranslation e0) (invtranslation e1) (invtranslation e2)
    (* Products *)
452
    | Pair (App tick e1) e2      => Pair (invtranslation e1) (invtranslation e2)
MEVEL Glen's avatar
MEVEL Glen committed
453 454 455
    | Fst (App tick e)           => Fst (invtranslation e)
    | Snd (App tick e)           => Snd (invtranslation e)
    (* Sums *)
456 457
    | InjL (App tick e)          => InjL (invtranslation e)
    | InjR (App tick e)          => InjR (invtranslation e)
458
    | Case (App tick e0) (App tickaux1 (Rec BAnon BAnon e1)) (App tickaux2 (Rec BAnon BAnon e2)) =>
MEVEL Glen's avatar
MEVEL Glen committed
459 460 461 462
        Case (invtranslation e0) (invtranslation e1) (invtranslation e2)
    (* Heap *)
    | Alloc (App tick e)         => Alloc (invtranslation e)
    | Load (App tick e)          => Load (invtranslation e)
463 464 465
    | Store (App tick e1) e2     => Store (invtranslation e1) (invtranslation e2)
    | CAS (App tick e0) e1 e2    => CAS (invtranslation e0) (invtranslation e1) (invtranslation e2)
    | FAA (App tick e1) e2       => FAA (invtranslation e1) (invtranslation e2)
MEVEL Glen's avatar
MEVEL Glen committed
466
    | _ => #42
467 468
    end %E
  with invtranslationV v :=
MEVEL Glen's avatar
MEVEL Glen committed
469
    match v with
470
    | RecV f x e    => RecV f x (invtranslation e)
MEVEL Glen's avatar
MEVEL Glen committed
471 472 473 474 475 476
    | LitV _        => v
    | PairV v1 v2   => PairV (invtranslationV v1) (invtranslationV v2)
    | InjLV v       => InjLV (invtranslationV v)
    | InjRV v       => InjRV (invtranslationV v)
    end.

477
  Lemma invtranslation_translation {Htick : Tick} e :
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    invtranslation (translation e) = e
  with invtranslationV_translationV {Htick : Tick} v :
    invtranslationV (translationV v) = v.
  Proof.
    - specialize (invtranslation_translation Htick).
      specialize (invtranslationV_translationV Htick).
      destruct e;
        try by rewrite /= ?invtranslation_translation ?invtranslationV_translationV.
      (* Handle the App case by hand. *)
      { simpl. rewrite !invtranslation_translation. case_match=>//.
          by destruct e2. by destruct e2. }

    - specialize (invtranslation_translation Htick).
      specialize (invtranslationV_translationV Htick).
      destruct v;
        try by rewrite /= ?invtranslation_translation ?invtranslationV_translationV.
MEVEL Glen's avatar
MEVEL Glen committed
494 495 496 497 498 499 500 501 502 503 504 505 506
  Qed.
End InvTranslation.



(*
 * Characterizing expressions that are left invariant by translation
 *)

Section ClosureFree.

  Fixpoint closure_free (v : val) : bool :=
    match v with
507
    | RecV _ _ _ => false
MEVEL Glen's avatar
MEVEL Glen committed
508 509 510 511 512 513
    | LitV _ => true
    | PairV v1 v2 => closure_free v1 && closure_free v2
    | InjLV v1 => closure_free v1
    | InjRV v1 => closure_free v1
    end.

514
  Lemma closure_free_is_translationV_invariant {Htick : Tick} v :
MEVEL Glen's avatar
MEVEL Glen committed
515
    closure_free v 
516
    translationV v = v.
MEVEL Glen's avatar
MEVEL Glen committed
517 518 519
  Proof.
    intros ?.
    induction v as
520 521
      [ (* LitV *) lit
      | (* RecV *) f x e1
MEVEL Glen's avatar
MEVEL Glen committed
522 523 524
      | (* PairV *) v1 IH1 v2 IH2
      | (* InjLV *) v1 IH1
      | (* InjRV *) v1 IH1
525
      ] ; cbn in *.
MEVEL Glen's avatar
MEVEL Glen committed
526
    - done.
527 528
    - contradiction.
    - rewrite IH1 ?IH2; naive_solver.
MEVEL Glen's avatar
MEVEL Glen committed
529 530 531 532 533 534 535 536 537 538
    - rewrite IH1 ; naive_solver.
    - rewrite IH1 ; naive_solver.
  Qed.

  Lemma closure_free_is_invtranslationV_invariant v :
    closure_free v 
    invtranslationV v = v.
  Proof.
    intros ?.
    induction v as
539 540
      [ (* LitV *) lit
      | (* RecV *) f x e1
MEVEL Glen's avatar
MEVEL Glen committed
541 542 543
      | (* PairV *) v1 IH1 v2 IH2
      | (* InjLV *) v1 IH1
      | (* InjRV *) v1 IH1
544
      ] ; cbn.
MEVEL Glen's avatar
MEVEL Glen committed
545
    - done.
546
    - contradiction.
MEVEL Glen's avatar
MEVEL Glen committed
547 548 549 550 551
    - rewrite -> IH1, IH2 ; naive_solver.
    - rewrite IH1 ; naive_solver.
    - rewrite IH1 ; naive_solver.
  Qed.

552
  Lemma closure_free_translationV {Htick : Tick} v :
MEVEL Glen's avatar
MEVEL Glen committed
553
    closure_free v 
554
    closure_free (translationV v).
MEVEL Glen's avatar
MEVEL Glen committed
555
  Proof.
556
    intros Hv. by rewrite (closure_free_is_translationV_invariant v Hv).
MEVEL Glen's avatar
MEVEL Glen committed
557 558 559 560 561 562 563 564 565
  Qed.

  Lemma closure_free_invtranslationV v :
    closure_free v 
    closure_free (invtranslationV v).
  Proof.
    intros Hv. by rewrite (closure_free_is_invtranslationV_invariant v Hv).
  Qed.

566 567
  Lemma closure_free_translationV_iff  {Htick : Tick} v :
    closure_free v    closure_free (translationV v).
MEVEL Glen's avatar
MEVEL Glen committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  Proof.
    split.
    - apply closure_free_translationV.
    - intros H % closure_free_invtranslationV. by rewrite invtranslationV_translationV in H.
  Qed.

  Lemma closure_free_predicate (φ : val  Prop) :
    ( (v : val), φ v  closure_free v) 
     (v : val),
      φ v  φ (invtranslationV v).
  Proof.
    intros Hφ v H.
    by specialize (Hφ _ H) as -> % closure_free_is_invtranslationV_invariant.
  Qed.

End ClosureFree.