tests.v 2.92 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
Section LiftingTests.
25
  Context `{heapG Σ}.
26
27
  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  N, heap_ctx N  wp N e (λ v, v = '2).
32
  Proof.
33
    move=> N. rewrite /e.
34
35
36
37
    wp> eapply wp_alloc; eauto. apply forall_intro=>l; apply wand_intro_l.
    wp_rec. wp> eapply wp_load; eauto with I. apply sep_mono_r, wand_intro_l.
    wp_bin_op. wp> eapply wp_store; eauto with I. apply sep_mono_r, wand_intro_l.
    wp_rec. wp> eapply wp_load; eauto with I. apply sep_mono_r, wand_intro_l.
38
39
    by apply const_intro.
  Qed.
40

41
  Definition FindPred : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
42
    rec: "pred" "x" "y" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
43
      let: "yp" := "y" + '1 in
44
      if: "yp" < "x" then "pred" "x" "yp" else "y".
45
  Definition Pred : val :=
46
    λ: "x",
47
      if: "x"  '0 then -FindPred (-"x" + '2) '0 else FindPred "x" '0.
Ralf Jung's avatar
Ralf Jung committed
48

49
  Lemma FindPred_spec n1 n2 E Q :
50
    ( (n1 < n2)  Q '(n2 - 1))  wp E (FindPred 'n2 'n1) Q.
51
  Proof.
52
    revert n1; apply löb_all_1=>n1.
53
    rewrite (comm uPred_and ( _)%I) assoc; apply const_elim_r=>?.
54
    (* first need to do the rec to get a later *)
55
    wp_rec>.
56
    (* FIXME: ssr rewrite fails with "Error: _pattern_value_ is used in conclusion." *)
57
    rewrite ->(later_intro (Q _)); rewrite -!later_and; apply later_mono.
Robbert Krebbers's avatar
Robbert Krebbers committed
58
59
    wp_rec. wp_bin_op. wp_rec. wp_bin_op=> ?; wp_if.
    - rewrite (forall_elim (n1 + 1)) const_equiv; last omega.
60
      by rewrite left_id impl_elim_l.
61
    - wp_value. assert (n1 = n2 - 1) as -> by omega; auto with I.
62
63
  Qed.

64
  Lemma Pred_spec n E Q :  Q (LitV (n - 1))  wp E (Pred 'n)%L Q.
65
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
66
67
    wp_rec>; apply later_mono; wp_bin_op=> ?; wp_if.
    - wp_un_op. wp_bin_op.
68
      ewp apply FindPred_spec.
69
70
      apply and_intro; first auto with I omega.
      wp_un_op. by replace (n - 1) with (- (-n + 2 - 1)) by omega.
71
    - ewp apply FindPred_spec. auto with I omega.
72
  Qed.
Ralf Jung's avatar
Ralf Jung committed
73

74
  Goal  E,
75
    True  wp (Σ:=globalF Σ) E (let: "x" := Pred '42 in Pred "x") (λ v, v = '40).
Ralf Jung's avatar
Ralf Jung committed
76
  Proof.
77
    intros. ewp> apply Pred_spec. wp_rec. ewp> apply Pred_spec. auto with I.
Ralf Jung's avatar
Ralf Jung committed
78
  Qed.
79
End LiftingTests.