tests.v 4.65 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
(** This file is essentially a bunch of testcases. *)
Ralf Jung's avatar
Ralf Jung committed
2
Require Import program_logic.upred program_logic.ownership.
Ralf Jung's avatar
Ralf Jung committed
3
Require Import heap_lang.lifting heap_lang.sugar.
4
Import heap_lang uPred notations.
Ralf Jung's avatar
Ralf Jung committed
5 6

Module LangTests.
7 8
  Definition add := (Lit 21 + Lit 21)%L.
  Goal  σ, prim_step add σ (Lit 42) σ None.
9
  Proof. intros; do_step done. Qed.
10
  Definition rec_app : expr := (rec:: #0 #1) (Lit 0).
Ralf Jung's avatar
Ralf Jung committed
11
  Goal  σ, prim_step rec_app σ rec_app σ None.
12
  Proof. Set Printing All. intros; do_step done. Qed.
13 14
  Definition lam : expr := λ: #0 + Lit 21.
  Goal  σ, prim_step (lam (Lit 21)) σ add σ None.
15
  Proof. intros; do_step done. Qed.
Ralf Jung's avatar
Ralf Jung committed
16 17
End LangTests.

18
Module LiftingTests.
19 20
  Context {Σ : iFunctor}.
  Implicit Types P : iProp heap_lang Σ.
21
  Implicit Types Q : val  iProp heap_lang Σ.
22

23
  (* FIXME: Fix levels so that we do not need the parenthesis here. *)
24 25
  Definition e  : expr := let: ref (Lit 1) in #0 <- !#0 + Lit 1; !#0.
  Goal  σ E, (ownP σ : iProp heap_lang Σ)  (wp E e (λ v, (v = LitV 2))).
26 27
  Proof.
    move=> σ E. rewrite /e.
28
    rewrite -wp_let. rewrite -wp_alloc_pst; last done.
29 30 31
    apply sep_intro_True_r; first done.
    rewrite -later_intro. apply forall_intro=>l.
    apply wand_intro_l. rewrite right_id. apply const_elim_l; move=>_.
32
    rewrite -later_intro. asimpl.
33 34
    (* TODO RJ: If you go here, you can see how the sugar does not print
       all so nicely. *)
35 36
    rewrite -(wp_bindi (SeqCtx (Load (Loc _)))).
    rewrite -(wp_bindi (StoreRCtx (LocV _))).
37
    rewrite -(wp_bindi (BinOpLCtx PlusOp _)).
38
    rewrite -wp_load_pst; first (apply sep_intro_True_r; first done); last first.
39
    { by rewrite lookup_insert. } (* RJ FIXME: figure out why apply and eapply fail. *)
40
    rewrite -later_intro. apply wand_intro_l. rewrite right_id.
41
    rewrite -wp_bin_op // -later_intro.
42
    rewrite -wp_store_pst; first (apply sep_intro_True_r; first done); last first.
43 44
    { by rewrite lookup_insert. }
    { done. }
45
    rewrite -later_intro. apply wand_intro_l. rewrite right_id.
46
    rewrite -wp_lam // -later_intro. asimpl.
47
    rewrite -wp_load_pst; first (apply sep_intro_True_r; first done); last first.
48
    { by rewrite lookup_insert. }
49
    rewrite -later_intro. apply wand_intro_l. rewrite right_id.
50 51
    by apply const_intro.
  Qed.
52

Ralf Jung's avatar
Ralf Jung committed
53 54 55
  (* TODO: once asimpl preserves notation, we don't need
     FindPred' anymore. *)
  (* FIXME: fix notation so that we do not need parenthesis or %L *)
56
  Definition FindPred' (n1 Sn1 n2 f : expr) : expr :=
57
    if Sn1 < n2 then f Sn1 else n1.
58
  Definition FindPred (n2 : expr) : expr :=
59
    rec:: (let: #1 + Lit 1 in FindPred' #2 #0 n2.[ren(+3)] #1)%L.
60
  Definition Pred : expr :=
61
    λ: (if #0  Lit 0 then Lit 0 else FindPred #0 (Lit 0))%L.
Ralf Jung's avatar
Ralf Jung committed
62

63
  Lemma FindPred_spec n1 n2 E Q :
64
    ( (n1 < n2)  Q (LitV $ pred n2))  wp E (FindPred (Lit n2) (Lit n1)) Q.
65 66 67 68 69 70 71
  Proof.
    revert n1. apply löb_all_1=>n1.
    rewrite -wp_rec //. asimpl.
    (* Get rid of the ▷ in the premise. *)
    etransitivity; first (etransitivity; last eapply equiv_spec, later_and).
    { apply and_mono; first done. by rewrite -later_intro. }
    apply later_mono.
72 73
    (* Go on *)
    (* FIXME "rewrite -(wp_let _ _ (FindPred' n1 #0 n2 (FindPred n2)))" started to
74
    fail after we changed # n to use ids instead of Var *)
75 76
    pose proof (wp_let (Σ:=Σ) E (Lit n1 + Lit 1)%L
                   (FindPred' (Lit n1) #0 (Lit n2) (FindPred (Lit n2))) Q).
77
    unfold Let, Lam in H; rewrite -H.
78 79
    rewrite -wp_bin_op //. asimpl.
    rewrite -(wp_bindi (IfCtx _ _)).
80
    rewrite -!later_intro /=.
81
    apply wp_lt; intros Hn12.
82
    * rewrite -wp_if_true.
83
      rewrite -!later_intro. asimpl.
84
      rewrite (forall_elim (S n1)).
85 86 87
      eapply impl_elim; first by eapply and_elim_l. apply and_intro.
      + apply const_intro; omega.
      + by rewrite !and_elim_r.
88
    * rewrite -wp_if_false.
89
      rewrite -!later_intro -wp_value' //.
90
      rewrite and_elim_r. apply const_elim_l=>Hle.
Ralf Jung's avatar
Ralf Jung committed
91
      by replace n1 with (pred n2) by omega.
92 93 94
  Qed.

  Lemma Pred_spec n E Q :
95
    Q (LitV $ pred n)  wp E (Pred $ Lit n) Q.
96 97
  Proof.
    rewrite -wp_lam //. asimpl.
98
    rewrite -(wp_bindi (IfCtx _ _)).
99
    apply later_mono, wp_le=> Hn.
100
    - rewrite -wp_if_true.
Ralf Jung's avatar
Ralf Jung committed
101
      rewrite -!later_intro -wp_value' //.
Ralf Jung's avatar
Ralf Jung committed
102
      by replace n with 0 by omega.
103
    - rewrite -wp_if_false.
104
      rewrite -!later_intro -FindPred_spec.
Ralf Jung's avatar
Ralf Jung committed
105
      auto using and_intro, const_intro with omega.
106
  Qed.
Ralf Jung's avatar
Ralf Jung committed
107

108
  Goal  E,
109
    True  wp (Σ:=Σ) E (let: Pred $ Lit 42 in Pred #0) (λ v, (v = LitV 40)).
Ralf Jung's avatar
Ralf Jung committed
110 111 112 113 114
  Proof.
    intros E. rewrite -wp_let. rewrite -Pred_spec -!later_intro.
    asimpl. (* TODO RJ: Can we somehow make it so that Pred gets folded again? *)
    rewrite -Pred_spec -later_intro. by apply const_intro.
  Qed.
115
End LiftingTests.