tests.v 4.14 KB
Newer Older
Ralf Jung's avatar
Ralf Jung committed
1
(** This file is essentially a bunch of testcases. *)
2
From program_logic Require Import ownership.
3
From heap_lang Require Import substitution tactics heap notation.
4
Import uPred.
Ralf Jung's avatar
Ralf Jung committed
5

6
Section LangTests.
Robbert Krebbers's avatar
Robbert Krebbers committed
7
8
  Definition add := ('21 + '21)%L.
  Goal  σ, prim_step add σ ('42) σ None.
9
  Proof. intros; do_step done. Qed.
10
  Definition rec_app : expr := ((rec: "f" "x" := "f" "x") '0).
Ralf Jung's avatar
Ralf Jung committed
11
  Goal  σ, prim_step rec_app σ rec_app σ None.
12
13
  Proof.
    intros. rewrite /rec_app. (* FIXME: do_step does not work here *)
14
    by eapply (Ectx_step  _ _ _ _ _ []), (BetaS _ _ _ _ '0).
15
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
16
17
  Definition lam : expr := λ: "x", "x" + '21.
  Goal  σ, prim_step (lam '21)%L σ add σ None.
18
19
  Proof.
    intros. rewrite /lam. (* FIXME: do_step does not work here *)
20
    by eapply (Ectx_step  _ _ _ _ _ []), (BetaS "" "x" ("x" + '21) _ '21).
21
  Qed.
Ralf Jung's avatar
Ralf Jung committed
22
23
End LangTests.

24
25
26
27
Section LiftingTests.
  Context {Σ : iFunctorG} (HeapI : gid) `{!HeapInG Σ HeapI}.
  Implicit Types P : iPropG heap_lang Σ.
  Implicit Types Q : val  iPropG heap_lang Σ.
28

29
  Definition e  : expr :=
30
    let: "x" := ref '1 in "x" <- !"x" + '1;; !"x".
31
  Goal  γh N, heap_ctx HeapI γh N  wp N e (λ v, v = '2).
32
  Proof.
33
34
    move=> γh N. rewrite /e.
    rewrite -(wp_bindi (LetCtx _ _)). eapply wp_alloc; eauto; [].
35
    rewrite -later_intro; apply forall_intro=>l; apply wand_intro_l.
36
37
    rewrite -wp_let //= -later_intro.
    rewrite -(wp_bindi (SeqCtx (Load (Loc _)))) /=. 
38
39
    rewrite -(wp_bindi (StoreRCtx (LocV _))) /=.
    rewrite -(wp_bindi (BinOpLCtx PlusOp _)) /=.
40
41
    eapply wp_load; eauto with I; []. apply sep_mono; first done.
    rewrite -later_intro; apply wand_intro_l.
42
    rewrite -wp_bin_op // -later_intro.
43
44
    eapply wp_store; eauto with I; []. apply sep_mono; first done.
    rewrite -later_intro. apply wand_intro_l.
45
    rewrite -wp_seq -wp_value' -later_intro.
46
47
    eapply wp_load; eauto with I; []. apply sep_mono; first done.
    rewrite -later_intro. apply wand_intro_l.
48
49
    by apply const_intro.
  Qed.
50

51
  Definition FindPred : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
52
    rec: "pred" "x" "y" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
53
      let: "yp" := "y" + '1 in
54
      if: "yp" < "x" then "pred" "x" "yp" else "y".
55
  Definition Pred : val :=
56
    λ: "x",
57
      if: "x"  '0 then -FindPred (-"x" + '2) '0 else FindPred "x" '0.
Ralf Jung's avatar
Ralf Jung committed
58

59
  Lemma FindPred_spec n1 n2 E Q :
60
    ( (n1 < n2)  Q '(n2 - 1))  wp E (FindPred 'n2 'n1) Q.
61
  Proof.
62
    revert n1; apply löb_all_1=>n1.
63
    rewrite (comm uPred_and ( _)%I) assoc; apply const_elim_r=>?.
64
    (* first need to do the rec to get a later *)
65
    rewrite -(wp_bindi (AppLCtx _)) /=.
66
    rewrite -wp_rec // =>-/=; rewrite -wp_value //=.
67
68
69
    (* FIXME: ssr rewrite fails with "Error: _pattern_value_ is used in conclusion." *)
    rewrite ->(later_intro (Q _)).
    rewrite -!later_and; apply later_mono.
70
    (* Go on *)
71
    rewrite -wp_let //= -later_intro.
72
73
    rewrite -(wp_bindi (LetCtx _ _)) -wp_bin_op //= -wp_let' //= -!later_intro.
    rewrite -(wp_bindi (IfCtx _ _)) /=.
74
    apply wp_lt=> ?.
75
76
    - rewrite -wp_if_true -!later_intro.
      rewrite (forall_elim (n1 + 1)) const_equiv; last omega.
77
      by rewrite left_id impl_elim_l.
78
    - assert (n1 = n2 - 1) as -> by omega.
79
      rewrite -wp_if_false -!later_intro.
80
      by rewrite -wp_value // and_elim_r.
81
82
  Qed.

83
  Lemma Pred_spec n E Q :  Q (LitV (n - 1))  wp E (Pred 'n)%L Q.
84
  Proof.
85
    rewrite -wp_lam //=.
86
    rewrite -(wp_bindi (IfCtx _ _)) /=.
87
    apply later_mono, wp_le=> Hn.
88
    - rewrite -wp_if_true.
89
90
91
      rewrite -(wp_bindi (UnOpCtx _)) /=.
      rewrite -(wp_bind [AppLCtx _; AppRCtx _]) /=.
      rewrite -(wp_bindi (BinOpLCtx _ _)) /=.
92
93
94
95
      rewrite -wp_un_op //=.
      rewrite -wp_bin_op //= -!later_intro.
      rewrite -FindPred_spec. apply and_intro; first by (apply const_intro; omega).
      rewrite -wp_un_op //= -later_intro.
96
      by assert (n - 1 = - (- n + 2 - 1)) as -> by omega.
97
98
    - rewrite -wp_if_false -!later_intro.
      rewrite -FindPred_spec.
Ralf Jung's avatar
Ralf Jung committed
99
      auto using and_intro, const_intro with omega.
100
  Qed.
Ralf Jung's avatar
Ralf Jung committed
101

102
  Goal  E,
103
    True  wp (Σ:=globalF Σ) E (let: "x" := Pred '42 in Pred "x") (λ v, v = '40).
Ralf Jung's avatar
Ralf Jung committed
104
  Proof.
105
    intros E.
106
    rewrite -(wp_bindi (LetCtx _ _)) -Pred_spec //= -wp_let' //=.
107
    by rewrite -Pred_spec -!later_intro /=.
Ralf Jung's avatar
Ralf Jung committed
108
  Qed.
109
End LiftingTests.