logrel_binary.v 23 KB
Newer Older
1 2 3
Require Import iris.program_logic.hoare.
Require Import iris.program_logic.lifting.
Require Import iris.algebra.upred_big_op.
4 5
Require Import iris_logrel.F_mu_ref_par.lang iris_logrel.F_mu_ref_par.typing
        iris_logrel.F_mu_ref_par.rules iris_logrel.F_mu_ref_par.rules_binary.
6 7 8 9
From iris.program_logic Require Export lifting.
From iris.algebra Require Import upred_big_op frac dec_agree.
From iris.program_logic Require Export invariants ghost_ownership.
From iris.program_logic Require Import ownership auth.
10
Require Import iris.proofmode.pviewshifts.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
Import uPred.

(** interp : is a unary logical relation. *)
Section logrel.
  Context {Σ : gFunctors}.
  Notation "# v" := (of_val v) (at level 20).

  Canonical Structure bivalC := prodC valC valC.

  (** Just to get nicer closed forms, we define extend_context_interp in
      three steps. *)
  Program Definition extend_context_interp_fun1
    (τi : bivalC -n> iPropG lang Σ)
    (f : varC -n> bivalC -n> iPropG lang Σ) :
    (varC -n> bivalC -n> iPropG lang Σ) :=
    {| cofe_mor_car :=
         λ x,
         match x return bivalC -n> iPropG lang Σ with
         | O => τi
         | S x' => f x'
         end
    |}.

  Program Definition extend_context_interp_fun2
    (τi : bivalC -n> iPropG lang Σ) :
    (varC -n> bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) :=
    {|
      cofe_mor_car := λ f, extend_context_interp_fun1 τi f
    |}.
  Next Obligation.
  Proof. intros ???? Hfg x; destruct x; cbn; trivial. Qed.

  Program Definition extend_context_interp :
    (bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) :=
    {|
      cofe_mor_car := λ τi, extend_context_interp_fun2 τi
    |}.
  Next Obligation.
  Proof. intros n g h H Δ x y. destruct x; cbn; auto. Qed.

  Program Definition extend_context_interp_apply :
    ((varC -n> bivalC -n> iPropG lang Σ)) -n>
    ((varC -n> bivalC -n> iPropG lang Σ) -n>
     bivalC -n> iPropG lang Σ) -n>
    (bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ) :=
    {|
      cofe_mor_car := λ Δ,
        {|
          cofe_mor_car := λ f,
            {|
              cofe_mor_car := λ g, f (extend_context_interp g Δ)
            |}
        |}
    |}.
  Solve Obligations with
  repeat intros ?; (cbn + idtac);
    try match goal with [H : _ {_} _|- _] => rewrite H end; trivial.
  Next Obligation.
  Proof.
    intros n Δ Δ' HΔ f g x.  cbn.
    match goal with
      |- _ _ ?F x {n} _ _ ?G x =>
      assert (F {n} G) as HFG; [|rewrite HFG; trivial]
    end.
    apply cofe_mor_car_ne; trivial. intros y. cbn.
    destruct y; trivial.
  Qed.

  Program Definition interp_unit : bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car := λ w, (w.1 = UnitV  w.2 = UnitV)%I
    |}.
  Next Obligation.
  Proof. intros n x y [H1 H2]; rewrite H1 H2; trivial. Qed.

89 90 91 92 93 94 95 96 97 98 99 100 101 102
  Program Definition interp_nat : bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car := λ w, ( n, w.1 = (v n)  w.2 = (v n))%I
    |}.
  Next Obligation.
  Proof. intros n x y [H1 H2]; rewrite H1 H2; trivial. Qed.

  Program Definition interp_bool : bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car := λ w, ( b, w.1 = (v b)  w.2 = (v b))%I
    |}.
  Next Obligation.
  Proof. intros n x y [H1 H2]; rewrite H1 H2; trivial. Qed.

