tests.v 3.27 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 wp_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
    move=> γh N. rewrite /e.
34
    wp_focus (ref '1)%L. eapply wp_alloc; eauto; [].
35
    rewrite -later_intro; apply forall_intro=>l; apply wand_intro_l.
36
37
38
    wp_rec.
    wp_focus (! LocV l)%L.
    eapply wp_load; eauto with I; []. apply sep_mono_r.
39
    rewrite -later_intro; apply wand_intro_l.
40
41
42
    wp_bin_op.
    wp_focus (_ <- _)%L.
    eapply wp_store; eauto with I; []. apply sep_mono_r.
43
    rewrite -later_intro. apply wand_intro_l.
44
    wp_rec.
45
46
    eapply wp_load; eauto with I; []. apply sep_mono; first done.
    rewrite -later_intro. apply wand_intro_l.
47
48
    by apply const_intro.
  Qed.
49

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

58
  Lemma FindPred_spec n1 n2 E Q :
59
    ( (n1 < n2)  Q '(n2 - 1))  wp E (FindPred 'n2 'n1) Q.
60
  Proof.
61
    revert n1; apply löb_all_1=>n1.
62
    rewrite (comm uPred_and ( _)%I) assoc; apply const_elim_r=>?.
63
    (* first need to do the rec to get a later *)
64
    wp_rec>.
65
    (* FIXME: ssr rewrite fails with "Error: _pattern_value_ is used in conclusion." *)
66
67
68
    rewrite ->(later_intro (Q _)); rewrite -!later_and; apply later_mono.
    wp_rec. wp_bin_op. wp_rec. wp_bin_op=> ?.
    - wp_if. rewrite (forall_elim (n1 + 1)) const_equiv; last omega.
69
      by rewrite left_id impl_elim_l.
70
    - assert (n1 = n2 - 1) as -> by omega.
71
      wp_if. auto with I.
72
73
  Qed.

74
  Lemma Pred_spec n E Q :  Q (LitV (n - 1))  wp E (Pred 'n)%L Q.
75
  Proof.
76
    wp_rec>; apply later_mono; wp_bin_op=> ?.
77
78
79
80
81
    - wp_if. wp_un_op. wp_bin_op.
      wp_focus (FindPred _ _); rewrite -FindPred_spec.
      apply and_intro; first auto with I omega.
      wp_un_op. by replace (n - 1) with (- (-n + 2 - 1)) by omega.
    - wp_if. rewrite -FindPred_spec. auto with I omega.
82
  Qed.
Ralf Jung's avatar
Ralf Jung committed
83

84
  Goal  E,
85
    True  wp (Σ:=globalF Σ) E (let: "x" := Pred '42 in Pred "x") (λ v, v = '40).
Ralf Jung's avatar
Ralf Jung committed
86
  Proof.
87
    intros E.
88
89
    wp_focus (Pred '42); rewrite -Pred_spec -later_intro.
    wp_rec. rewrite -Pred_spec -later_intro; auto with I.
Ralf Jung's avatar
Ralf Jung committed
90
  Qed.
91
End LiftingTests.