sort.v 4.88 KB
Newer Older
1
From stdpp Require Import sorting.
Robbert Krebbers's avatar
Robbert Krebbers committed
2
From actris.channel Require Import proto_channel proofmode.
3
From iris.heap_lang Require Import proofmode notation.
4
From actris.utils Require Export llist compare.
Robbert Krebbers's avatar
Robbert Krebbers committed
5

Robbert Krebbers's avatar
Robbert Krebbers committed
6
Definition lmerge : val :=
7
  rec: "go" "cmp" "ys" "zs" :=
Robbert Krebbers's avatar
Robbert Krebbers committed
8 9
    if: lisnil "ys" then lapp "ys" "zs" else
    if: lisnil "zs" then #() else
10 11 12
    let: "y" := lhead "ys" in
    let: "z" := lhead "zs" in
    if: "cmp" "y" "z"
Robbert Krebbers's avatar
Robbert Krebbers committed
13 14
    then lpop "ys";; "go" "cmp" "ys" "zs";; lcons "y" "ys";; #()
    else lpop "zs";; "go" "cmp" "ys" "zs";; lcons "z" "ys";; #().
Robbert Krebbers's avatar
Robbert Krebbers committed
15

Robbert Krebbers's avatar
Robbert Krebbers committed
16
Definition sort_service : val :=
Robbert Krebbers's avatar
Robbert Krebbers committed
17 18 19
  rec: "go" "c" :=
    let: "cmp" := recv "c" in
    let: "xs" := recv "c" in
20 21
    if: llength "xs"  #1 then send "c" #() else
    let: "zs" := lsplit "xs" in
Robbert Krebbers's avatar
Robbert Krebbers committed
22 23
    let: "cy" := start_chan "go" in
    let: "cz" := start_chan "go" in
24
    send "cy" "cmp";; send "cy" "xs";;
Robbert Krebbers's avatar
Robbert Krebbers committed
25 26
    send "cz" "cmp";; send "cz" "zs";;
    recv "cy";; recv "cz";;
27
    lmerge "cmp" "xs" "zs";;
Robbert Krebbers's avatar
Robbert Krebbers committed
28 29
    send "c" #().

Robbert Krebbers's avatar
Robbert Krebbers committed
30
Section sort.
Robbert Krebbers's avatar
Robbert Krebbers committed
31 32 33
  Context `{!heapG Σ, !proto_chanG Σ} (N : namespace).

  Definition sort_protocol : iProto Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
34
    (<!> A (I : A  val  iProp Σ) (R : A  A  Prop)
35
         `{!RelDecision R, !Total R} (cmp : val),
Robbert Krebbers's avatar
Robbert Krebbers committed
36
       MSG cmp {{ cmp_spec I R cmp }};
Robbert Krebbers's avatar
Robbert Krebbers committed
37
     <!> (xs : list A) (l : loc) (vs : list val),
38
       MSG #l {{ llist l vs  [ list] x;v  xs;vs, I x v }};
Robbert Krebbers's avatar
Robbert Krebbers committed
39
     <?> (xs' : list A) (vs' : list val),
Robbert Krebbers's avatar
Robbert Krebbers committed
40
       MSG #() {{  Sorted R xs'    xs'  xs  
41
                  llist l vs'  [ list] x;v  xs';vs', I x v }};
Robbert Krebbers's avatar
Robbert Krebbers committed
42
     END)%proto.
Robbert Krebbers's avatar
Robbert Krebbers committed
43 44

  Lemma lmerge_spec {A} (I : A  val  iProp Σ) (R : A  A  Prop)