103 104 105 106 107 108 109 110 111 112 113 114
  Program Definition interp_prod :
    (bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ) -n>
    bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car :=
        λ τ1i,
        {|
          cofe_mor_car :=
            λ τ2i,
            {|
              cofe_mor_car :=
                λ w, ( w1 w2, w = (PairV (w1.1) (w1.2), PairV (w2.1) (w2.2)) 
115
                               τ1i (w1.1, w2.1)  τ2i (w1.2, w2.2))%I
116 117 118 119 120 121 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
            |}
        |}
    |}.
  Next Obligation.
  Proof.
    intros τ1i τ2i n [x1 x2] [y1 y2] [H1 H2]; simpl in *.
    rewrite H1 H2; trivial.
  Qed.
  Next Obligation.
  Proof.
    intros τ1i n τi τi' Hτi y; simpl.
    apply exist_ne => z; apply exist_ne => z'.
    rewrite Hτi; trivial.
  Qed.
  Next Obligation.
    intros n τi τi' Hτi τ2i y; simpl.
    apply exist_ne => z; apply exist_ne => z'.
    rewrite Hτi; trivial.
  Qed.

  Program Definition interp_sum :
    (bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ) -n>
    bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car :=
        λ τ1i,
        {|
          cofe_mor_car :=
            λ τ2i,
            {|
              cofe_mor_car :=
147 148
                λ w, (( w1, w = (InjLV (w1.1), InjLV (w1.2))  τ1i w1) 
                      ( w2, w = (InjRV (w2.1), InjRV (w2.2))  τ2i w2))%I
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
            |}
        |}
    |}.
  Next Obligation.
  Proof.
    intros τ1i τ2i n [x1 x2] [y1 y2] [H1 H2]; simpl in *.
    rewrite H1 H2; trivial.
  Qed.
  Next Obligation.
  Proof.
    intros τ1i n τi τi' Hτi y; simpl.
    apply or_ne; apply exist_ne => z; trivial.
    rewrite Hτi; trivial.
  Qed.
  Next Obligation.
    intros n τi τi' Hτi τ2i y; simpl.
    apply or_ne; apply exist_ne => z; trivial.
    rewrite Hτi; trivial.
  Qed.

  Context `{Si : cfgSG Σ}.

  Program Definition interp_arrow :
    (bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ) -n>
    bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car :=
        λ τ1i,
        {|
          cofe_mor_car :=
            λ τ2i,
            {|
              cofe_mor_car :=
                λ w, (  j K v,
183
                           τ1i v  j  (fill K (App (# w.2) (# v.2))) 
184
                           WP App (# w.1) (# v.1)
185
                              {{z,  z', j  (fill K (# z'))  τ2i (z, z')}})%I
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
            |}
        |}
    |}.
  Next Obligation.
    intros τ1i τ2i n [x1 x2] [y1 y2] [H1 H2]; simpl in *.
    rewrite H1 H2; trivial.
  Qed.
  Next Obligation.
    intros τ1i n τi τi' Hτi z; simpl in *.
    apply always_ne;
      apply forall_ne =>j; apply forall_ne =>K; apply forall_ne =>v.
    apply impl_ne; trivial.
    apply wp_ne => t; apply exist_ne => h. rewrite Hτi; trivial.
  Qed.
  Next Obligation.
    intros n τi τi' Hτi τ2i z; simpl in *.
    apply always_ne;
      apply forall_ne =>j; apply forall_ne =>K; apply forall_ne =>v.
    apply impl_ne; trivial. rewrite Hτi; trivial.
  Qed.

  Program Definition interp_forall :
    ((bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ)) -n>
    bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car :=
        λ τi,
        {|
          cofe_mor_car :=
            λ w,
216
            ( (τ'i : {f : (bivalC -n> iPropG lang Σ) |
Robbert Krebbers's avatar
Robbert Krebbers committed
217
                        vw, PersistentP (f vw)}%type),
218 219
                 ( j K,
                      j  (fill K (TApp (# w.2))) 
220
                      WP TApp (# w.1) {{v,  v', j  (fill K (# v')) 
221
                                      (τi (`τ'i) (v, v'))}}))%I
222 223 224
        |}
    |}.
  Next Obligation.
