sort.v 4.32 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 38
     <!> (xs : list A) (l : loc), MSG #l {{ llist I l xs }};
     <?> (xs' : list A), MSG #() {{  Sorted R xs'    xs'  xs   llist I l xs' }};
Robbert Krebbers's avatar
Robbert Krebbers committed
39
     END)%proto.
Robbert Krebbers's avatar
Robbert Krebbers committed
40 41

  Lemma lmerge_spec {A} (I : A  val  iProp Σ) (R : A  A  Prop)
Robbert Krebbers's avatar
Robbert Krebbers committed
42
      `{!RelDecision R, !Total R} (cmp : val) l1 l2 xs1 xs2 :
Robbert Krebbers's avatar
Robbert Krebbers committed
43
    cmp_spec I R cmp -
Robbert Krebbers's avatar
Robbert Krebbers committed
44
    {{{ llist I l1 xs1  llist I l2 xs2 }}}
45
      lmerge cmp #l1 #l2
Robbert Krebbers's avatar
Robbert Krebbers committed
46
    {{{ RET #(); llist I l1 (list_merge R xs1 xs2) }}}.
Robbert Krebbers's avatar
Robbert Krebbers committed
47
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
48 49
    iIntros "#Hcmp" (Ψ) "!> [Hl1 Hl2] HΨ".
    iLöb as "IH" forall (l2 xs1 xs2 Ψ).
Robbert Krebbers's avatar
Robbert Krebbers committed
50
    wp_lam. wp_apply (lisnil_spec with "Hl1"); iIntros "Hl1".
Robbert Krebbers's avatar
Robbert Krebbers committed
51
    destruct xs1 as [|x1 xs1]; wp_pures.
Robbert Krebbers's avatar
Robbert Krebbers committed
52 53 54
    { 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".
Robbert Krebbers's avatar
Robbert Krebbers committed
55
    destruct xs2 as [|x2 xs2]; wp_pures.
56
    { iApply "HΨ". iFrame. }
Robbert Krebbers's avatar
Robbert Krebbers committed
57 58 59
    wp_apply (lhead_spec_aux with "Hl1"); iIntros (v1 l1') "[HIx1 Hl1]".
    wp_apply (lhead_spec_aux with "Hl2"); iIntros (v2 l2') "[HIx2 Hl2]".
    wp_apply ("Hcmp" with "[$HIx1 $HIx2]"); iIntros "[HIx1 HIx2] /=".
Robbert Krebbers's avatar
Robbert Krebbers committed
60 61
    case_bool_decide; wp_pures.
    - rewrite decide_True //.
Robbert Krebbers's avatar
Robbert Krebbers committed
62 63 64 65 66
      wp_apply (lpop_spec_aux with "Hl1"); iIntros "Hl1".
      wp_apply ("IH" $! _ _ (_ :: _) with "Hl1 [HIx2 Hl2]").
      { iExists _, _. iFrame. }
      iIntros "Hl1".
      wp_apply (lcons_spec with "[$Hl1 $HIx1]"). iIntros "Hl1". iApply "HΨ". iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
67
    - rewrite decide_False //.
Robbert Krebbers's avatar
Robbert Krebbers committed
68 69 70 71 72
      wp_apply (lpop_spec_aux with "Hl2"); iIntros "Hl2".
      wp_apply ("IH" $! _ (_ :: _) with "[HIx1 Hl1] Hl2").
      { iExists _, _. iFrame. }
      iIntros "Hl1".
      wp_apply (lcons_spec with "[$Hl1 $HIx2]"); iIntros "Hl1". iApply "HΨ". iFrame.
Robbert Krebbers's avatar
Robbert Krebbers committed
73
  Qed.
74

Robbert Krebbers's avatar
Robbert Krebbers committed
75
  Lemma sort_service_spec p c :
Robbert Krebbers's avatar
Robbert Krebbers committed
76
    {{{ c  iProto_dual sort_protocol <++> p @ N }}}
Robbert Krebbers's avatar
Robbert Krebbers committed
77
      sort_service c
Robbert Krebbers's avatar
Robbert Krebbers committed
78
    {{{ RET #(); c  p @ N }}}.
Robbert Krebbers's avatar
Robbert Krebbers committed
79
  Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
80
    iIntros (Ψ) "Hc HΨ". iLöb as "IH" forall (p c Ψ). wp_lam.
81
    wp_recv (A I R ?? cmp) as "#Hcmp".
Robbert Krebbers's avatar
Robbert Krebbers committed
82
    wp_recv (xs l) as "Hl".
83
    wp_apply (llength_spec with "Hl"); iIntros "Hl".
Robbert Krebbers's avatar
Robbert Krebbers committed
84 85 86
    wp_op; case_bool_decide as Hlen; wp_if.
    { assert (Sorted R xs).
      { destruct xs as [|x1 [|x2 xs]]; simpl in *; eauto with lia. }
Robbert Krebbers's avatar
Robbert Krebbers committed
87
      wp_send with "[$Hl]"; first by auto. by iApply "HΨ". }
88 89
    wp_apply (lsplit_spec with "Hl"); iIntros (l2 vs1 vs2);
      iDestruct 1 as (->) "[Hl1 Hl2]".
Robbert Krebbers's avatar
Robbert Krebbers committed
90 91 92 93 94 95
    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. }
96
    wp_send with "[$Hcmp]".
Robbert Krebbers's avatar
Robbert Krebbers committed
97
    wp_send with "[$Hl1]".
98
    wp_send with "[$Hcmp]".
Robbert Krebbers's avatar
Robbert Krebbers committed
99 100 101 102 103
    wp_send with "[$Hl2]".
    wp_recv (ys1) as (??) "Hl1".
    wp_recv (ys2) as (??) "Hl2".
    wp_apply (lmerge_spec with "Hcmp [$Hl1 $Hl2]"); iIntros "Hl".
    wp_send ((list_merge R ys1 ys2)) with "[$Hl]".
104 105 106 107
    - iSplit; iPureIntro.
      + by apply (Sorted_list_merge _).
      + rewrite (merge_Permutation R). by f_equiv.
    - by iApply "HΨ".
Robbert Krebbers's avatar
Robbert Krebbers committed
108
  Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
109
End sort.