45
      `{!RelDecision R, !Total R} (cmp : val) l1 l2 xs1 xs2 vs1 vs2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
46
    cmp_spec I R cmp -
47 48 49 50 51 52
    {{{
      llist l1 vs1  llist l2 vs2 
      ([ list] x;v  xs1;vs1, I x v)  ([ list] x;v  xs2;vs2, I x v)
    }}}
      lmerge cmp #l1 #l2
    {{{ ws, RET #(); llist l1 ws  [ list] x;v  list_merge R xs1 xs2;ws, I x v }}}.
Robbert Krebbers's avatar
Robbert Krebbers committed
53 54 55 56
  Proof.
    iIntros "#Hcmp" (Ψ) "!> (Hl1 & Hl2 & HI1 & HI2) HΨ".
    iLöb as "IH" forall (l2 xs1 xs2 vs1 vs2 Ψ).
    wp_lam. wp_apply (lisnil_spec with "Hl1"); iIntros "Hl1".
57
    destruct xs1 as [|x1 xs1], vs1 as [|v1 vs1]; simpl; done || wp_pures.
Robbert Krebbers's avatar
Robbert Krebbers committed
58 59 60
    { wp_apply (lapp_spec with "[$Hl1 $Hl2]"); iIntros "[Hl1 _] /=".
      iApply "HΨ". iFrame. by rewrite list_merge_nil_l. }
    wp_apply (lisnil_spec with "Hl2"); iIntros "Hl2".
61 62
    destruct xs2 as [|x2 xs2], vs2 as [|v2 vs2]; simpl; done || wp_pures.
    { iApply "HΨ". iFrame. }
Robbert Krebbers's avatar
Robbert Krebbers committed
63 64
    wp_apply (lhead_spec with "Hl1"); iIntros "Hl1".
    wp_apply (lhead_spec with "Hl2"); iIntros "Hl2".
Robbert Krebbers's avatar
Robbert Krebbers committed
65 66 67 68
    iDestruct "HI1" as "[HI1 HI1']"; iDestruct "HI2" as "[HI2 HI2']".
    wp_apply ("Hcmp" with "[$HI1 $HI2]"); iIntros "[HI1 HI2]".
    case_bool_decide; wp_pures.
    - rewrite decide_True //.
Robbert Krebbers's avatar
Robbert Krebbers committed
69 70 71 72 73
      wp_apply (lpop_spec with "Hl1"); iIntros "Hl1".
      wp_apply ("IH" $! _ _ (x2 :: _) with "Hl1 Hl2 HI1' [$HI2 $HI2']").
      iIntros (ws) "[Hl1 HI]".
      wp_apply (lcons_spec with "Hl1"); iIntros "Hl1".
      wp_pures. iApply "HΨ". iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
74
    - rewrite decide_False //.
Robbert Krebbers's avatar
Robbert Krebbers committed
75 76 77 78 79 80
      wp_apply (lpop_spec with "Hl2"); iIntros "Hl2".
      wp_apply ("IH" $! _ (x1 :: _) with "Hl1 Hl2 [$HI1 $HI1'] HI2'").
      iIntros (ws) "[Hl1 HI]".
      wp_apply (lcons_spec with "Hl1"); iIntros "Hl1".
      wp_pures. iApply "HΨ". iFrame.
  Qed.
81

Robbert Krebbers's avatar
Robbert Krebbers committed
82
  Lemma sort_service_spec p c :
Robbert Krebbers's avatar
Robbert Krebbers committed
83
    {{{ c  iProto_dual sort_protocol <++> p @ N }}}
Robbert Krebbers's avatar
Robbert Krebbers committed
84
      sort_service c
Robbert Krebbers's avatar
Robbert Krebbers committed
85
    {{{ RET #(); c  p @ N }}}.
Robbert Krebbers's avatar
Robbert Krebbers committed
86
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
87
    iIntros (Ψ) "Hc HΨ". iLöb as "IH" forall (p c Ψ).
Robbert Krebbers's avatar
Robbert Krebbers committed
88
    wp_lam.
89 90
    wp_recv (A I R ?? cmp) as "#Hcmp".
    wp_recv (xs l vs) as "[Hl HI]".
91
    wp_apply (llength_spec with "Hl"); iIntros "Hl".
Robbert Krebbers's avatar
Robbert Krebbers committed
92 93 94 95
    iDestruct (big_sepL2_length with "HI") as %<-.
    wp_op; case_bool_decide as Hlen; wp_if.
    { assert (Sorted R xs).
      { destruct xs as [|x1 [|x2 xs]]; simpl in *; eauto with lia. }
96
      wp_send with "[$Hl $HI]"; first by auto. by iApply "HΨ". }
97 98
    wp_apply (lsplit_spec with "Hl"); iIntros (l2 vs1 vs2);
      iDestruct 1 as (->) "[Hl1 Hl2]".
Robbert Krebbers's avatar
Robbert Krebbers committed
99
    iDestruct (big_sepL2_app_inv_r with "HI") as (xs1 xs2 ->) "[HI1 HI2]".
Robbert Krebbers's avatar
Robbert Krebbers committed
100 101 102 103 104 105
    wp_apply (start_chan_proto_spec N sort_protocol); iIntros (cy) "Hcy".
    { rewrite -{2}(right_id END%proto _ (iProto_dual _)).
      wp_apply ("IH" with "Hcy"); auto. }
    wp_apply (start_chan_proto_spec N sort_protocol); iIntros (cz) "Hcz".
    { rewrite -{2}(right_id END%proto _ (iProto_dual _)).
      wp_apply ("IH" with "Hcz"); auto. }
106 107 108 109
    wp_send with "[$Hcmp]".
    wp_send with "[$Hl1 $HI1]".
    wp_send with "[$Hcmp]".
    wp_send with "[$Hl2 $HI2]".
110 111
    wp_recv (ys1 ws1) as (??) "[Hl1 HI1]".
    wp_recv (ys2 ws2) as (??) "[Hl2 HI2]".
112
    wp_apply (lmerge_spec with "Hcmp [$Hl1 $Hl2 $HI1 $HI2]"); iIntros (ws) "[Hl HI]".
113 114 115 116 117
    wp_send ((list_merge R ys1 ys2) ws) with "[$Hl $HI]".
    - iSplit; iPureIntro.
      + by apply (Sorted_list_merge _).
      + rewrite (merge_Permutation R). by f_equiv.
    - by iApply "HΨ".
Robbert Krebbers's avatar
Robbert Krebbers committed
118
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
119
End sort.