225
    intros τi n [x1 x2] [y1 y2 ] [H1 H2]. simpl in *; auto.
226 227 228
    inversion H1; subst; inversion H2; subst; trivial.
  Qed.
  Next Obligation.
229
    intros n f g Hfg x; cbn.
230
    apply forall_ne=> P.
231
    apply always_ne.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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
    apply forall_ne => j; apply forall_ne => K.
    apply impl_ne; trivial. apply wp_ne => w; apply exist_ne => v'.
    rewrite Hfg; trivial.
  Qed.

  Program Definition interp_rec_pre :
    ((bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ)) -n>
    (bivalC -n> iPropG lang Σ) -n>
    (bivalC -n> iPropG lang Σ) :=
    {|
      cofe_mor_car :=
        λ τi,
        {| cofe_mor_car :=
             λ rec_appr,
             {|
               cofe_mor_car :=
                 λ w, ( ( v, w = (FoldV (v.1), FoldV (v.2)) 
                                (τi rec_appr v)))%I
             |}
        |}
    |}.
  Next Obligation.
    intros τi rec_appr n [x1 x2] [y1 y2] [H1 H2]; simpl in *.
    inversion H1; inversion H2; subst; trivial.
  Qed.
  Next Obligation.
    intros τi n f g Hfg x. cbn.
    apply always_ne, exist_ne =>w; rewrite Hfg; trivial.
  Qed.
  Next Obligation.
    intros n τi τi' Hτi f x. cbn.
    apply always_ne, exist_ne =>w; rewrite Hτi; trivial.
  Qed.

  Global Instance interp_rec_pre_contr
         (τi : (bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ))
    :
      Contractive (interp_rec_pre τi).
  Proof.
    intros n f g H w; cbn.
    apply always_ne, exist_ne; intros e; apply and_ne; trivial.
    apply later_contractive =>i Hi.
    rewrite H; trivial.
  Qed.

  Program Definition interp_rec :
    ((bivalC -n> iPropG lang Σ) -n> (bivalC -n> iPropG lang Σ)) -n>
    (bivalC -n> iPropG lang Σ)
    :=
      {|
        cofe_mor_car := λ τi, fixpoint (interp_rec_pre τi)
      |}.
  Next Obligation.
  Proof. intros n f g H; apply fixpoint_ne => z; rewrite H; trivial. Qed.

