|
| 1 | +(* |
| 2 | + Copyright 2023 Microsoft Research |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*) |
| 16 | + |
| 17 | +module Pulse.Lib.Core.Refs |
| 18 | +open FStar.Ghost |
| 19 | +open PulseCore.FractionalPermission |
| 20 | +open PulseCore.Observability |
| 21 | +open FStar.PCM |
| 22 | +module T = FStar.Tactics.V2 |
| 23 | +open Pulse.Lib.Dv {} |
| 24 | +open FStar.ExtractAs |
| 25 | +open Pulse.Lib.Core |
| 26 | + |
| 27 | +// These are PCM references in Type u#3 and should not be used directly. |
| 28 | +// The modules Pulse.Lib.(Ghost)PCMReference provide universe-polymorphic wrappers. |
| 29 | + |
| 30 | +//////////////////////////////////////////////////////// |
| 31 | +//Core PCM references |
| 32 | +//////////////////////////////////////////////////////// |
| 33 | +val core_pcm_ref : Type0 |
| 34 | +val null_core_pcm_ref : core_pcm_ref |
| 35 | +val is_null_core_pcm_ref (p:core_pcm_ref) |
| 36 | + : b:bool { b <==> p == null_core_pcm_ref } |
| 37 | + |
| 38 | +let pcm_ref |
| 39 | + (#a:Type u#3) |
| 40 | + (p:FStar.PCM.pcm a) |
| 41 | +: Type0 |
| 42 | += core_pcm_ref |
| 43 | + |
| 44 | +val pcm_pts_to |
| 45 | + #a |
| 46 | + (#p:pcm a) |
| 47 | + ([@@@mkey] r:pcm_ref p) |
| 48 | + (v:a) |
| 49 | +: slprop |
| 50 | + |
| 51 | +val timeless_pcm_pts_to |
| 52 | + #a |
| 53 | + (#p:pcm a) |
| 54 | + (r:pcm_ref p) |
| 55 | + (v:a) |
| 56 | +: Lemma (timeless (pcm_pts_to r v)) |
| 57 | + [SMTPat (timeless (pcm_pts_to r v))] |
| 58 | + |
| 59 | +let pcm_ref_null |
| 60 | + (#a:Type) |
| 61 | + (p:FStar.PCM.pcm a) |
| 62 | +: pcm_ref p |
| 63 | += null_core_pcm_ref |
| 64 | + |
| 65 | +let is_pcm_ref_null |
| 66 | + (#a:Type) |
| 67 | + (#p:FStar.PCM.pcm a) |
| 68 | + (r:pcm_ref p) |
| 69 | +: b:bool { b <==> r == pcm_ref_null p } |
| 70 | += is_null_core_pcm_ref r |
| 71 | + |
| 72 | +val pts_to_not_null |
| 73 | + (#a:Type) |
| 74 | + (#p:FStar.PCM.pcm a) |
| 75 | + (r:pcm_ref p) |
| 76 | + (v:a) |
| 77 | +: stt_ghost (squash (not (is_pcm_ref_null r))) |
| 78 | + emp_inames |
| 79 | + (pcm_pts_to r v) |
| 80 | + (fun _ -> pcm_pts_to r v) |
| 81 | + |
| 82 | +val alloc |
| 83 | + #a |
| 84 | + (#pcm:pcm a) |
| 85 | + (x:a{pcm.refine x}) |
| 86 | +: stt (pcm_ref pcm) |
| 87 | + emp |
| 88 | + (fun r -> pcm_pts_to r x) |
| 89 | + |
| 90 | +val read |
| 91 | + (#a:Type) |
| 92 | + (#p:pcm a) |
| 93 | + (r:pcm_ref p) |
| 94 | + (x:erased a) |
| 95 | + (f:(v:a{compatible p x v} |
| 96 | + -> GTot (y:a{compatible p y v /\ |
| 97 | + FStar.PCM.frame_compatible p x v y}))) |
| 98 | +: stt (v:a{compatible p x v /\ p.refine v}) |
| 99 | + (pcm_pts_to r x) |
| 100 | + (fun v -> pcm_pts_to r (f v)) |
| 101 | + |
| 102 | +val write |
| 103 | + (#a:Type) |
| 104 | + (#p:pcm a) |
| 105 | + (r:pcm_ref p) |
| 106 | + (x y:Ghost.erased a) |
| 107 | + (f:FStar.PCM.frame_preserving_upd p x y) |
| 108 | +: stt unit |
| 109 | + (pcm_pts_to r x) |
| 110 | + (fun _ -> pcm_pts_to r y) |
| 111 | + |
| 112 | +val share |
| 113 | + (#a:Type) |
| 114 | + (#pcm:pcm a) |
| 115 | + (r:pcm_ref pcm) |
| 116 | + (v0:FStar.Ghost.erased a) |
| 117 | + (v1:FStar.Ghost.erased a{composable pcm v0 v1}) |
| 118 | +: stt_ghost unit |
| 119 | + emp_inames |
| 120 | + (pcm_pts_to r (v0 `op pcm` v1)) |
| 121 | + (fun _ -> pcm_pts_to r v0 ** pcm_pts_to r v1) |
| 122 | + |
| 123 | +[@@allow_ambiguous] |
| 124 | +val gather |
| 125 | + (#a:Type) |
| 126 | + (#pcm:pcm a) |
| 127 | + (r:pcm_ref pcm) |
| 128 | + (v0:FStar.Ghost.erased a) |
| 129 | + (v1:FStar.Ghost.erased a) |
| 130 | +: stt_ghost (squash (composable pcm v0 v1)) |
| 131 | + emp_inames |
| 132 | + (pcm_pts_to r v0 ** pcm_pts_to r v1) |
| 133 | + (fun _ -> pcm_pts_to r (op pcm v0 v1)) |
| 134 | + |
| 135 | +///////////////////////////////////////////////////////////////////////// |
| 136 | +[@@erasable] |
| 137 | +val core_ghost_pcm_ref : Type0 |
| 138 | + |
| 139 | +val null_core_ghost_pcm_ref : core_ghost_pcm_ref |
| 140 | + |
| 141 | +let ghost_pcm_ref |
| 142 | + (#a:Type u#3) |
| 143 | + (p:FStar.PCM.pcm a) |
| 144 | +: Type0 |
| 145 | += core_ghost_pcm_ref |
| 146 | + |
| 147 | +val ghost_pcm_pts_to |
| 148 | + #a |
| 149 | + (#p:pcm a) |
| 150 | + ([@@@mkey] r:ghost_pcm_ref p) |
| 151 | + (v:a) |
| 152 | +: slprop |
| 153 | + |
| 154 | +val timeless_ghost_pcm_pts_to |
| 155 | + #a |
| 156 | + (#p:pcm a) |
| 157 | + (r:ghost_pcm_ref p) |
| 158 | + (v:a) |
| 159 | +: Lemma (timeless (ghost_pcm_pts_to r v)) |
| 160 | + [SMTPat (timeless (ghost_pcm_pts_to r v))] |
| 161 | + |
| 162 | +val ghost_pts_to_not_null |
| 163 | + (#a:Type) |
| 164 | + (#p:pcm a) |
| 165 | + (r:ghost_pcm_ref p) |
| 166 | + (v:a) |
| 167 | +: stt_ghost (squash (r =!= null_core_ghost_pcm_ref)) |
| 168 | + emp_inames |
| 169 | + (ghost_pcm_pts_to r v) |
| 170 | + (fun _ -> ghost_pcm_pts_to r v) |
| 171 | + |
| 172 | +val ghost_alloc |
| 173 | + #a |
| 174 | + (#pcm:pcm a) |
| 175 | + (x:erased a{pcm.refine x}) |
| 176 | +: stt_ghost (ghost_pcm_ref pcm) |
| 177 | + emp_inames |
| 178 | + emp |
| 179 | + (fun r -> ghost_pcm_pts_to r x) |
| 180 | + |
| 181 | +val ghost_read |
| 182 | + (#a:Type) |
| 183 | + (#p:pcm a) |
| 184 | + (r:ghost_pcm_ref p) |
| 185 | + (x:erased a) |
| 186 | + (f:(v:a{compatible p x v} |
| 187 | + -> GTot (y:a{compatible p y v /\ |
| 188 | + FStar.PCM.frame_compatible p x v y}))) |
| 189 | +: stt_ghost (erased (v:a{compatible p x v /\ p.refine v})) |
| 190 | + emp_inames |
| 191 | + (ghost_pcm_pts_to r x) |
| 192 | + (fun v -> ghost_pcm_pts_to r (f v)) |
| 193 | + |
| 194 | +val ghost_write |
| 195 | + (#a:Type) |
| 196 | + (#p:pcm a) |
| 197 | + (r:ghost_pcm_ref p) |
| 198 | + (x y:Ghost.erased a) |
| 199 | + (f:FStar.PCM.frame_preserving_upd p x y) |
| 200 | +: stt_ghost unit |
| 201 | + emp_inames |
| 202 | + (ghost_pcm_pts_to r x) |
| 203 | + (fun _ -> ghost_pcm_pts_to r y) |
| 204 | + |
| 205 | +val ghost_share |
| 206 | + (#a:Type) |
| 207 | + (#pcm:pcm a) |
| 208 | + (r:ghost_pcm_ref pcm) |
| 209 | + (v0:FStar.Ghost.erased a) |
| 210 | + (v1:FStar.Ghost.erased a{composable pcm v0 v1}) |
| 211 | +: stt_ghost unit |
| 212 | + emp_inames |
| 213 | + (ghost_pcm_pts_to r (v0 `op pcm` v1)) |
| 214 | + (fun _ -> ghost_pcm_pts_to r v0 ** ghost_pcm_pts_to r v1) |
| 215 | + |
| 216 | +[@@allow_ambiguous] |
| 217 | +val ghost_gather |
| 218 | + (#a:Type) |
| 219 | + (#pcm:pcm a) |
| 220 | + (r:ghost_pcm_ref pcm) |
| 221 | + (v0:FStar.Ghost.erased a) |
| 222 | + (v1:FStar.Ghost.erased a) |
| 223 | +: stt_ghost (squash (composable pcm v0 v1)) |
| 224 | + emp_inames |
| 225 | + (ghost_pcm_pts_to r v0 ** ghost_pcm_pts_to r v1) |
| 226 | + (fun _ -> ghost_pcm_pts_to r (op pcm v0 v1)) |
0 commit comments