Skip to content
Snippets Groups Projects
heap_lang.v 2.95 KiB
Newer Older
(** This file is essentially a bunch of testcases. *)
From iris.program_logic Require Import ownership hoare auth.
Robbert Krebbers's avatar
Robbert Krebbers committed
From iris.heap_lang Require Import proofmode notation.
Section LangTests.
  Definition add : expr := (#21 + #21)%E.
  Goal  σ, head_step add σ (#42) σ None.
  Proof. intros; do_head_step done. Qed.
  Definition rec_app : expr := ((rec: "f" "x" := "f" "x") #0)%E.
  Goal  σ, head_step rec_app σ rec_app σ None.
  Proof. intros. rewrite /rec_app. do_head_step done. Qed.
  Definition lam : expr := (λ: "x", "x" + #21)%E.
  Goal  σ, head_step (lam #21)%E σ add σ None.
  Proof. intros. rewrite /lam. do_head_step done. Qed.
End LangTests.

Section LiftingTests.
  Context `{heapG Σ}.
  Local Notation iProp := (iPropG heap_lang Σ).
  Implicit Types P Q : iPropG heap_lang Σ.
  Implicit Types Φ : val  iPropG heap_lang Σ.
  Definition heap_e  : expr :=
    let: "x" := ref #1 in "x" <- !"x" + #1 ;; !"x".
  Lemma heap_e_spec E :
     nclose heapN  E  heap_ctx  WP heap_e @ E {{ v, v = #2 }}.
    iIntros (HN) "#?". rewrite /heap_e.
    wp_alloc l. wp_let. wp_load. wp_op. wp_store. by wp_load.
    let: "x" := ref #1 in
    let: "y" := ref #1 in
    "x" <- !"x" + #1 ;; !"x".
  Lemma heap_e2_spec E :
     nclose heapN  E  heap_ctx  WP heap_e2 @ E {{ v, v = #2 }}.
    iIntros (HN) "#?". rewrite /heap_e2.
    wp_alloc l. wp_let. wp_alloc l'. wp_let.
    wp_load. wp_op. wp_store. wp_load. done.
  Definition FindPred : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
    rec: "pred" "x" "y" :=
      let: "yp" := "y" + #1 in
      if: "yp" < "x" then "pred" "x" "yp" else "y".
  Definition Pred : val :=
      if: "x"  #0 then -FindPred (-"x" + #2) #0 else FindPred "x" #0.
  Global Opaque FindPred Pred.
  Lemma FindPred_spec n1 n2 E Φ :
Robbert Krebbers's avatar
Robbert Krebbers committed
    n1 < n2 
    Φ #(n2 - 1)  WP FindPred #n2 #n1 @ E {{ Φ }}.
    iIntros (Hn) "HΦ". iLöb (n1 Hn) as "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
    wp_rec. wp_let. wp_op. wp_let. wp_op=> ?; wp_if.
    - iApply ("IH" with "[%] HΦ"). omega.
    - iApply pvs_intro. by assert (n1 = n2 - 1) as -> by omega.
  Lemma Pred_spec n E Φ :  Φ #(n - 1)  WP Pred #n @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
    iIntros "HΦ". wp_lam. wp_op=> ?; wp_if.
    - wp_op. wp_op.
Robbert Krebbers's avatar
Robbert Krebbers committed
      wp_apply FindPred_spec; first omega.
      wp_op. by replace (n - 1) with (- (-n + 2 - 1)) by omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
    - wp_apply FindPred_spec; eauto with omega.
  Lemma Pred_user E :
    (True : iProp)  WP let: "x" := Pred #42 in Pred "x" @ E {{ v, v = #40 }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
  Proof. iIntros "". wp_apply Pred_spec. wp_let. by wp_apply Pred_spec. Qed.

Section ClosedProofs.
  Definition Σ : gFunctors := #[ heapGF ].
  Notation iProp := (iPropG heap_lang Σ).
  Lemma heap_e_closed σ : {{ ownP σ : iProp }} heap_e {{ v, v = #2 }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
    iProof. iIntros "! Hσ".
    iPvs (heap_alloc with "Hσ") as (h) "[? _]"; first solve_ndisj.
    by iApply heap_e_spec; first solve_ndisj.
End ClosedProofs.