287
  Context `{i : heapIG Σ} (L : namespace).
288 289 290 291 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

  Program Definition interp_ref_pred (l : loc * loc) :
    (bivalC -n> iPropG lang Σ) -n> iPropG lang Σ :=
    {|
      cofe_mor_car := λ τi, ( v, (l.1) ↦ᵢ (v.1)  (l.2) ↦ₛ (v.2)  (τi v))%I
    |}.
  Next Obligation.
  Proof. intros ???? H; apply exist_ne =>w; rewrite H; trivial. Qed.

  Program Definition interp_ref :
    (bivalC -n> iPropG lang Σ) -n> bivalC -n> iPropG lang Σ :=
    {|
      cofe_mor_car :=
        λ τi, {|
          cofe_mor_car :=
            λ w, ( l, w = (LocV (l.1), LocV (l.2)) 
                       inv (L .@ l) (interp_ref_pred l τi))%I
        |}
    |}.
  Next Obligation.
    intros τi n [x1 x2] [y1 y2] [H1 H2]; simpl in *.
    inversion H1; inversion H2; subst; trivial.
  Qed.
  Next Obligation.
    intros n τi τi' Hτi z; simpl in *.
    apply exist_ne=>w; apply and_ne; trivial; cbn.
    apply (contractive_ne _); apply exist_ne=>w'; rewrite Hτi; trivial.
  Qed.

  Program Fixpoint interp (τ : type) {struct τ}
    : (varC -n> bivalC -n> iPropG lang Σ) -n> bivalC -n> iPropG lang Σ
    :=
      match τ return (varC -n> bivalC -n> iPropG lang Σ) -n>
                     bivalC -n> iPropG lang Σ
      with
      | TUnit => {| cofe_mor_car := λ Δ, interp_unit |}
324 325
      | TNat => {| cofe_mor_car := λ Δ, interp_nat |}
      | TBool => {| cofe_mor_car := λ Δ, interp_bool |}
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
      | TProd τ1 τ2 =>
        {| cofe_mor_car := λ Δ, interp_prod (interp τ1 Δ) (interp τ2 Δ)|}
      | TSum τ1 τ2 =>
        {| cofe_mor_car := λ Δ, interp_sum(interp τ1 Δ) (interp τ2 Δ)|}
      | TArrow τ1 τ2 =>
        {|cofe_mor_car := λ Δ, interp_arrow (interp τ1 Δ) (interp τ2 Δ)|}
      | TVar v => {| cofe_mor_car := λ Δ, (Δ v)  |}
      | TForall τ' =>
        {| cofe_mor_car :=
             λ Δ, interp_forall  (extend_context_interp_apply Δ (interp τ')) |}
      | TRec τ' =>
        {| cofe_mor_car :=
             λ Δ, interp_rec
                    (extend_context_interp_apply Δ (interp τ')) |}
      | Tref τ' => {| cofe_mor_car := λ Δ, interp_ref (interp τ' Δ) |}
      end%I.
  Solve Obligations
  with repeat intros ?;
              match goal with [H : _ {_} _|- _] => rewrite H end; trivial.

  Global Instance interp_Persistent
         τ (Δ : varC -n> bivalC -n> iPropG lang Σ)
Robbert Krebbers's avatar
Robbert Krebbers committed
348 349
         {HΔ :  x vw, PersistentP (Δ x vw)}
    :  vw, PersistentP (interp τ Δ vw).
350 351 352
  Proof.
    revert Δ HΔ.
    induction τ; cbn; intros Δ HΔ v; try apply _.
Robbert Krebbers's avatar
Robbert Krebbers committed
353 354
    rewrite /PersistentP /interp_rec fixpoint_unfold /interp_rec_pre; cbn.
    apply always_intro'; trivial.
355 356
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
357 358 359 360 361 362
  Global Instance extend_context_interp_Persistent
    (f : bivalC -n> iPropG lang Σ) (Δ : varC -n> bivalC -n> iPropG lang Σ)
           (Hf :  vw, PersistentP (f vw))
           {HΔ :  x vw, PersistentP (Δ x vw)}
    :  x vw, PersistentP (@extend_context_interp f Δ x vw).
  Proof. intros x v. destruct x; cbn; trivial. Qed.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

  Local Ltac properness :=
    repeat
      match goal with
      | |- ( _: _, _)%I  ( _: _, _)%I => apply exist_proper =>?
      | |- ( _: _, _)%I  ( _: _, _)%I => apply forall_proper =>?
      | |- (_  _)%I  (_  _)%I => apply and_proper
      | |- (_  _)%I  (_  _)%I => apply or_proper
      | |- (_  _)%I  (_  _)%I => apply impl_proper
      | |- (WP _ {{ _ }})%I  (WP _ {{ _ }})%I => apply wp_proper =>?
      | |- ( _)%I  ( _)%I => apply later_proper
      | |- ( _)%I  ( _)%I => apply always_proper
      | |- (inv _ _)%I  (inv _ _)%I => apply (contractive_proper _)
      | |- (_  _)%I  (_  _)%I => apply sep_proper
      end.

  Lemma interp_unused_contex_irrel
        (m n : nat)
        (Δ Δ' : varC -n> bivalC -n> iPropG lang Σ)
        (HΔ :  v, Δ (if lt_dec v m then v else (n + v)) 
                     Δ' (if lt_dec v m then v else (n + v)))
        (τ : type)
    :
      interp τ.[iter m up (ren (+n))] Δ  interp τ.[iter m up (ren (+n))] Δ'.
  Proof.
    revert m n Δ Δ' HΔ.
    induction τ; intros m n Δ Δ' HΔ v; cbn; auto.
    - properness; trivial; try apply IHτ1; try apply IHτ2; trivial.
    - properness; trivial; try apply IHτ1; try apply IHτ2; trivial.
    - properness; trivial; try apply IHτ1; try apply IHτ2; trivial.
    - match goal with
        |- _ _ ?f ?x  _ _ ?g ?x =>
        assert (f  g) as Hfg; [|rewrite Hfg; trivial]
      end.
      apply fixpoint_proper => ??; cbn.
      properness; trivial.
      change (up (iter m up (ren (+n)))) with (iter (S m) up (ren (+n))).
      apply IHτ.
      {
        intros x y. destruct x; cbn; trivial.
        destruct lt_dec.
        - specialize (HΔ x); destruct lt_dec; auto with omega.
        - destruct (n + S x) as [|k] eqn:Heq; trivial.
          specialize (HΔ x); destruct lt_dec; auto with omega.
          replace (n + x) with k in HΔ by omega; trivial.
      }
    -  rewrite iter_up. destruct lt_dec; cbn.
       + specialize (HΔ x); destruct lt_dec; auto with omega.
       + asimpl; unfold ids; cbn.
         specialize (HΔ x); destruct lt_dec; auto with omega.
         replace (m + n + (x - m)) with (n + x) by omega. trivial.
    - properness; trivial.
      change (up (iter m up (ren (+n)))) with (iter (S m) up (ren (+n))).
      apply IHτ.
      {
        intros x y. destruct x; cbn; trivial.
        destruct lt_dec.
        - specialize (HΔ x); destruct lt_dec; auto with omega.
        - destruct (n + S x) as [|k] eqn:Heq; trivial.
          specialize (HΔ x); destruct lt_dec; auto with omega.
          replace (n + x) with k in HΔ by omega; trivial.
      }
    - properness; trivial; try apply IHτ; trivial.
  Qed.

  Program Definition hop_context_interp (m n : nat) :
    (varC -n> bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) :=
    {| cofe_mor_car :=
         λ Δ,
         {| cofe_mor_car := λ v, if lt_dec v m then Δ v else Δ (v - n) |}
    |}.
  Next Obligation.
  Proof. intros ?????? Hxy; destruct Hxy; trivial. Qed.
  Next Obligation.
    intros ????? Hfg ?; cbn. destruct lt_dec; rewrite Hfg; trivial.
  Qed.

  Lemma extend_bofore_hop_context_interp (m n : nat)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (τi : bivalC -n> iPropG lang Σ)
        (v : var)
    :
      (extend_context_interp τi (hop_context_interp m n Δ)
                             (if lt_dec v (S m) then v else n + v))
         (hop_context_interp (S m) n (extend_context_interp τi Δ)
                              (if lt_dec v (S m) then v else n + v)).
  Proof.
    destruct v; cbn; trivial.
    repeat (destruct lt_dec; cbn); auto with omega.
    destruct (n + S v - n) eqn:Heq1;
      destruct (n + S v) eqn:Heq2; try destruct lt_dec; auto with omega.
    match goal with
      [ |- _ _ _ Δ ?a  _ _ _ Δ ?b] => assert (Heq : a = b) by omega;
                                       rewrite Heq; trivial
    end.
  Qed.

  Lemma interp_subst_weaken
        (m n : nat)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (τ : type)
    : interp τ Δ  interp τ.[iter m up (ren (+n))] (hop_context_interp m n Δ).
  Proof.
    revert m n Δ.
    induction τ; intros m n Δ v; cbn -[extend_context_interp]; auto.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - match goal with
        |- _ _ ?f ?x  _ _ ?g ?x =>
        assert (f  g) as Hfg; [|rewrite Hfg; trivial]
      end.
      apply fixpoint_proper => ??; cbn -[extend_context_interp].
      properness; trivial.
      rewrite IHτ.
      change (up (iter m up (ren (+n)))) with (iter (S m) up (ren (+n))).
      apply interp_unused_contex_irrel.
      intros w; rewrite extend_bofore_hop_context_interp; trivial.
    - rewrite iter_up.
      asimpl; unfold ids; cbn; destruct lt_dec; cbn; destruct lt_dec; auto with omega.
      replace (m + n + (x - m)) with (x + n) by omega.
      replace (x + n - n) with x; trivial.
      { (** An incompleteness in omega and lia! *)
        clear.
        replace (x + n) with (n + x) by omega.
        induction n; cbn; auto with omega.
        induction x; cbn; trivial.
      }
    - properness; trivial.
      change (up (iter m up (ren (+n)))) with (iter (S m) up (ren (+n))).
      rewrite IHτ.
      apply interp_unused_contex_irrel.
      intros w; rewrite extend_bofore_hop_context_interp; trivial.
    - properness; trivial; try apply IHτ; trivial.
  Qed.

  Lemma interp_ren_S (τ : type)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (τi : bivalC -n> iPropG lang Σ)
    : interp τ Δ  interp τ.[ren (+1)] (extend_context_interp τi Δ).
  Proof.
    rewrite (interp_subst_weaken 0 1).
    apply interp_unused_contex_irrel.
    { clear. intros [|v]; cbn; trivial. }
  Qed.

  Local Opaque eq_nat_dec.

  Program Definition context_interp_insert (m : nat) :
    (bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) -n>
    (varC -n> bivalC -n> iPropG lang Σ) :=
    {| cofe_mor_car :=
         λ τi,
         {| cofe_mor_car :=
              λ Δ,
              {| cofe_mor_car :=
                   λ v, if lt_dec v m then Δ v else
                          if eq_nat_dec v m then τi else Δ (v - 1)
              |}
         |}
    |}.
  Next Obligation.
  Proof. intros m τi Δ n x y Hxy; destruct Hxy; trivial. Qed.
  Next Obligation.
  Proof.
    intros m τi n Δ Δ' HΔ x; cbn;
      destruct lt_dec; try destruct eq_nat_dec; auto.
  Qed.
  Next Obligation.
  Proof.
    intros m n f g Hfg F Δ x; cbn;
      destruct lt_dec; try destruct eq_nat_dec; auto.
  Qed.

  Lemma extend_context_interp_insert (m : nat)
        (τi : bivalC -n> iPropG lang Σ)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (Ti : bivalC -n> iPropG lang Σ)
    :
      (extend_context_interp Ti (context_interp_insert m τi Δ))
         (context_interp_insert (S m) τi (extend_context_interp Ti Δ)).
  Proof.
    intros [|v]; cbn; trivial.
    repeat destruct lt_dec; trivial;
      repeat destruct eq_nat_dec; cbn; auto with omega.
    destruct v; cbn; auto with omega.
    replace (v - 0) with v by omega; trivial.
  Qed.

  Lemma context_interp_insert_O_extend
        (τi : bivalC -n> iPropG lang Σ)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
    :
      (context_interp_insert O τi Δ)
         (extend_context_interp τi Δ).
  Proof.
    intros [|v]; cbn; trivial.
    repeat destruct lt_dec; trivial;
      repeat destruct eq_nat_dec; cbn; auto with omega.
    destruct v; cbn; auto with omega.
  Qed.

  Lemma iter_up_subst_type (m : nat) (τ : type) (x : var) :
      (iter m up (τ .: ids) x) =
      if lt_dec x m then ids x else
        if eq_nat_dec x m then τ.[ren (+m)] else ids (x - 1).
  Proof.
    revert x τ.
    induction m; intros x τ; cbn.
    - destruct x; cbn.
      + destruct eq_nat_dec; auto with omega.
        asimpl; trivial.
      + destruct eq_nat_dec; auto with omega.
    - destruct x; asimpl; trivial.
      rewrite IHm.
      repeat destruct lt_dec; repeat destruct eq_nat_dec;
        asimpl; auto with omega.
  Qed.

  Lemma interp_subst_iter_up
        (m : nat)
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (τ : type)
        (τ' : type)
    : interp τ (context_interp_insert m (interp τ'.[ren (+m)] Δ) Δ)
              interp τ.[iter m up (τ' .: ids)] Δ.
  Proof.
    revert m Δ.
    induction τ; intros m Δ v; cbn -[extend_context_interp]; auto.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - properness; trivial; try apply IHτ1; try apply IHτ2.
    - match goal with
        |- _ _ ?f ?x  _ _ ?g ?x =>
        assert (f  g) as Hfg; [|rewrite Hfg; trivial]
      end.
      apply fixpoint_proper => ??; cbn -[extend_context_interp].
      properness; trivial.
      rewrite extend_context_interp_insert.
      change (up (iter m up (τ' .: ids))) with (iter (S m) up (τ' .: ids)).
      rewrite -IHτ.
      replace (τ'.[ren (+S m)]) with ((τ'.[ren (+m)]).[ren (+1)])
        by (asimpl; trivial).
      rewrite -interp_ren_S; trivial.
    - rewrite iter_up_subst_type.
      repeat destruct lt_dec; repeat destruct eq_nat_dec;
        unfold ids; asimpl; trivial.
    - properness; trivial.
      rewrite extend_context_interp_insert.
      change (up (iter m up (τ' .: ids))) with (iter (S m) up (τ' .: ids)).
      rewrite -IHτ.
      replace (τ'.[ren (+S m)]) with ((τ'.[ren (+m)]).[ren (+1)])
        by (asimpl; trivial).
      rewrite -interp_ren_S; trivial.
    - properness; trivial; try apply IHτ; trivial.
  Qed.

  Lemma interp_subst
        (Δ : varC -n> bivalC -n> iPropG lang Σ)
        (τ : type)
        (τ' : type)
    : interp τ (extend_context_interp (interp τ' Δ) Δ)  interp τ.[τ'/] Δ.
  Proof.
    rewrite -(interp_subst_iter_up O Δ τ τ').
    rewrite context_interp_insert_O_extend.
    asimpl; trivial.
  Qed.

  Lemma zip_with_context_interp_subst
        (Δ : varC -n> bivalC -n> iPropG lang Σ) (Γ : list type)
        (vs : list bivalC) (τi : bivalC -n> iPropG lang Σ) :
Robbert Krebbers's avatar
Robbert Krebbers committed
636 637
    ([] zip_with (λ τ, interp τ Δ) Γ vs)%I
       ([] zip_with (λ τ, interp τ (extend_context_interp τi Δ))
638 639 640 641 642 643 644 645 646 647
                    (map (λ t : type, t.[ren (+1)]) Γ) vs)%I.
  Proof.
    revert Δ vs τi.
    induction Γ as [|Γ]; intros Δ vs τi; cbn; trivial.
    destruct vs; cbn; trivial.
    apply and_proper.
    - apply interp_ren_S.
    - apply IHΓ.
  Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
648 649
  Lemma EqType_related_eq τ {H : EqType τ} v v' (Δ : varC -n> bivalC -n> iPropG lang Σ)
        {HΔ :  x vw, PersistentP (Δ x vw)} :
650 651
    interp τ Δ (v, v')   (v = v').
  Proof.
652
    revert v v'; induction H => v v'; iIntros "#H1".
653
    - simpl; iDestruct "H1" as "[% %]"; subst; trivial.
654 655
    - simpl; iDestruct "H1" as {n} "[% %]"; subst; trivial.
    - simpl; iDestruct "H1" as {b} "[% %]"; subst; trivial.
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    - iDestruct "H1" as {w1 w2} "[% [H1 H2]]".
      destruct w1; destruct w2; simpl in *.
      inversion H1; subst.
      rewrite IHEqType1 IHEqType2.
      iDestruct "H1" as "%". iDestruct "H2" as "%". subst; trivial.
    - iDestruct "H1" as "[H1|H1]".
      + iDestruct "H1" as {w} "[% H1]".
        destruct w; simpl in *.
        inversion H1; subst.
        rewrite IHEqType1.
        iDestruct "H1" as "%". subst; trivial.
      + iDestruct "H1" as {w} "[% H1]".
        destruct w; simpl in *.
        inversion H1; subst.
        rewrite IHEqType2.
        iDestruct "H1" as "%". subst; trivial.
  Qed.
673
End logrel.