file_name
stringlengths
5
52
name
stringlengths
4
95
original_source_type
stringlengths
0
23k
source_type
stringlengths
9
23k
source_definition
stringlengths
9
57.9k
source
dict
source_range
dict
file_context
stringlengths
0
721k
dependencies
dict
opens_and_abbrevs
listlengths
2
94
vconfig
dict
interleaved
bool
1 class
verbose_type
stringlengths
1
7.42k
effect
stringclasses
118 values
effect_flags
sequencelengths
0
2
mutual_with
sequencelengths
0
11
ideal_premises
sequencelengths
0
236
proof_features
sequencelengths
0
1
is_simple_lemma
bool
2 classes
is_div
bool
2 classes
is_proof
bool
2 classes
is_simply_typed
bool
2 classes
is_type
bool
2 classes
partial_definition
stringlengths
5
3.99k
completed_definiton
stringlengths
1
1.63M
isa_cross_project_example
bool
1 class
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.loc_regions_unused_in
val loc_regions_unused_in (h: HS.mem) (rs: Set.set HS.rid) : Lemma (requires (forall r . Set.mem r rs ==> (~ (HS.live_region h r)))) (ensures (loc_unused_in h `loc_includes` loc_regions false rs))
val loc_regions_unused_in (h: HS.mem) (rs: Set.set HS.rid) : Lemma (requires (forall r . Set.mem r rs ==> (~ (HS.live_region h r)))) (ensures (loc_unused_in h `loc_includes` loc_regions false rs))
let loc_regions_unused_in = MG.loc_regions_unused_in cls
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 1215, "start_col": 0, "start_line": 1215 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: FStar.Monotonic.HyperStack.mem -> rs: FStar.Set.set FStar.Monotonic.HyperHeap.rid -> FStar.Pervasives.Lemma (requires forall (r: FStar.Monotonic.HyperHeap.rid). FStar.Set.mem r rs ==> ~(FStar.Monotonic.HyperStack.live_region h r)) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h) (LowStar.Monotonic.Buffer.loc_regions false rs))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.ModifiesGen.loc_regions_unused_in", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls" ]
[]
true
false
true
false
false
let loc_regions_unused_in =
MG.loc_regions_unused_in cls
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mreference_live_loc_not_unused_in
val mreference_live_loc_not_unused_in (#t: Type) (#pre: Preorder.preorder t) (h: HS.mem) (r: HS.mreference t pre) : Lemma (requires (h `HS.contains` r)) (ensures (loc_not_unused_in h `loc_includes` loc_freed_mreference r /\ loc_not_unused_in h `loc_includes` loc_mreference r)) [SMTPatOr [ [SMTPat (HS.contains h r)]; [SMTPat (loc_not_unused_in h `loc_includes` loc_mreference r)]; [SMTPat (loc_not_unused_in h `loc_includes` loc_freed_mreference r)]; ]]
val mreference_live_loc_not_unused_in (#t: Type) (#pre: Preorder.preorder t) (h: HS.mem) (r: HS.mreference t pre) : Lemma (requires (h `HS.contains` r)) (ensures (loc_not_unused_in h `loc_includes` loc_freed_mreference r /\ loc_not_unused_in h `loc_includes` loc_mreference r)) [SMTPatOr [ [SMTPat (HS.contains h r)]; [SMTPat (loc_not_unused_in h `loc_includes` loc_mreference r)]; [SMTPat (loc_not_unused_in h `loc_includes` loc_freed_mreference r)]; ]]
let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 1240, "start_col": 0, "start_line": 1239 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: FStar.Monotonic.HyperStack.mem -> r: FStar.Monotonic.HyperStack.mreference t pre -> FStar.Pervasives.Lemma (requires FStar.Monotonic.HyperStack.contains h r) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_not_unused_in h) (LowStar.Monotonic.Buffer.loc_freed_mreference r) /\ LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_not_unused_in h) (LowStar.Monotonic.Buffer.loc_mreference r)) [ SMTPatOr [ [SMTPat (FStar.Monotonic.HyperStack.contains h r)]; [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_not_unused_in h) (LowStar.Monotonic.Buffer.loc_mreference r)) ]; [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_not_unused_in h) (LowStar.Monotonic.Buffer.loc_freed_mreference r)) ] ] ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.ModifiesGen.mreference_live_loc_not_unused_in", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls" ]
[]
true
false
true
false
false
let mreference_live_loc_not_unused_in =
MG.mreference_live_loc_not_unused_in cls
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mreference_unused_in_loc_unused_in
val mreference_unused_in_loc_unused_in (#t: Type) (#pre: Preorder.preorder t) (h: HS.mem) (r: HS.mreference t pre) : Lemma (requires (r `HS.unused_in` h)) (ensures (loc_unused_in h `loc_includes` loc_freed_mreference r /\ loc_unused_in h `loc_includes` loc_mreference r)) [SMTPatOr [ [SMTPat (HS.unused_in r h)]; [SMTPat (loc_unused_in h `loc_includes` loc_mreference r)]; [SMTPat (loc_unused_in h `loc_includes` loc_freed_mreference r)]; ]]
val mreference_unused_in_loc_unused_in (#t: Type) (#pre: Preorder.preorder t) (h: HS.mem) (r: HS.mreference t pre) : Lemma (requires (r `HS.unused_in` h)) (ensures (loc_unused_in h `loc_includes` loc_freed_mreference r /\ loc_unused_in h `loc_includes` loc_mreference r)) [SMTPatOr [ [SMTPat (HS.unused_in r h)]; [SMTPat (loc_unused_in h `loc_includes` loc_mreference r)]; [SMTPat (loc_unused_in h `loc_includes` loc_freed_mreference r)]; ]]
let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 1243, "start_col": 0, "start_line": 1242 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: FStar.Monotonic.HyperStack.mem -> r: FStar.Monotonic.HyperStack.mreference t pre -> FStar.Pervasives.Lemma (requires FStar.Monotonic.HyperStack.unused_in r h) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h) (LowStar.Monotonic.Buffer.loc_freed_mreference r) /\ LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h) (LowStar.Monotonic.Buffer.loc_mreference r)) [ SMTPatOr [ [SMTPat (FStar.Monotonic.HyperStack.unused_in r h)]; [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h ) (LowStar.Monotonic.Buffer.loc_mreference r)) ]; [ SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h ) (LowStar.Monotonic.Buffer.loc_freed_mreference r)) ] ] ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.ModifiesGen.mreference_unused_in_loc_unused_in", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls" ]
[]
true
false
true
false
false
let mreference_unused_in_loc_unused_in =
MG.mreference_unused_in_loc_unused_in cls
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.unused_in_loc_unused_in
val unused_in_loc_unused_in (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :Lemma (requires (unused_in b h)) (ensures (loc_unused_in h `loc_includes` loc_addr_of_buffer b)) [SMTPat (unused_in b h)]
val unused_in_loc_unused_in (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :Lemma (requires (unused_in b h)) (ensures (loc_unused_in h `loc_includes` loc_addr_of_buffer b)) [SMTPat (unused_in b h)]
let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; ()
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 1232, "start_col": 0, "start_line": 1228 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> h: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.unused_in b h) (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h) (LowStar.Monotonic.Buffer.loc_addr_of_buffer b)) [SMTPat (LowStar.Monotonic.Buffer.unused_in b h)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.Monotonic.HyperStack.mem", "Prims.unit", "FStar.ModifiesGen.loc_addresses_unused_in", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.frameOf", "FStar.Set.singleton", "Prims.nat", "LowStar.Monotonic.Buffer.as_addr", "FStar.Classical.move_requires", "FStar.Pervasives.Native.tuple2", "FStar.Monotonic.HyperHeap.rid", "Prims.l_imp", "Prims.b2t", "FStar.Monotonic.HyperStack.live_region", "FStar.Pervasives.Native.fst", "FStar.Monotonic.Heap.addr_unused_in", "FStar.Pervasives.Native.snd", "FStar.Map.sel", "FStar.Monotonic.Heap.heap", "FStar.Monotonic.HyperStack.get_hmap", "FStar.ModifiesGen.does_not_contain_addr", "FStar.ModifiesGen.addr_unused_in_does_not_contain_addr", "FStar.Pervasives.Native.Mktuple2", "LowStar.Monotonic.Buffer.unused_in_equiv" ]
[]
false
false
true
false
false
let unused_in_loc_unused_in #_ #_ #_ b h =
unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; ()
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.popped_modifies
val popped_modifies (h0 h1: HS.mem) : Lemma (requires (HS.popped h0 h1)) (ensures (modifies (loc_region_only false (HS.get_tip h0)) h0 h1)) [SMTPat (HS.popped h0 h1)]
val popped_modifies (h0 h1: HS.mem) : Lemma (requires (HS.popped h0 h1)) (ensures (modifies (loc_region_only false (HS.get_tip h0)) h0 h1)) [SMTPat (HS.popped h0 h1)]
let popped_modifies = MG.popped_modifies #_ cls
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 1252, "start_col": 0, "start_line": 1252 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires FStar.Monotonic.HyperStack.popped h0 h1) (ensures LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_region_only false (FStar.Monotonic.HyperStack.get_tip h0)) h0 h1) [SMTPat (FStar.Monotonic.HyperStack.popped h0 h1)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.ModifiesGen.popped_modifies", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls" ]
[]
true
false
true
false
false
let popped_modifies =
MG.popped_modifies #_ cls
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.is_null
val is_null (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.Stack bool (requires (fun h -> live h b)) (ensures (fun h y h' -> h == h' /\ y == g_is_null b))
val is_null (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.Stack bool (requires (fun h -> live h b)) (ensures (fun h y h' -> h == h' /\ y == g_is_null b))
let is_null #_ #_ #_ b = Null? b
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 1291, "start_col": 0, "start_line": 1291 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> FStar.HyperStack.ST.Stack Prims.bool
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.uu___is_Null", "Prims.bool" ]
[]
false
true
false
false
false
let is_null #_ #_ #_ b =
Null? b
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.modifies_remove_new_locs
val modifies_remove_new_locs (l_fresh l_aux l_goal:loc) (h1 h2 h3:HS.mem) : Lemma (requires (fresh_loc l_fresh h1 h2 /\ modifies l_aux h1 h2 /\ l_goal `loc_includes` l_aux /\ modifies (loc_union l_fresh l_goal) h2 h3)) (ensures (modifies l_goal h1 h3)) [SMTPat (fresh_loc l_fresh h1 h2); SMTPat (modifies l_aux h1 h2); SMTPat (modifies l_goal h1 h3)]
val modifies_remove_new_locs (l_fresh l_aux l_goal:loc) (h1 h2 h3:HS.mem) : Lemma (requires (fresh_loc l_fresh h1 h2 /\ modifies l_aux h1 h2 /\ l_goal `loc_includes` l_aux /\ modifies (loc_union l_fresh l_goal) h2 h3)) (ensures (modifies l_goal h1 h3)) [SMTPat (fresh_loc l_fresh h1 h2); SMTPat (modifies l_aux h1 h2); SMTPat (modifies l_goal h1 h3)]
let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 1255, "start_col": 0, "start_line": 1254 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l_fresh: LowStar.Monotonic.Buffer.loc -> l_aux: LowStar.Monotonic.Buffer.loc -> l_goal: LowStar.Monotonic.Buffer.loc -> h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> h3: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.fresh_loc l_fresh h1 h2 /\ LowStar.Monotonic.Buffer.modifies l_aux h1 h2 /\ LowStar.Monotonic.Buffer.loc_includes l_goal l_aux /\ LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_union l_fresh l_goal) h2 h3) (ensures LowStar.Monotonic.Buffer.modifies l_goal h1 h3) [ SMTPat (LowStar.Monotonic.Buffer.fresh_loc l_fresh h1 h2); SMTPat (LowStar.Monotonic.Buffer.modifies l_aux h1 h2); SMTPat (LowStar.Monotonic.Buffer.modifies l_goal h1 h3) ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.modifies_only_not_unused_in", "Prims.unit" ]
[]
true
false
true
false
false
let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 =
modifies_only_not_unused_in l_goal h1 h3
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.modifies_loc_unused_in
val modifies_loc_unused_in (l: loc) (h1 h2: HS.mem) (l' : loc) : Lemma (requires ( modifies l h1 h2 /\ address_liveness_insensitive_locs `loc_includes` l /\ loc_unused_in h2 `loc_includes` l' )) (ensures (loc_unused_in h1 `loc_includes` l')) [SMTPatOr [ [SMTPat (modifies l h1 h2); SMTPat (loc_unused_in h2 `loc_includes` l')]; [SMTPat (modifies l h1 h2); SMTPat (loc_unused_in h1 `loc_includes` l')]; ]]
val modifies_loc_unused_in (l: loc) (h1 h2: HS.mem) (l' : loc) : Lemma (requires ( modifies l h1 h2 /\ address_liveness_insensitive_locs `loc_includes` l /\ loc_unused_in h2 `loc_includes` l' )) (ensures (loc_unused_in h1 `loc_includes` l')) [SMTPatOr [ [SMTPat (modifies l h1 h2); SMTPat (loc_unused_in h2 `loc_includes` l')]; [SMTPat (modifies l h1 h2); SMTPat (loc_unused_in h1 `loc_includes` l')]; ]]
let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l'
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 1248, "start_col": 0, "start_line": 1245 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: LowStar.Monotonic.Buffer.loc -> h1: FStar.Monotonic.HyperStack.mem -> h2: FStar.Monotonic.HyperStack.mem -> l': LowStar.Monotonic.Buffer.loc -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.modifies l h1 h2 /\ LowStar.Monotonic.Buffer.loc_includes LowStar.Monotonic.Buffer.address_liveness_insensitive_locs l /\ LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h2) l') (ensures LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h1) l') [ SMTPatOr [ [ SMTPat (LowStar.Monotonic.Buffer.modifies l h1 h2); SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h2 ) l') ]; [ SMTPat (LowStar.Monotonic.Buffer.modifies l h1 h2); SMTPat (LowStar.Monotonic.Buffer.loc_includes (LowStar.Monotonic.Buffer.loc_unused_in h1 ) l') ] ] ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.loc_includes_trans", "LowStar.Monotonic.Buffer.loc_unused_in", "Prims.unit", "LowStar.Monotonic.Buffer.modifies_address_liveness_insensitive_unused_in", "LowStar.Monotonic.Buffer.modifies_loc_includes", "LowStar.Monotonic.Buffer.address_liveness_insensitive_locs" ]
[]
true
false
true
false
false
let modifies_loc_unused_in l h1 h2 l' =
modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l'
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.fresh_frame_modifies
val fresh_frame_modifies (h0 h1: HS.mem) : Lemma (requires (HS.fresh_frame h0 h1)) (ensures (modifies loc_none h0 h1)) [SMTPat (HS.fresh_frame h0 h1)]
val fresh_frame_modifies (h0 h1: HS.mem) : Lemma (requires (HS.fresh_frame h0 h1)) (ensures (modifies loc_none h0 h1)) [SMTPat (HS.fresh_frame h0 h1)]
let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 69, "end_line": 1250, "start_col": 0, "start_line": 1250 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h0: FStar.Monotonic.HyperStack.mem -> h1: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires FStar.Monotonic.HyperStack.fresh_frame h0 h1) (ensures LowStar.Monotonic.Buffer.modifies LowStar.Monotonic.Buffer.loc_none h0 h1) [SMTPat (FStar.Monotonic.HyperStack.fresh_frame h0 h1)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Monotonic.HyperStack.mem", "FStar.ModifiesGen.fresh_frame_modifies", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "Prims.unit" ]
[]
true
false
true
false
false
let fresh_frame_modifies h0 h1 =
MG.fresh_frame_modifies #_ cls h0 h1
false
SteelLoops.fst
SteelLoops.sum_to_n_for
val sum_to_n_for (r: ref UInt32.t) : SteelT unit (vptr r) (fun _ -> vptr r)
val sum_to_n_for (r: ref UInt32.t) : SteelT unit (vptr r) (fun _ -> vptr r)
let sum_to_n_for (r:ref UInt32.t) : SteelT unit (vptr r) (fun _ -> vptr r) = for_loop 0sz 10sz (fun _ -> vptr r) (fun _ -> let x = read r in write r (x `FStar.UInt32.add_mod` 1ul))
{ "file_name": "share/steel/tests/krml/SteelLoops.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 71, "end_line": 13, "start_col": 0, "start_line": 8 }
module SteelLoops open Steel.Effect.Atomic open Steel.Effect open Steel.Reference open Steel.Loops
{ "checked_file": "/", "dependencies": [ "Steel.Reference.fsti.checked", "Steel.Loops.fsti.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.SizeT.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Int32.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "SteelLoops.fst" }
[ { "abbrev": false, "full_module": "Steel.Loops", "short_module": null }, { "abbrev": false, "full_module": "Steel.Reference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Reference.ref FStar.UInt32.t -> Steel.Effect.SteelT Prims.unit
Steel.Effect.SteelT
[]
[]
[ "Steel.Reference.ref", "FStar.UInt32.t", "Steel.Loops.for_loop", "FStar.SizeT.__uint_to_t", "Steel.Loops.nat_at_most", "Steel.Reference.vptr", "Steel.Effect.Common.vprop", "Steel.Loops.u32_between", "Steel.Reference.write", "FStar.UInt32.add_mod", "FStar.UInt32.__uint_to_t", "Prims.unit", "Steel.Reference.read" ]
[]
false
true
false
false
false
let sum_to_n_for (r: ref UInt32.t) : SteelT unit (vptr r) (fun _ -> vptr r) =
for_loop 0sz 10sz (fun _ -> vptr r) (fun _ -> let x = read r in write r (x `FStar.UInt32.add_mod` 1ul))
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.includes_frameOf_as_addr
val includes_frameOf_as_addr (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (larger:mbuffer a1 rrel1 rel1) (smaller:mbuffer a2 rrel2 rel2) :Lemma (requires (larger `includes` smaller)) (ensures (g_is_null larger == g_is_null smaller /\ frameOf larger == frameOf smaller /\ as_addr larger == as_addr smaller)) [SMTPat (larger `includes` smaller)]
val includes_frameOf_as_addr (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (larger:mbuffer a1 rrel1 rel1) (smaller:mbuffer a2 rrel2 rel2) :Lemma (requires (larger `includes` smaller)) (ensures (g_is_null larger == g_is_null smaller /\ frameOf larger == frameOf smaller /\ as_addr larger == as_addr smaller)) [SMTPat (larger `includes` smaller)]
let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 166, "end_line": 1280, "start_col": 0, "start_line": 1277 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
larger: LowStar.Monotonic.Buffer.mbuffer a1 rrel1 rel1 -> smaller: LowStar.Monotonic.Buffer.mbuffer a2 rrel2 rel2 -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.includes larger smaller) (ensures LowStar.Monotonic.Buffer.g_is_null larger == LowStar.Monotonic.Buffer.g_is_null smaller /\ LowStar.Monotonic.Buffer.frameOf larger == LowStar.Monotonic.Buffer.frameOf smaller /\ LowStar.Monotonic.Buffer.as_addr larger == LowStar.Monotonic.Buffer.as_addr smaller) [SMTPat (LowStar.Monotonic.Buffer.includes larger smaller)]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.op_BarBar", "LowStar.Monotonic.Buffer.uu___is_Null", "Prims.bool", "FStar.ModifiesGen.loc_includes_aloc_elim", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.as_addr", "LowStar.Monotonic.Buffer.ubuffer_of_buffer", "Prims.unit" ]
[]
false
false
true
false
false
let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller =
if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.disjoint_neq
val disjoint_neq (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires (disjoint b1 b2 /\ U32.v (len b1) > 0)) (ensures (~(b1 === b2)))
val disjoint_neq (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires (disjoint b1 b2 /\ U32.v (len b1) > 0)) (ensures (~(b1 === b2)))
let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else ()
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 9, "end_line": 1260, "start_col": 0, "start_line": 1257 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mbuffer a1 rrel1 rel1 -> b2: LowStar.Monotonic.Buffer.mbuffer a2 rrel2 rel2 -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.disjoint b1 b2 /\ FStar.UInt32.v (LowStar.Monotonic.Buffer.len b1) > 0) (ensures ~(b1 === b2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "Prims.nat", "LowStar.Monotonic.Buffer.as_addr", "FStar.ModifiesGen.loc_disjoint_aloc_elim", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.ubuffer_of_buffer", "Prims.bool", "Prims.unit" ]
[]
false
false
true
false
false
let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 =
if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.index
val index (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) :HST.Stack a (requires (fun h -> live h b /\ U32.v i < length b)) (ensures (fun h y h' -> h == h' /\ y == Seq.index (as_seq h b) (U32.v i)))
val index (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) :HST.Stack a (requires (fun h -> live h b /\ U32.v i < length b)) (ensures (fun h y h' -> h == h' /\ y == Seq.index (as_seq h b) (U32.v i)))
let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 1308, "start_col": 0, "start_line": 1305 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: FStar.UInt32.t -> FStar.HyperStack.ST.Stack a
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.Seq.Base.index", "Prims.op_Addition", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__idx", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "FStar.HyperStack.ST.op_Bang", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content" ]
[]
false
true
false
false
false
let index #_ #_ #_ b i =
let open HST in let s = !(Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.g_upd_seq
val g_upd_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (s:Seq.lseq a (length b)) (h:HS.mem{live h b}) :GTot HS.mem
val g_upd_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (s:Seq.lseq a (length b)) (h:HS.mem{live h b}) :GTot HS.mem
let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 97, "end_line": 1315, "start_col": 0, "start_line": 1310 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> s: FStar.Seq.Properties.lseq a (LowStar.Monotonic.Buffer.length b) -> h: FStar.Monotonic.HyperStack.mem{LowStar.Monotonic.Buffer.live h b} -> Prims.GTot FStar.Monotonic.HyperStack.mem
Prims.GTot
[ "sometrivial" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.length", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "Prims.op_Equality", "Prims.int", "FStar.Seq.Base.length", "Prims.bool", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.upd", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "FStar.Seq.Properties.replace_subseq", "FStar.Monotonic.HyperStack.sel" ]
[]
false
false
false
false
false
let g_upd_seq #_ #_ #_ b s h =
if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.recallable
val recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0
val recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0
let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b )
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1365, "start_col": 0, "start_line": 1360 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.l_imp", "Prims.b2t", "Prims.op_Negation", "LowStar.Monotonic.Buffer.g_is_null", "Prims.l_and", "FStar.HyperStack.ST.is_eternal_region", "LowStar.Monotonic.Buffer.frameOf", "FStar.Monotonic.HyperStack.is_mm", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "LowStar.Monotonic.Buffer.buffer_compatible" ]
[]
false
false
false
false
true
let recallable (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) : GTot Type0 =
(not (g_is_null b)) ==> (HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.region_lifetime_sub
val region_lifetime_sub (#a:Type0) (#rrel #rel #subrel:srel a) (b0:mbuffer a rrel rel) (b1:mbuffer a rrel subrel) : Lemma (requires rrel_rel_always_compatible rrel subrel) (ensures (region_lifetime_buf b0 /\ (exists i len. U32.v i + U32.v len <= length b0 /\ b1 == mgsub subrel b0 i len)) ==> region_lifetime_buf b1)
val region_lifetime_sub (#a:Type0) (#rrel #rel #subrel:srel a) (b0:mbuffer a rrel rel) (b1:mbuffer a rrel subrel) : Lemma (requires rrel_rel_always_compatible rrel subrel) (ensures (region_lifetime_buf b0 /\ (exists i len. U32.v i + U32.v len <= length b0 /\ b1 == mgsub subrel b0 i len)) ==> region_lifetime_buf b1)
let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 110, "end_line": 1378, "start_col": 0, "start_line": 1374 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b0: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> b1: LowStar.Monotonic.Buffer.mbuffer a rrel subrel -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.rrel_rel_always_compatible rrel subrel) (ensures LowStar.Monotonic.Buffer.region_lifetime_buf b0 /\ (exists (i: FStar.UInt32.t) (len: FStar.UInt32.t). FStar.UInt32.v i + FStar.UInt32.v len <= LowStar.Monotonic.Buffer.length b0 /\ b1 == LowStar.Monotonic.Buffer.mgsub subrel b0 i len) ==> LowStar.Monotonic.Buffer.region_lifetime_buf b1)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "Prims._assert", "Prims.l_Forall", "Prims.nat", "Prims.l_and", "LowStar.Monotonic.Buffer.compatible_sub_preorder", "Prims.unit" ]
[]
false
false
true
false
false
let region_lifetime_sub #a #rrel #rel #subrel b0 b1 =
match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len: nat) (i: nat) (j: nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.pointer_distinct_sel_disjoint
val pointer_distinct_sel_disjoint (#a:Type0) (#rrel1 #rrel2 #rel1 #rel2:srel a) (b1:mpointer a rrel1 rel1) (b2:mpointer a rrel2 rel2) (h:HS.mem) :Lemma (requires (live h b1 /\ live h b2 /\ get h b1 0 =!= get h b2 0)) (ensures (disjoint b1 b2))
val pointer_distinct_sel_disjoint (#a:Type0) (#rrel1 #rrel2 #rel1 #rel2:srel a) (b1:mpointer a rrel1 rel1) (b2:mpointer a rrel2 rel2) (h:HS.mem) :Lemma (requires (live h b1 /\ live h b2 /\ get h b1 0 =!= get h b2 0)) (ensures (disjoint b1 b2))
let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 1289, "start_col": 0, "start_line": 1282 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mpointer a rrel1 rel1 -> b2: LowStar.Monotonic.Buffer.mpointer a rrel2 rel2 -> h: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.live h b1 /\ LowStar.Monotonic.Buffer.live h b2 /\ ~(LowStar.Monotonic.Buffer.get h b1 0 == LowStar.Monotonic.Buffer.get h b2 0)) (ensures LowStar.Monotonic.Buffer.disjoint b1 b2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mpointer", "FStar.Monotonic.HyperStack.mem", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "Prims.nat", "LowStar.Monotonic.Buffer.as_addr", "LowStar.Monotonic.Buffer.loc_disjoint_buffer", "Prims.unit", "FStar.Monotonic.HyperStack.mreference_distinct_sel_disjoint", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "Prims.bool" ]
[]
false
false
true
false
false
let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h =
if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then (HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2) else loc_disjoint_buffer b1 b2
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.modifies_only_not_unused_in
val modifies_only_not_unused_in (l: loc) (h h' : HS.mem) : Lemma (requires (modifies (loc_union (loc_unused_in h) l) h h')) (ensures (modifies l h h'))
val modifies_only_not_unused_in (l: loc) (h h' : HS.mem) : Lemma (requires (modifies (loc_union (loc_unused_in h) l) h h')) (ensures (modifies l h h'))
let modifies_only_not_unused_in = MG.modifies_only_not_unused_in
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 64, "end_line": 1237, "start_col": 0, "start_line": 1237 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: LowStar.Monotonic.Buffer.loc -> h: FStar.Monotonic.HyperStack.mem -> h': FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_union (LowStar.Monotonic.Buffer.loc_unused_in h) l) h h') (ensures LowStar.Monotonic.Buffer.modifies l h h')
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.ModifiesGen.modifies_only_not_unused_in", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls" ]
[]
true
false
true
false
false
let modifies_only_not_unused_in =
MG.modifies_only_not_unused_in
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.lemma_g_upd_with_same_seq
val lemma_g_upd_with_same_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :Lemma (requires (live h b)) (ensures (g_upd_seq b (as_seq h b) h == h))
val lemma_g_upd_with_same_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h:HS.mem) :Lemma (requires (live h b)) (ensures (g_upd_seq b (as_seq h b) h == h))
let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 1324, "start_col": 0, "start_line": 1317 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> h: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.live h b) (ensures LowStar.Monotonic.Buffer.g_upd_seq b (LowStar.Monotonic.Buffer.as_seq h b) h == h)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.uu___is_Null", "Prims.bool", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.lemma_heap_equality_upd_with_sel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "Prims.unit", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Properties.replace_subseq", "FStar.Seq.Base.slice", "FStar.Monotonic.HyperStack.sel" ]
[]
false
false
true
false
false
let lemma_g_upd_with_same_seq #_ #_ #_ b h =
if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.region_lifetime_buf
val region_lifetime_buf (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) : Type0
val region_lifetime_buf (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) : Type0
let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b )
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1372, "start_col": 0, "start_line": 1367 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b )
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.l_imp", "Prims.b2t", "Prims.op_Negation", "LowStar.Monotonic.Buffer.g_is_null", "Prims.l_and", "FStar.Monotonic.HyperStack.is_heap_color", "FStar.Monotonic.HyperHeap.color", "LowStar.Monotonic.Buffer.frameOf", "FStar.Monotonic.HyperStack.is_mm", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "LowStar.Monotonic.Buffer.buffer_compatible" ]
[]
false
false
false
false
true
let region_lifetime_buf #_ #_ #_ b =
(not (g_is_null b)) ==> (HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.moffset
val moffset (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) :HST.Stack (mbuffer a rrel sub_rel) (requires (fun h -> U32.v i <= length b /\ compatible_sub b i (U32.sub (len b) i) sub_rel /\ live h b)) (ensures (fun h y h' -> h == h' /\ y == mgsub sub_rel b i (U32.sub (len b) i)))
val moffset (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) :HST.Stack (mbuffer a rrel sub_rel) (requires (fun h -> U32.v i <= length b /\ compatible_sub b i (U32.sub (len b) i) sub_rel /\ live h b)) (ensures (fun h y h' -> h == h' /\ y == mgsub sub_rel b i (U32.sub (len b) i)))
let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i)))
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 87, "end_line": 1303, "start_col": 0, "start_line": 1299 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
sub_rel: LowStar.Monotonic.Buffer.srel a -> b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: FStar.UInt32.t -> FStar.HyperStack.ST.Stack (LowStar.Monotonic.Buffer.mbuffer a rrel sub_rel)
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Null", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.add", "FStar.Ghost.hide", "FStar.UInt32.sub" ]
[]
false
true
false
false
false
let moffset #a #rrel #rel sub_rel b i =
match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i)))
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.recall
val recall (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.Stack unit (requires (fun m -> recallable b \/ (region_lifetime_buf b /\ HS.live_region m (frameOf b)))) (ensures (fun m0 _ m1 -> m0 == m1 /\ live m1 b))
val recall (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.Stack unit (requires (fun m -> recallable b \/ (region_lifetime_buf b /\ HS.live_region m (frameOf b)))) (ensures (fun m0 _ m1 -> m0 == m1 /\ live m1 b))
let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 78, "end_line": 1397, "start_col": 0, "start_line": 1397 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.uu___is_Null", "Prims.unit", "Prims.bool", "FStar.HyperStack.ST.recall", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content" ]
[]
false
true
false
false
false
let recall #_ #_ #_ b =
if Null? b then () else HST.recall (Buffer?.content b)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.witnessed
val witnessed (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Type0
val witnessed (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Type0
let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 1409, "start_col": 0, "start_line": 1405 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> p: LowStar.Monotonic.Buffer.spred a -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.spred", "FStar.Seq.Base.empty", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.token_p", "LowStar.Monotonic.Buffer.spred_as_mempred" ]
[]
false
false
false
false
true
let witnessed #_ #rrel #rel b p =
match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.upd'
val upd' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (v:a) :HST.Stack unit (requires (fun h -> live h b /\ U32.v i < length b /\ rel (as_seq h b) (Seq.upd (as_seq h b) (U32.v i) v))) (ensures (fun h _ h' -> h' == g_upd b (U32.v i) v h))
val upd' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (v:a) :HST.Stack unit (requires (fun h -> live h b /\ U32.v i < length b /\ rel (as_seq h b) (Seq.upd (as_seq h b) (U32.v i) v))) (ensures (fun h _ h' -> h' == g_upd b (U32.v i) v h))
let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 15, "end_line": 1358, "start_col": 0, "start_line": 1348 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: FStar.UInt32.t -> v: a -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.op_Colon_Equals", "Prims.unit", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Properties.replace_subseq", "FStar.Seq.Base.upd", "LowStar.Monotonic.Buffer.as_seq", "FStar.Seq.Base.seq", "FStar.Seq.Base.slice", "FStar.HyperStack.ST.op_Bang", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get" ]
[]
false
true
false
false
false
let upd' #_ #_ #_ b i v =
let open HST in let h = get () in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v))); content := sf
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.recall_p
val recall_p (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.ST unit (requires (fun h0 -> (recallable b \/ live h0 b) /\ b `witnessed` p)) (ensures (fun h0 _ h1 -> h0 == h1 /\ live h0 b /\ p (as_seq h0 b)))
val recall_p (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.ST unit (requires (fun h0 -> (recallable b \/ live h0 b) /\ b `witnessed` p)) (ensures (fun h0 _ h1 -> h0 == h1 /\ live h0 b /\ p (as_seq h0 b)))
let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 71, "end_line": 1435, "start_col": 0, "start_line": 1432 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> p: LowStar.Monotonic.Buffer.spred a -> FStar.HyperStack.ST.ST Prims.unit
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.spred", "Prims.unit", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.recall_p", "LowStar.Monotonic.Buffer.spred_as_mempred" ]
[]
false
true
false
false
false
let recall_p #_ #_ #_ b p =
match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.msub
val msub (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (len:Ghost.erased U32.t) :HST.Stack (mbuffer a rrel sub_rel) (requires (fun h -> U32.v i + U32.v (Ghost.reveal len) <= length b /\ compatible_sub b i (Ghost.reveal len) sub_rel /\ live h b)) (ensures (fun h y h' -> h == h' /\ y == mgsub sub_rel b i (Ghost.reveal len)))
val msub (#a:Type0) (#rrel #rel:srel a) (sub_rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (len:Ghost.erased U32.t) :HST.Stack (mbuffer a rrel sub_rel) (requires (fun h -> U32.v i + U32.v (Ghost.reveal len) <= length b /\ compatible_sub b i (Ghost.reveal len) sub_rel /\ live h b)) (ensures (fun h y h' -> h == h' /\ y == mgsub sub_rel b i (Ghost.reveal len)))
let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 1297, "start_col": 0, "start_line": 1293 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
sub_rel: LowStar.Monotonic.Buffer.srel a -> b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: FStar.UInt32.t -> len: FStar.Ghost.erased FStar.UInt32.t -> FStar.HyperStack.ST.Stack (LowStar.Monotonic.Buffer.mbuffer a rrel sub_rel)
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.Ghost.erased", "LowStar.Monotonic.Buffer.Null", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.add" ]
[]
false
true
false
false
false
let msub #a #rrel #rel sub_rel b i len =
match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.lemma_stable_on_rel_is_stable_on_rrel
val lemma_stable_on_rel_is_stable_on_rrel (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (p: spred a) : Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b)))
val lemma_stable_on_rel_is_stable_on_rrel (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (p: spred a) : Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b)))
let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 32, "end_line": 1421, "start_col": 8, "start_line": 1411 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> p: LowStar.Monotonic.Buffer.spred a -> FStar.Pervasives.Lemma (requires Buffer? b /\ LowStar.Monotonic.Buffer.stable_on p rel) (ensures FStar.HyperStack.ST.stable_on (LowStar.Monotonic.Buffer.spred_as_mempred b p) (Buffer?.content b))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.spred", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.Classical.forall_intro_2", "FStar.Monotonic.HyperStack.mem", "Prims.l_imp", "Prims.l_and", "FStar.Monotonic.HyperStack.sel", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Classical.arrow_to_impl", "LowStar.Monotonic.Buffer.buffer_compatible", "Prims._assert", "LowStar.Monotonic.Buffer.as_seq", "FStar.HyperStack.ST.mem_predicate", "LowStar.Monotonic.Buffer.spred_as_mempred", "LowStar.Monotonic.Buffer.uu___is_Buffer", "LowStar.Monotonic.Buffer.stable_on", "FStar.HyperStack.ST.stable_on", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content" ]
[]
false
false
true
false
false
let lemma_stable_on_rel_is_stable_on_rrel (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (p: spred a) : Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) =
let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1: HS.mem) : Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.freeable
val freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0
val freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0
let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 1456, "start_col": 0, "start_line": 1450 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.l_and", "Prims.b2t", "Prims.op_Negation", "LowStar.Monotonic.Buffer.g_is_null", "FStar.Monotonic.HyperStack.is_mm", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "FStar.Monotonic.HyperStack.is_heap_color", "FStar.Monotonic.HyperHeap.color", "LowStar.Monotonic.Buffer.frameOf", "Prims.op_GreaterThan", "Prims.eq2", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.__proj__Buffer__item__idx", "FStar.UInt32.__uint_to_t", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.__proj__Buffer__item__length" ]
[]
false
false
false
false
true
let freeable (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) =
(not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.empty_disjoint
val empty_disjoint (#t1 #t2: Type) (#rrel1 #rel1: srel t1) (#rrel2 #rel2: srel t2) (b1: mbuffer t1 rrel1 rel1) (b2: mbuffer t2 rrel2 rel2) : Lemma (requires (length b1 == 0)) (ensures (disjoint b1 b2))
val empty_disjoint (#t1 #t2: Type) (#rrel1 #rel1: srel t1) (#rrel2 #rel2: srel t2) (b1: mbuffer t1 rrel1 rel1) (b2: mbuffer t2 rrel2 rel2) : Lemma (requires (length b1 == 0)) (ensures (disjoint b1 b2))
let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else ()
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 9, "end_line": 1268, "start_col": 0, "start_line": 1262 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mbuffer t1 rrel1 rel1 -> b2: LowStar.Monotonic.Buffer.mbuffer t2 rrel2 rel2 -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.length b1 == 0) (ensures LowStar.Monotonic.Buffer.disjoint b1 b2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "Prims.nat", "LowStar.Monotonic.Buffer.as_addr", "FStar.ModifiesGen.loc_disjoint_aloc_intro", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.ubuffer_of_buffer", "Prims.bool", "Prims.unit" ]
[]
false
false
true
false
false
let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 =
let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.free
val free (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.ST unit (requires (fun h0 -> live h0 b /\ freeable b)) (ensures (fun h0 _ h1 -> (not (g_is_null b)) /\ Map.domain (HS.get_hmap h1) `Set.equal` Map.domain (HS.get_hmap h0) /\ (HS.get_tip h1) == (HS.get_tip h0) /\ modifies (loc_addr_of_buffer b) h0 h1 /\ HS.live_region h1 (frameOf b)))
val free (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :HST.ST unit (requires (fun h0 -> live h0 b /\ freeable b)) (ensures (fun h0 _ h1 -> (not (g_is_null b)) /\ Map.domain (HS.get_hmap h1) `Set.equal` Map.domain (HS.get_hmap h0) /\ (HS.get_tip h1) == (HS.get_tip h0) /\ modifies (loc_addr_of_buffer b) h0 h1 /\ HS.live_region h1 (frameOf b)))
let free #_ #_ #_ b = HST.rfree (Buffer?.content b)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 1458, "start_col": 0, "start_line": 1458 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> FStar.HyperStack.ST.ST Prims.unit
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.HyperStack.ST.rfree", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "Prims.unit" ]
[]
false
true
false
false
false
let free #_ #_ #_ b =
HST.rfree (Buffer?.content b)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.witness_p
val witness_p (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.ST unit (requires (fun h0 -> p (as_seq h0 b) /\ p `stable_on` rel)) (ensures (fun h0 _ h1 -> h0 == h1 /\ b `witnessed` p))
val witness_p (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.ST unit (requires (fun h0 -> p (as_seq h0 b) /\ p `stable_on` rel)) (ensures (fun h0 _ h1 -> h0 == h1 /\ b `witnessed` p))
let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 1430, "start_col": 0, "start_line": 1423 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> p: LowStar.Monotonic.Buffer.spred a -> FStar.HyperStack.ST.ST Prims.unit
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.spred", "Prims.unit", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.witness_p", "LowStar.Monotonic.Buffer.spred_as_mempred", "Prims._assert", "FStar.HyperStack.ST.stable_on", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "LowStar.Monotonic.Buffer.lemma_stable_on_rel_is_stable_on_rrel" ]
[]
false
true
false
false
false
let witness_p #a #rrel #rel b p =
match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.recallable_mgsub
val recallable_mgsub (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a) :Lemma (requires (U32.v i + U32.v len <= length b /\ compatible_sub b i len sub_rel /\ recallable b)) (ensures (recallable (mgsub sub_rel b i len))) [SMTPatOr [ [SMTPat (recallable (mgsub sub_rel b i len))]; [SMTPat (recallable b); SMTPat (mgsub sub_rel b i len);] ]]
val recallable_mgsub (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:U32.t) (len:U32.t) (sub_rel:srel a) :Lemma (requires (U32.v i + U32.v len <= length b /\ compatible_sub b i len sub_rel /\ recallable b)) (ensures (recallable (mgsub sub_rel b i len))) [SMTPatOr [ [SMTPat (recallable (mgsub sub_rel b i len))]; [SMTPat (recallable b); SMTPat (mgsub sub_rel b i len);] ]]
let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 1388, "start_col": 0, "start_line": 1382 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: FStar.UInt32.t -> len: FStar.UInt32.t -> sub_rel: LowStar.Monotonic.Buffer.srel a -> FStar.Pervasives.Lemma (requires FStar.UInt32.v i + FStar.UInt32.v len <= LowStar.Monotonic.Buffer.length b /\ LowStar.Monotonic.Buffer.compatible_sub b i len sub_rel /\ LowStar.Monotonic.Buffer.recallable b) (ensures LowStar.Monotonic.Buffer.recallable (LowStar.Monotonic.Buffer.mgsub sub_rel b i len)) [ SMTPatOr [ [ SMTPat (LowStar.Monotonic.Buffer.recallable (LowStar.Monotonic.Buffer.mgsub sub_rel b i len)) ]; [ SMTPat (LowStar.Monotonic.Buffer.recallable b); SMTPat (LowStar.Monotonic.Buffer.mgsub sub_rel b i len) ] ] ]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatibility_is_transitive", "Prims.unit" ]
[]
false
false
true
false
false
let recallable_mgsub #_ #rrel #rel b i len sub_rel =
match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mmalloc
val mmalloc (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:a) (len:U32.t) :HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ freeable b}) (requires (fun _ -> malloc_pre r len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init)))
val mmalloc (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:a) (len:U32.t) :HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ freeable b}) (requires (fun _ -> malloc_pre r len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init)))
let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 1505, "start_col": 0, "start_line": 1504 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.Monotonic.HyperHeap.rid -> init: a -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) {LowStar.Monotonic.Buffer.frameOf b == r /\ LowStar.Monotonic.Buffer.freeable b})
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.Monotonic.HyperHeap.rid", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.alloc_heap_common", "FStar.Seq.Base.create", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.lmbuffer", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.freeable" ]
[]
false
true
false
false
false
let mmalloc #_ #_ r init len =
alloc_heap_common r len (Seq.create (U32.v len) init) true
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.freeable_disjoint
val freeable_disjoint (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires (freeable b1 /\ length b2 > 0 /\ disjoint b1 b2)) (ensures (frameOf b1 <> frameOf b2 \/ as_addr b1 <> as_addr b2))
val freeable_disjoint (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires (freeable b1 /\ length b2 > 0 /\ disjoint b1 b2)) (ensures (frameOf b1 <> frameOf b2 \/ as_addr b1 <> as_addr b2))
let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 139, "end_line": 1464, "start_col": 0, "start_line": 1462 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mbuffer a1 rrel1 rel1 -> b2: LowStar.Monotonic.Buffer.mbuffer a2 rrel2 rel2 -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.freeable b1 /\ LowStar.Monotonic.Buffer.length b2 > 0 /\ LowStar.Monotonic.Buffer.disjoint b1 b2) (ensures LowStar.Monotonic.Buffer.frameOf b1 <> LowStar.Monotonic.Buffer.frameOf b2 \/ LowStar.Monotonic.Buffer.as_addr b1 <> LowStar.Monotonic.Buffer.as_addr b2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "Prims.nat", "LowStar.Monotonic.Buffer.as_addr", "FStar.ModifiesGen.loc_disjoint_aloc_elim", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.ubuffer_of_buffer", "Prims.bool", "Prims.unit" ]
[]
false
false
true
false
false
let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 =
if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.abuffer'
val abuffer' (region: HS.rid) (addr: nat) : Tot Type0
val abuffer' (region: HS.rid) (addr: nat) : Tot Type0
let abuffer' = ubuffer'
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 23, "end_line": 1650, "start_col": 0, "start_line": 1650 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options let fill #t #rrel #rel b z len = fill' b z len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
region: FStar.Monotonic.HyperHeap.rid -> addr: Prims.nat -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowStar.Monotonic.Buffer.ubuffer'" ]
[]
false
false
false
true
true
let abuffer' =
ubuffer'
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.witnessed_functorial
val witnessed_functorial (#a:Type0) (#rrel #rel1 #rel2:srel a) (b1:mbuffer a rrel rel1) (b2:mbuffer a rrel rel2) (i len:U32.t) (s1 s2:spred a) : Lemma (requires rrel_rel_always_compatible rrel rel1 /\ //e.g. trivial_preorder, immutable preorder etc. U32.v i + U32.v len <= length b1 /\ b2 == mgsub rel2 b1 i len /\ //the underlying allocation unit for b1 and b2 must be the same witnessed b1 s1 /\ (forall h. s1 (as_seq h b1) ==> s2 (as_seq h b2))) (ensures witnessed b2 s2)
val witnessed_functorial (#a:Type0) (#rrel #rel1 #rel2:srel a) (b1:mbuffer a rrel rel1) (b2:mbuffer a rrel rel2) (i len:U32.t) (s1 s2:spred a) : Lemma (requires rrel_rel_always_compatible rrel rel1 /\ //e.g. trivial_preorder, immutable preorder etc. U32.v i + U32.v len <= length b1 /\ b2 == mgsub rel2 b1 i len /\ //the underlying allocation unit for b1 and b2 must be the same witnessed b1 s1 /\ (forall h. s1 (as_seq h b1) ==> s2 (as_seq h b2))) (ensures witnessed b2 s2)
let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 1442, "start_col": 0, "start_line": 1437 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mbuffer a rrel rel1 -> b2: LowStar.Monotonic.Buffer.mbuffer a rrel rel2 -> i: FStar.UInt32.t -> len: FStar.UInt32.t -> s1: LowStar.Monotonic.Buffer.spred a -> s2: LowStar.Monotonic.Buffer.spred a -> FStar.Pervasives.Lemma (requires LowStar.Monotonic.Buffer.rrel_rel_always_compatible rrel rel1 /\ FStar.UInt32.v i + FStar.UInt32.v len <= LowStar.Monotonic.Buffer.length b1 /\ b2 == LowStar.Monotonic.Buffer.mgsub rel2 b1 i len /\ LowStar.Monotonic.Buffer.witnessed b1 s1 /\ (forall (h: FStar.Monotonic.HyperStack.mem). s1 (LowStar.Monotonic.Buffer.as_seq h b1) ==> s2 (LowStar.Monotonic.Buffer.as_seq h b2)) ) (ensures LowStar.Monotonic.Buffer.witnessed b2 s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.spred", "FStar.Pervasives.Native.Mktuple2", "Prims._assert", "Prims.eq2", "FStar.Seq.Base.seq", "LowStar.Monotonic.Buffer.as_seq", "FStar.Monotonic.HyperStack.empty_mem", "FStar.Seq.Base.empty", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.token_functoriality", "LowStar.Monotonic.Buffer.spred_as_mempred", "Prims.unit", "Prims.l_Forall", "Prims.nat", "Prims.l_and", "LowStar.Monotonic.Buffer.compatible_sub_preorder" ]
[]
false
false
true
false
false
let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 =
match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len: nat) (i: nat) (j: nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.witnessed_functorial_st
val witnessed_functorial_st (#a:Type0) (#rrel #rel1 #rel2:srel a) (b1:mbuffer a rrel rel1) (b2:mbuffer a rrel rel2) (i len:U32.t) (s1 s2:spred a) : HST.Stack unit (requires fun h -> live h b1 /\ U32.v i + U32.v len <= length b1 /\ b2 == mgsub rel2 b1 i len /\ witnessed b1 s1 /\ (forall h. s1 (as_seq h b1) ==> s2 (as_seq h b2))) (ensures fun h0 _ h1 -> h0 == h1 /\ witnessed b2 s2)
val witnessed_functorial_st (#a:Type0) (#rrel #rel1 #rel2:srel a) (b1:mbuffer a rrel rel1) (b2:mbuffer a rrel rel2) (i len:U32.t) (s1 s2:spred a) : HST.Stack unit (requires fun h -> live h b1 /\ U32.v i + U32.v len <= length b1 /\ b2 == mgsub rel2 b1 i len /\ witnessed b1 s1 /\ (forall h. s1 (as_seq h b1) ==> s2 (as_seq h b2))) (ensures fun h0 _ h1 -> h0 == h1 /\ witnessed b2 s2)
let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 1448, "start_col": 0, "start_line": 1444 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Monotonic.Buffer.mbuffer a rrel rel1 -> b2: LowStar.Monotonic.Buffer.mbuffer a rrel rel2 -> i: FStar.UInt32.t -> len: FStar.UInt32.t -> s1: LowStar.Monotonic.Buffer.spred a -> s2: LowStar.Monotonic.Buffer.spred a -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.spred", "FStar.Pervasives.Native.Mktuple2", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "FStar.HyperStack.ST.token_functoriality", "LowStar.Monotonic.Buffer.spred_as_mempred", "Prims.unit" ]
[]
false
true
false
false
false
let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 =
match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.malloca
val malloca (#a:Type0) (#rrel:srel a) (init:a) (len:U32.t) :HST.StackInline (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> alloca_pre len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init) /\ frameOf b == HS.get_tip h0))
val malloca (#a:Type0) (#rrel:srel a) (init:a) (len:U32.t) :HST.StackInline (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> alloca_pre len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init) /\ frameOf b == HS.get_tip h0))
let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 1515, "start_col": 0, "start_line": 1510 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
init: a -> len: FStar.UInt32.t -> FStar.HyperStack.ST.StackInline (LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len))
FStar.HyperStack.ST.StackInline
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.salloc", "FStar.Seq.Base.create", "FStar.HyperStack.ST.mstackref", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive" ]
[]
false
true
false
false
false
let malloca #a #rrel init len =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mgcmalloc_and_blit
val mgcmalloc_and_blit (#a:Type0) (#rrel:srel a) (r:HS.rid) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ recallable b}) (requires fun h0 -> malloc_pre r len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
val mgcmalloc_and_blit (#a:Type0) (#rrel:srel a) (r:HS.rid) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ recallable b}) (requires fun h0 -> malloc_pre r len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 64, "end_line": 1502, "start_col": 0, "start_line": 1501 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.Monotonic.HyperHeap.rid -> src: LowStar.Monotonic.Buffer.mbuffer a rrel1 rel1 -> id_src: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) {LowStar.Monotonic.Buffer.frameOf b == r /\ LowStar.Monotonic.Buffer.recallable b})
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.alloc_heap_common", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "FStar.Seq.Base.seq", "Prims.eq2", "Prims.int", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.Seq.Base.length", "LowStar.Monotonic.Buffer.read_sub_buffer", "Prims.l_and", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.recallable" ]
[]
false
true
false
false
false
let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len =
alloc_heap_common r len (read_sub_buffer src id_src len) false
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mgcmalloc
val mgcmalloc (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:a) (len:U32.t) :HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ recallable b}) (requires (fun _ -> malloc_pre r len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init)))
val mgcmalloc (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:a) (len:U32.t) :HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ recallable b}) (requires (fun _ -> malloc_pre r len)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init)))
let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 1484, "start_col": 0, "start_line": 1483 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.Monotonic.HyperHeap.rid -> init: a -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) {LowStar.Monotonic.Buffer.frameOf b == r /\ LowStar.Monotonic.Buffer.recallable b})
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.Monotonic.HyperHeap.rid", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.alloc_heap_common", "FStar.Seq.Base.create", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.lmbuffer", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.recallable" ]
[]
false
true
false
false
false
let mgcmalloc #_ #_ r init len =
alloc_heap_common r len (Seq.create (U32.v len) init) false
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.malloca_and_blit
val malloca_and_blit (#a:Type0) (#rrel:srel a) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.StackInline (lmbuffer a rrel rrel (U32.v len)) (requires fun h0 -> alloca_pre len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)) /\ frameOf b == HS.get_tip h0)
val malloca_and_blit (#a:Type0) (#rrel:srel a) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.StackInline (lmbuffer a rrel rrel (U32.v len)) (requires fun h0 -> alloca_pre len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)) /\ frameOf b == HS.get_tip h0)
let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 1522, "start_col": 0, "start_line": 1517 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
src: LowStar.Monotonic.Buffer.mbuffer a rrel1 rel1 -> id_src: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.StackInline (LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len))
FStar.HyperStack.ST.StackInline
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.salloc", "FStar.HyperStack.ST.mstackref", "LowStar.Monotonic.Buffer.read_sub_buffer", "FStar.Seq.Base.seq", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive" ]
[]
false
true
false
false
false
let malloca_and_blit #a #rrel #_ #_ src id_src len =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len)
false
Hacl.K256.PrecompTable.fst
Hacl.K256.PrecompTable.precomp_g_pow2_192_table_lseq_w4
val precomp_g_pow2_192_table_lseq_w4 : LSeq.lseq uint64 240
val precomp_g_pow2_192_table_lseq_w4 : LSeq.lseq uint64 240
let precomp_g_pow2_192_table_lseq_w4 : LSeq.lseq uint64 240 = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15); Seq.seq_of_list precomp_g_pow2_192_table_list_w4
{ "file_name": "code/k256/Hacl.K256.PrecompTable.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 50, "end_line": 266, "start_col": 0, "start_line": 264 }
module Hacl.K256.PrecompTable open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open Lib.IntTypes open Lib.Buffer module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module LE = Lib.Exponentiation module SE = Spec.Exponentiation module SPT = Hacl.Spec.PrecompBaseTable module SPT256 = Hacl.Spec.PrecompBaseTable256 module SPTK = Hacl.Spec.K256.PrecompTable module S = Spec.K256 module SL = Spec.K256.Lemmas open Hacl.Impl.K256.Point include Hacl.Impl.K256.Group #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" let proj_point_to_list p = SPTK.proj_point_to_list_lemma p; SPTK.proj_point_to_list p let lemma_refl x = SPTK.proj_point_to_list_lemma x //----------------- inline_for_extraction noextract let proj_g_pow2_64 : S.proj_point = [@inline_let] let rX : S.felem = 0x46ec0aa60b0b98c37b29371784676ad967b7beb1a941ddb6fbbff95b44cb788b in [@inline_let] let rY : S.felem = 0x6b946755bbc6b677576579c990a1ccf14a710545251a1428fabbf02f40268e63 in [@inline_let] let rZ : S.felem = 0x3c114b2ac17c199ec9eba9f7cc64dc459ca2e53f5bbead2b4e618b318ffcc00e in (rX, rY, rZ) val lemma_proj_g_pow2_64_eval : unit -> Lemma (SE.exp_pow2 S.mk_k256_concrete_ops S.g 64 == proj_g_pow2_64) let lemma_proj_g_pow2_64_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_k256_concrete_ops S.g 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops S.g 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops S.g 64); let rX : S.felem = 0x46ec0aa60b0b98c37b29371784676ad967b7beb1a941ddb6fbbff95b44cb788b in let rY : S.felem = 0x6b946755bbc6b677576579c990a1ccf14a710545251a1428fabbf02f40268e63 in let rZ : S.felem = 0x3c114b2ac17c199ec9eba9f7cc64dc459ca2e53f5bbead2b4e618b318ffcc00e in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_128 : S.proj_point = [@inline_let] let rX : S.felem = 0x98299efbc8e459915404ae015b48cac3b929e0158665f3c7fa5489fbd25c4297 in [@inline_let] let rY : S.felem = 0xf1e5cbc9579e7d11a31681e947c2959ae0298a006d1c06ab1ad93d6716ea50cc in [@inline_let] let rZ : S.felem = 0x5c53ffe15001674a2eacb60c9327a8b0ddbd93a0fa6d90309de6cc124133938b in (rX, rY, rZ) val lemma_proj_g_pow2_128_eval : unit -> Lemma (SE.exp_pow2 S.mk_k256_concrete_ops proj_g_pow2_64 64 == proj_g_pow2_128) let lemma_proj_g_pow2_128_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_k256_concrete_ops proj_g_pow2_64 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64); let rX : S.felem = 0x98299efbc8e459915404ae015b48cac3b929e0158665f3c7fa5489fbd25c4297 in let rY : S.felem = 0xf1e5cbc9579e7d11a31681e947c2959ae0298a006d1c06ab1ad93d6716ea50cc in let rZ : S.felem = 0x5c53ffe15001674a2eacb60c9327a8b0ddbd93a0fa6d90309de6cc124133938b in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_192 : S.proj_point = [@inline_let] let rX : S.felem = 0xbd382b67d20492b1480ca58a6d7d617ba413a9bc7c2f1cff51301ef960fb245c in [@inline_let] let rY : S.felem = 0x0b232afcf692673aa714357c524c07867a64ea3b9dfab53f0e74622159e86b0d in [@inline_let] let rZ : S.felem = 0x028a1380449aede5df8219420b458d464a6a4773ac91e8305237878cef1cffa6 in (rX, rY, rZ) val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_k256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192) let lemma_proj_g_pow2_192_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_k256_concrete_ops proj_g_pow2_128 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_128 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_128 64); let rX : S.felem = 0xbd382b67d20492b1480ca58a6d7d617ba413a9bc7c2f1cff51301ef960fb245c in let rY : S.felem = 0x0b232afcf692673aa714357c524c07867a64ea3b9dfab53f0e74622159e86b0d in let rZ : S.felem = 0x028a1380449aede5df8219420b458d464a6a4773ac91e8305237878cef1cffa6 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) // let proj_g_pow2_64 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops S.g 64) // let proj_g_pow2_128 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_64 64) // let proj_g_pow2_192 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_k256_concrete_ops proj_g_pow2_128 64) inline_for_extraction noextract let proj_g_pow2_64_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_64) inline_for_extraction noextract let proj_g_pow2_128_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_128) inline_for_extraction noextract let proj_g_pow2_192_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_192) let proj_g_pow2_64_lseq : LSeq.lseq uint64 15 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); Seq.seq_of_list proj_g_pow2_64_list let proj_g_pow2_128_lseq : LSeq.lseq uint64 15 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); Seq.seq_of_list proj_g_pow2_128_list let proj_g_pow2_192_lseq : LSeq.lseq uint64 15 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); Seq.seq_of_list proj_g_pow2_192_list val proj_g_pow2_64_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_64 == pow_point (pow2 64) g_aff) let proj_g_pow2_64_lemma () = lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_64_lemma S.mk_k256_concrete_ops S.g val proj_g_pow2_128_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_128 == pow_point (pow2 128) g_aff) let proj_g_pow2_128_lemma () = lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_128_lemma S.mk_k256_concrete_ops S.g val proj_g_pow2_192_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_192 == pow_point (pow2 192) g_aff) let proj_g_pow2_192_lemma () = lemma_proj_g_pow2_192_eval (); lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_192_lemma S.mk_k256_concrete_ops S.g let proj_g_pow2_64_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); proj_g_pow2_64_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_64 let proj_g_pow2_128_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); proj_g_pow2_128_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_128 let proj_g_pow2_192_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); proj_g_pow2_192_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_192 let mk_proj_g_pow2_64 () = createL proj_g_pow2_64_list let mk_proj_g_pow2_128 () = createL proj_g_pow2_128_list let mk_proj_g_pow2_192 () = createL proj_g_pow2_192_list //---------------- /// window size = 4; precomputed table = [[0]G, [1]G, ..., [15]G] inline_for_extraction noextract let precomp_basepoint_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} = normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15) let precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 240 = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15); Seq.seq_of_list precomp_basepoint_table_list_w4 let precomp_basepoint_table_lemma_w4 () = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table S.g 15); SPT.precomp_base_table_lemma mk_k256_precomp_base_table S.g 16 precomp_basepoint_table_lseq_w4 let precomp_basepoint_table_w4: x:glbuffer uint64 240ul{witnessed x precomp_basepoint_table_lseq_w4 /\ recallable x} = createL_global precomp_basepoint_table_list_w4 /// window size = 4; precomputed table = [[0]([pow2 64]G), [1]([pow2 64]G), ..., [15]([pow2 64]G)] inline_for_extraction noextract let precomp_g_pow2_64_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} = normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15) let precomp_g_pow2_64_table_lseq_w4 : LSeq.lseq uint64 240 = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15); Seq.seq_of_list precomp_g_pow2_64_table_list_w4 let precomp_g_pow2_64_table_lemma_w4 () = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_64 15); SPT.precomp_base_table_lemma mk_k256_precomp_base_table proj_g_pow2_64 16 precomp_g_pow2_64_table_lseq_w4; proj_g_pow2_64_lemma () let precomp_g_pow2_64_table_w4: x:glbuffer uint64 240ul{witnessed x precomp_g_pow2_64_table_lseq_w4 /\ recallable x} = createL_global precomp_g_pow2_64_table_list_w4 /// window size = 4; precomputed table = [[0]([pow2 128]G), [1]([pow2 128]G),...,[15]([pow2 128]G)] inline_for_extraction noextract let precomp_g_pow2_128_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} = normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15) let precomp_g_pow2_128_table_lseq_w4 : LSeq.lseq uint64 240 = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15); Seq.seq_of_list precomp_g_pow2_128_table_list_w4 let precomp_g_pow2_128_table_lemma_w4 () = normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_128 15); SPT.precomp_base_table_lemma mk_k256_precomp_base_table proj_g_pow2_128 16 precomp_g_pow2_64_table_lseq_w4; proj_g_pow2_128_lemma () let precomp_g_pow2_128_table_w4: x:glbuffer uint64 240ul{witnessed x precomp_g_pow2_128_table_lseq_w4 /\ recallable x} = createL_global precomp_g_pow2_128_table_list_w4 /// window size = 4; precomputed table = [[0]([pow2 192]G), [1]([pow2 192]G),...,[15]([pow2 192]G)] inline_for_extraction noextract let precomp_g_pow2_192_table_list_w4: x:list uint64{FStar.List.Tot.length x = 240} = normalize_term (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15)
{ "checked_file": "/", "dependencies": [ "Spec.K256.Lemmas.fsti.checked", "Spec.K256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Exponentiation.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.PrecompBaseTable256.fsti.checked", "Hacl.Spec.PrecompBaseTable.fsti.checked", "Hacl.Spec.K256.PrecompTable.fsti.checked", "Hacl.Impl.K256.Point.fsti.checked", "Hacl.Impl.K256.Group.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.K256.PrecompTable.fst" }
[ { "abbrev": true, "full_module": "Spec.K256.Lemmas", "short_module": "SL" }, { "abbrev": true, "full_module": "Hacl.Spec.K256.PrecompTable", "short_module": "SPTK" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable256", "short_module": "SPT256" }, { "abbrev": true, "full_module": "Lib.Exponentiation", "short_module": "LE" }, { "abbrev": false, "full_module": "Hacl.Impl.K256.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.K256.Point", "short_module": null }, { "abbrev": true, "full_module": "Spec.K256", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable", "short_module": "SPT" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation.Definitions", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation.Definition", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.K256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.K256", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Lib.Sequence.lseq Lib.IntTypes.uint64 240
Prims.Tot
[ "total" ]
[]
[ "FStar.Seq.Base.seq_of_list", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "Hacl.K256.PrecompTable.precomp_g_pow2_192_table_list_w4", "Prims.unit", "FStar.Pervasives.normalize_term_spec", "Prims.list", "Lib.IntTypes.uint_t", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.List.Tot.Base.length", "FStar.Mul.op_Star", "Prims.op_Addition", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "FStar.UInt32.uint_to_t", "Hacl.Spec.PrecompBaseTable.precomp_base_table_list", "Spec.K256.PointOps.proj_point", "Hacl.K256.PrecompTable.mk_k256_precomp_base_table", "Hacl.K256.PrecompTable.proj_g_pow2_192", "Lib.Sequence.lseq", "Lib.IntTypes.uint64" ]
[]
false
false
false
false
false
let precomp_g_pow2_192_table_lseq_w4:LSeq.lseq uint64 240 =
normalize_term_spec (SPT.precomp_base_table_list mk_k256_precomp_base_table proj_g_pow2_192 15); Seq.seq_of_list precomp_g_pow2_192_table_list_w4
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.alloc_heap_common
val alloc_heap_common (#a: Type0) (#rrel: srel a) (r: HST.erid) (len: U32.t{U32.v len > 0}) (s: Seq.seq a {Seq.length s == U32.v len}) (mm: bool) : HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b))
val alloc_heap_common (#a: Type0) (#rrel: srel a) (r: HST.erid) (len: U32.t{U32.v len > 0}) (s: Seq.seq a {Seq.length s == U32.v len}) (mm: bool) : HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b))
let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 1481, "start_col": 8, "start_line": 1466 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.HyperStack.ST.erid -> len: FStar.UInt32.t{FStar.UInt32.v len > 0} -> s: FStar.Seq.Base.seq a {FStar.Seq.Base.length s == FStar.UInt32.v len} -> mm: Prims.bool -> FStar.HyperStack.ST.ST (LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len))
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.HyperStack.ST.erid", "FStar.UInt32.t", "Prims.b2t", "Prims.op_GreaterThan", "FStar.UInt32.v", "FStar.Seq.Base.seq", "Prims.eq2", "Prims.int", "Prims.l_or", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.UInt32.n", "FStar.Seq.Base.length", "Prims.bool", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.ralloc_mm", "FStar.HyperStack.ST.mmmref", "FStar.HyperStack.ST.ralloc", "FStar.HyperStack.ST.mref", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive", "FStar.Monotonic.HyperStack.mem", "Prims.l_True", "Prims.l_and", "LowStar.Monotonic.Buffer.alloc_post_mem_common", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "FStar.Monotonic.HyperStack.is_mm", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "LowStar.Monotonic.Buffer.__proj__Buffer__item__idx", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.__proj__Buffer__item__length" ]
[]
false
true
false
false
false
let alloc_heap_common (#a: Type0) (#rrel: srel a) (r: HST.erid) (len: U32.t{U32.v len > 0}) (s: Seq.seq a {Seq.length s == U32.v len}) (mm: bool) : HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.read_sub_buffer
val read_sub_buffer (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (idx len: U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len))
val read_sub_buffer (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (idx len: U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len))
let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 1499, "start_col": 8, "start_line": 1486 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> idx: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (FStar.Seq.Base.seq a)
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.Seq.Base.slice", "FStar.UInt32.v", "Prims.op_Addition", "FStar.Seq.Base.seq", "LowStar.Monotonic.Buffer.__proj__Buffer__item__idx", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "FStar.Seq.Properties.lseq", "FStar.HyperStack.ST.op_Bang", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "Prims.b2t", "Prims.op_GreaterThan", "Prims.op_LessThanOrEqual", "LowStar.Monotonic.Buffer.length", "Prims.eq2", "LowStar.Monotonic.Buffer.as_seq" ]
[]
false
true
false
false
false
let read_sub_buffer (#a: Type0) (#rrel #rel: srel a) (b: mbuffer a rrel rel) (idx len: U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) =
let open HST in let s = !(Buffer?.content b) in let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in Seq.slice s (U32.v idx) (U32.v idx + U32.v len)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.malloca_of_list
val malloca_of_list (#a:Type0) (#rrel:srel a) (init: list a) :HST.StackInline (lmbuffer a rrel rrel (normalize_term (List.Tot.length init))) (requires (fun _ -> alloca_of_list_pre init)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.seq_of_list init) /\ frameOf b == HS.get_tip h0))
val malloca_of_list (#a:Type0) (#rrel:srel a) (init: list a) :HST.StackInline (lmbuffer a rrel rrel (normalize_term (List.Tot.length init))) (requires (fun _ -> alloca_of_list_pre init)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.seq_of_list init) /\ frameOf b == HS.get_tip h0))
let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 1531, "start_col": 0, "start_line": 1524 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
init: Prims.list a -> FStar.HyperStack.ST.StackInline (LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.Pervasives.normalize_term (FStar.List.Tot.Base.length init)))
FStar.HyperStack.ST.StackInline
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "Prims.list", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.Pervasives.normalize_term", "Prims.nat", "FStar.List.Tot.Base.length", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.salloc", "FStar.HyperStack.ST.mstackref", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive", "FStar.Seq.Base.seq", "Prims.eq2", "FStar.Seq.Base.length", "FStar.Seq.Base.seq_of_list", "FStar.UInt32.uint_to_t" ]
[]
false
true
false
false
false
let malloca_of_list #a #rrel init =
let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mmalloc_drgn
val mmalloc_drgn (#a:Type0) (#rrel:srel a) (d:HST.drgn) (init:a) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ region_lifetime_buf b}) (requires fun h -> alloc_drgn_pre h d len) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init))
val mmalloc_drgn (#a:Type0) (#rrel:srel a) (d:HST.drgn) (init:a) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ region_lifetime_buf b}) (requires fun h -> alloc_drgn_pre h d len) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init))
let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 1547, "start_col": 0, "start_line": 1542 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d: FStar.HyperStack.ST.drgn -> init: a -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) { LowStar.Monotonic.Buffer.frameOf b == FStar.HyperStack.ST.rid_of_drgn d /\ LowStar.Monotonic.Buffer.region_lifetime_buf b })
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.HyperStack.ST.drgn", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "Prims.l_and", "Prims.eq2", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "FStar.HyperStack.ST.rid_of_drgn", "LowStar.Monotonic.Buffer.region_lifetime_buf", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.ralloc_drgn", "FStar.Seq.Base.create", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive" ]
[]
false
true
false
false
false
let mmalloc_drgn #a #rrel d init len =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mgcmalloc_of_list
val mgcmalloc_of_list (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:list a) :HST.ST (b:lmbuffer a rrel rrel (normalize_term (List.Tot.length init)){frameOf b == r /\ recallable b}) (requires (fun _ -> gcmalloc_of_list_pre r init)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.seq_of_list init)))
val mgcmalloc_of_list (#a:Type0) (#rrel:srel a) (r:HS.rid) (init:list a) :HST.ST (b:lmbuffer a rrel rrel (normalize_term (List.Tot.length init)){frameOf b == r /\ recallable b}) (requires (fun _ -> gcmalloc_of_list_pre r init)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.seq_of_list init)))
let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len)
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 1540, "start_col": 0, "start_line": 1533 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.Monotonic.HyperHeap.rid -> init: Prims.list a -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.Pervasives.normalize_term (FStar.List.Tot.Base.length init)) {LowStar.Monotonic.Buffer.frameOf b == r /\ LowStar.Monotonic.Buffer.recallable b})
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.Monotonic.HyperHeap.rid", "Prims.list", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.Pervasives.normalize_term", "Prims.nat", "FStar.List.Tot.Base.length", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.recallable", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.ralloc", "FStar.HyperStack.ST.mref", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive", "FStar.Seq.Base.seq", "FStar.Seq.Base.length", "FStar.Seq.Base.seq_of_list", "FStar.UInt32.uint_to_t" ]
[]
false
true
false
false
false
let mgcmalloc_of_list #a #rrel r init =
let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.tu
val tu : FStar.Relational.Relational.rel Prims.unit Prims.unit
let tu = twice ()
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 28, "start_col": 0, "start_line": 28 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
FStar.Relational.Relational.rel Prims.unit Prims.unit
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.twice", "Prims.unit" ]
[]
false
false
false
true
false
let tu =
twice ()
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.twice
val twice : x: _ -> FStar.Relational.Relational.rel _ _
let twice x = R x x
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 19, "end_line": 27, "start_col": 0, "start_line": 27 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p}
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: _ -> FStar.Relational.Relational.rel _ _
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.R", "FStar.Relational.Relational.rel" ]
[]
false
false
false
true
false
let twice x =
R x x
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map2Teq
val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c)
val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c)
let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 70, "end_line": 35, "start_col": 0, "start_line": 35 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: a -> _: b -> c) -> _: FStar.Relational.Relational.double a -> _: FStar.Relational.Relational.double b -> FStar.Relational.Relational.double c
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.Relational.Relational.double", "FStar.Pervasives.Native.Mktuple2", "FStar.Relational.Relational.rel", "FStar.Relational.Relational.R" ]
[]
false
false
false
false
false
let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) =
R (f x1 y1) (f x2 y2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mmalloc_and_blit
val mmalloc_and_blit (#a:Type0) (#rrel:srel a) (r:HS.rid) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ freeable b}) (requires fun h0 -> malloc_pre r len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
val mmalloc_and_blit (#a:Type0) (#rrel:srel a) (r:HS.rid) (#rrel1 #rel1:srel a) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == r /\ freeable b}) (requires fun h0 -> malloc_pre r len /\ live h0 src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 63, "end_line": 1508, "start_col": 0, "start_line": 1507 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: FStar.Monotonic.HyperHeap.rid -> src: LowStar.Monotonic.Buffer.mbuffer a rrel1 rel1 -> id_src: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) {LowStar.Monotonic.Buffer.frameOf b == r /\ LowStar.Monotonic.Buffer.freeable b})
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.alloc_heap_common", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "FStar.Seq.Base.seq", "Prims.eq2", "Prims.int", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.UInt.size", "FStar.Seq.Base.length", "LowStar.Monotonic.Buffer.read_sub_buffer", "Prims.l_and", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.freeable" ]
[]
false
true
false
false
false
let mmalloc_and_blit #_ #_ r #_ #_ src id_src len =
alloc_heap_common r len (read_sub_buffer src id_src len) true
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.fill
val fill (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) == Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) == Seq.slice (as_seq h0 b) (U32.v len) (length b) ))
val fill (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) == Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) == Seq.slice (as_seq h0 b) (U32.v len) (length b) ))
let fill #t #rrel #rel b z len = fill' b z len
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 1648, "start_col": 0, "start_line": 1648 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer t rrel rel -> z: t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.fill'", "Prims.unit" ]
[]
false
true
false
false
false
let fill #t #rrel #rel b z len =
fill' b z len
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.cloc_cls
val cloc_cls: MG.cls abuffer
val cloc_cls: MG.cls abuffer
let cloc_cls = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.cls abuffer) cls
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 1656, "start_col": 0, "start_line": 1654 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options let fill #t #rrel #rel b z len = fill' b z len let abuffer' = ubuffer' let coerce (t2: Type) (#t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1)) = x1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
FStar.ModifiesGen.cls LowStar.Monotonic.Buffer.abuffer
Prims.Tot
[ "total" ]
[]
[ "LowStar.Monotonic.Buffer.coerce", "FStar.ModifiesGen.cls", "LowStar.Monotonic.Buffer.abuffer", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2" ]
[]
false
false
false
true
false
let cloc_cls =
assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.cls abuffer) cls
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map1T
val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b)
val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b)
let rel_map1T f (R x1 x2) = R (f x1) (f x2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 32, "start_col": 0, "start_line": 32 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: 'a -> 'b) -> _: FStar.Relational.Relational.double 'a -> FStar.Relational.Relational.double 'b
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Relational.Relational.R" ]
[]
false
false
false
true
false
let rel_map1T f (R x1 x2) =
R (f x1) (f x2)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mmalloc_drgn_mm
val mmalloc_drgn_mm (#a:Type0) (#rrel:srel a) (d:HST.drgn) (init:a) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ freeable b}) (requires fun h -> alloc_drgn_pre h d len) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init))
val mmalloc_drgn_mm (#a:Type0) (#rrel:srel a) (d:HST.drgn) (init:a) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ freeable b}) (requires fun h -> alloc_drgn_pre h d len) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.create (U32.v len) init))
let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 1554, "start_col": 0, "start_line": 1549 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d: FStar.HyperStack.ST.drgn -> init: a -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) { LowStar.Monotonic.Buffer.frameOf b == FStar.HyperStack.ST.rid_of_drgn d /\ LowStar.Monotonic.Buffer.freeable b })
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.HyperStack.ST.drgn", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "Prims.l_and", "Prims.eq2", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "FStar.HyperStack.ST.rid_of_drgn", "LowStar.Monotonic.Buffer.freeable", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.ralloc_drgn_mm", "FStar.Seq.Base.create", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive" ]
[]
false
true
false
false
false
let mmalloc_drgn_mm #a #rrel d init len =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map2G
val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c)
val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c)
let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 41, "start_col": 0, "start_line": 41 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: 'a -> _: 'b -> Prims.GTot 'c) -> _: FStar.Relational.Relational.double 'a -> _: FStar.Relational.Relational.double 'b -> Prims.GTot (FStar.Relational.Relational.double 'c)
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Pervasives.Native.Mktuple2", "FStar.Relational.Relational.rel", "FStar.Relational.Relational.R" ]
[]
false
false
false
false
false
let rel_map2G f (R x1 x2) (R y1 y2) =
R (f x1 y1) (f x2 y2)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.op_Hat_Plus
val op_Hat_Plus : _: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
let op_Hat_Plus = rel_map2T (fun x y -> x + y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 46, "end_line": 50, "start_col": 0, "start_line": 50 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.int", "Prims.op_Addition" ]
[]
false
false
false
true
false
let op_Hat_Plus =
rel_map2T (fun x y -> x + y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map3T
val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd)
val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd)
let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 75, "end_line": 44, "start_col": 0, "start_line": 44 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: 'a -> _: 'b -> _: 'c -> 'd) -> _: FStar.Relational.Relational.double 'a -> _: FStar.Relational.Relational.double 'b -> _: FStar.Relational.Relational.double 'c -> FStar.Relational.Relational.double 'd
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Pervasives.Native.Mktuple3", "FStar.Relational.Relational.rel", "FStar.Relational.Relational.R" ]
[]
false
false
false
true
false
let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) =
R (f x1 y1 z1) (f x2 y2 z2)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map2T
val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c)
val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c)
let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 38, "start_col": 0, "start_line": 38 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: 'a -> _: 'b -> 'c) -> _: FStar.Relational.Relational.double 'a -> _: FStar.Relational.Relational.double 'b -> FStar.Relational.Relational.double 'c
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Pervasives.Native.Mktuple2", "FStar.Relational.Relational.rel", "FStar.Relational.Relational.R" ]
[]
false
false
false
true
false
let rel_map2T f (R x1 x2) (R y1 y2) =
R (f x1 y1) (f x2 y2)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.rel_map3G
val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd)
val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd)
let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 75, "end_line": 47, "start_col": 0, "start_line": 47 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: 'a -> _: 'b -> _: 'c -> Prims.GTot 'd) -> _: FStar.Relational.Relational.double 'a -> _: FStar.Relational.Relational.double 'b -> _: FStar.Relational.Relational.double 'c -> Prims.GTot (FStar.Relational.Relational.double 'd)
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Pervasives.Native.Mktuple3", "FStar.Relational.Relational.rel", "FStar.Relational.Relational.R" ]
[]
false
false
false
false
false
let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) =
R (f x1 y1 z1) (f x2 y2 z2)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.op_Hat_Star
val op_Hat_Star : _: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 56, "end_line": 52, "start_col": 0, "start_line": 52 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.int", "Prims.op_Multiply" ]
[]
false
false
false
true
false
let op_Hat_Star =
rel_map2T (fun x y -> op_Multiply x y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.op_Hat_Minus
val op_Hat_Minus : _: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
let op_Hat_Minus = rel_map2T (fun x y -> x - y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 51, "start_col": 0, "start_line": 51 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.int -> FStar.Relational.Relational.double Prims.int
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.int", "Prims.op_Subtraction" ]
[]
false
false
false
true
false
let op_Hat_Minus =
rel_map2T (fun x y -> x - y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.op_Hat_Slash
val op_Hat_Slash : _: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.nonzero -> FStar.Relational.Relational.double Prims.int
let op_Hat_Slash = rel_map2T (fun x y -> x / y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 53, "start_col": 0, "start_line": 53 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.int -> _: FStar.Relational.Relational.double Prims.nonzero -> FStar.Relational.Relational.double Prims.int
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.int", "Prims.nonzero", "Prims.op_Division" ]
[]
false
false
false
true
false
let op_Hat_Slash =
rel_map2T (fun x y -> x / y)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.cloc_of_loc
val cloc_of_loc (l: loc) : Tot (MG.loc cloc_cls)
val cloc_of_loc (l: loc) : Tot (MG.loc cloc_cls)
let cloc_of_loc l = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.loc cloc_cls) l
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 1660, "start_col": 0, "start_line": 1658 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options let fill #t #rrel #rel b z len = fill' b z len let abuffer' = ubuffer' let coerce (t2: Type) (#t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1)) = x1 let cloc_cls = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.cls abuffer) cls
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: LowStar.Monotonic.Buffer.loc -> FStar.ModifiesGen.loc LowStar.Monotonic.Buffer.cloc_cls
Prims.Tot
[ "total" ]
[]
[ "LowStar.Monotonic.Buffer.loc", "LowStar.Monotonic.Buffer.coerce", "FStar.ModifiesGen.loc", "LowStar.Monotonic.Buffer.abuffer", "LowStar.Monotonic.Buffer.cloc_cls", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "FStar.ModifiesGen.cls", "LowStar.Monotonic.Buffer.ubuffer" ]
[]
false
false
false
true
false
let cloc_of_loc l =
assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.loc cloc_cls) l
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.mmalloc_drgn_and_blit
val mmalloc_drgn_and_blit (#a:Type0) (#rrel:srel a) (#rrel1 #rel1:srel a) (d:HST.drgn) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ region_lifetime_buf b}) (requires fun h -> alloc_drgn_pre h d len /\ live h src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
val mmalloc_drgn_and_blit (#a:Type0) (#rrel:srel a) (#rrel1 #rel1:srel a) (d:HST.drgn) (src:mbuffer a rrel1 rel1) (id_src:U32.t) (len:U32.t) : HST.ST (b:lmbuffer a rrel rrel (U32.v len){frameOf b == HST.rid_of_drgn d /\ region_lifetime_buf b}) (requires fun h -> alloc_drgn_pre h d len /\ live h src /\ U32.v id_src + U32.v len <= length src) (ensures fun h0 b h1 -> alloc_post_mem_common b h0 h1 (Seq.slice (as_seq h0 src) (U32.v id_src) (U32.v id_src + U32.v len)))
let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 1561, "start_col": 0, "start_line": 1556 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
d: FStar.HyperStack.ST.drgn -> src: LowStar.Monotonic.Buffer.mbuffer a rrel1 rel1 -> id_src: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.ST (b: LowStar.Monotonic.Buffer.lmbuffer a rrel rrel (FStar.UInt32.v len) { LowStar.Monotonic.Buffer.frameOf b == FStar.HyperStack.ST.rid_of_drgn d /\ LowStar.Monotonic.Buffer.region_lifetime_buf b })
FStar.HyperStack.ST.ST
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "FStar.HyperStack.ST.drgn", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.Buffer", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.lmbuffer", "FStar.UInt32.v", "Prims.l_and", "Prims.eq2", "FStar.Monotonic.HyperHeap.rid", "LowStar.Monotonic.Buffer.frameOf", "FStar.HyperStack.ST.rid_of_drgn", "LowStar.Monotonic.Buffer.region_lifetime_buf", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.HyperStack.ST.ralloc_drgn", "LowStar.Monotonic.Buffer.read_sub_buffer", "FStar.Seq.Base.seq", "Prims.unit", "LowStar.Monotonic.Buffer.lemma_seq_sub_compatilibity_is_reflexive" ]
[]
false
true
false
false
false
let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len =
lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content:HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.cons_rel
val cons_rel : _: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel (Prims.list _) (Prims.list _) -> FStar.Relational.Relational.rel (Prims.list _) (Prims.list _)
let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 58, "start_col": 0, "start_line": 58 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel (Prims.list _) (Prims.list _) -> FStar.Relational.Relational.rel (Prims.list _) (Prims.list _)
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel", "Prims.list", "FStar.Pervasives.Native.Mktuple2", "FStar.Relational.Relational.R", "Prims.Cons" ]
[]
false
false
false
true
false
let cons_rel (R x y) (R xs ys) =
R (x :: xs) (y :: ys)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.pair_rel
val pair_rel : _: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> FStar.Relational.Relational.rel (_ * _) (_ * _)
let pair_rel (R a b) (R c d) = R (a,c) (b,d)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 60, "start_col": 0, "start_line": 60 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> FStar.Relational.Relational.rel (_ * _) (_ * _)
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel", "FStar.Pervasives.Native.Mktuple2", "FStar.Relational.Relational.R", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
true
false
let pair_rel (R a b) (R c d) =
R (a, c) (b, d)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.triple_rel
val triple_rel : _: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> FStar.Relational.Relational.rel ((_ * _) * _) ((_ * _) * _)
let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 61, "start_col": 0, "start_line": 61 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> _: FStar.Relational.Relational.rel _ _ -> FStar.Relational.Relational.rel ((_ * _) * _) ((_ * _) * _)
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel", "FStar.Pervasives.Native.Mktuple3", "FStar.Relational.Relational.R", "FStar.Pervasives.Native.tuple3" ]
[]
false
false
false
true
false
let triple_rel (R a b) (R c d) (R e f) =
R (a, c, e) (b, d, f)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.fst_rel
val fst_rel : _: FStar.Relational.Relational.double (_ * _) -> FStar.Relational.Relational.double _
let fst_rel = rel_map1T fst
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 62, "start_col": 0, "start_line": 62 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double (_ * _) -> FStar.Relational.Relational.double _
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map1T", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.fst", "FStar.Relational.Relational.double" ]
[]
false
false
false
true
false
let fst_rel =
rel_map1T fst
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.snd_rel
val snd_rel : _: FStar.Relational.Relational.double (_ * _) -> FStar.Relational.Relational.double _
let snd_rel = rel_map1T snd
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 27, "end_line": 63, "start_col": 0, "start_line": 63 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double (_ * _) -> FStar.Relational.Relational.double _
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map1T", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.snd", "FStar.Relational.Relational.double" ]
[]
false
false
false
true
false
let snd_rel =
rel_map1T snd
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.or_rel
val or_rel : _: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
let or_rel = rel_map2T (fun x y -> x || y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 42, "end_line": 67, "start_col": 0, "start_line": 67 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.bool", "Prims.op_BarBar" ]
[]
false
false
false
true
false
let or_rel =
rel_map2T (fun x y -> x || y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.and_rel
val and_rel : _: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
let and_rel = rel_map2T (fun x y -> x && y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 66, "start_col": 0, "start_line": 66 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2T", "Prims.bool", "Prims.op_AmpAmp" ]
[]
false
false
false
true
false
let and_rel =
rel_map2T (fun x y -> x && y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.eq_irel
val eq_irel : x: FStar.Relational.Relational.rel t t -> Prims.bool
let eq_irel (#t:eqtype) (x:(rel t t)) = match x with | R a b -> a = b
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 74, "start_col": 0, "start_line": 73 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *) let and_rel = rel_map2T (fun x y -> x && y) let or_rel = rel_map2T (fun x y -> x || y) let eq_rel = rel_map2Teq (fun (x y:bool) -> x = y) (* Some convenient functions combining left and right side (for specification only) *) let and_irel (R a b) = a && b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.Relational.Relational.rel t t -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.Relational.Relational.rel", "Prims.op_Equality", "Prims.bool" ]
[]
false
false
false
false
false
let eq_irel (#t: eqtype) (x: (rel t t)) =
match x with | R a b -> a = b
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.and_irel
val and_irel : _: FStar.Relational.Relational.rel Prims.bool Prims.bool -> Prims.bool
let and_irel (R a b) = a && b
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 29, "end_line": 71, "start_col": 0, "start_line": 71 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *) let and_rel = rel_map2T (fun x y -> x && y) let or_rel = rel_map2T (fun x y -> x || y) let eq_rel = rel_map2Teq (fun (x y:bool) -> x = y)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.rel Prims.bool Prims.bool -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel", "Prims.bool", "Prims.op_AmpAmp" ]
[]
false
false
false
true
false
let and_irel (R a b) =
a && b
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.eq_rel
val eq_rel : _: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
let eq_rel = rel_map2Teq (fun (x y:bool) -> x = y)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 50, "end_line": 68, "start_col": 0, "start_line": 68 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *) let and_rel = rel_map2T (fun x y -> x && y)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.double Prims.bool -> _: FStar.Relational.Relational.double Prims.bool -> FStar.Relational.Relational.double Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel_map2Teq", "Prims.bool", "Prims.op_Equality" ]
[]
false
false
false
true
false
let eq_rel =
rel_map2Teq (fun (x: bool) (y: bool) -> x = y)
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.or_irel
val or_irel : _: FStar.Relational.Relational.rel Prims.bool Prims.bool -> Prims.bool
let or_irel (R a b) = a || b
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 28, "end_line": 72, "start_col": 0, "start_line": 72 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *) let and_rel = rel_map2T (fun x y -> x && y) let or_rel = rel_map2T (fun x y -> x || y) let eq_rel = rel_map2Teq (fun (x y:bool) -> x = y) (* Some convenient functions combining left and right side (for specification only) *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: FStar.Relational.Relational.rel Prims.bool Prims.bool -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.rel", "Prims.bool", "Prims.op_BarBar" ]
[]
false
false
false
true
false
let or_irel (R a b) =
a || b
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.coerce
val coerce (t2 #t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1))
val coerce (t2 #t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1))
let coerce (t2: Type) (#t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1)) = x1
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 108, "end_line": 1652, "start_col": 0, "start_line": 1652 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options let fill #t #rrel #rel b z len = fill' b z len let abuffer' = ubuffer'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t2: Type -> x1: t1 -> Prims.Pure t2
Prims.Pure
[]
[]
[ "Prims.eq2" ]
[]
false
false
false
false
false
let coerce (t2 #t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1)) =
x1
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.sel_rel1
val sel_rel1 : h: FStar.Relational.Relational.double FStar.Monotonic.Heap.heap -> r: FStar.Heap.ref a -> Prims.GTot (FStar.Relational.Relational.double a)
let sel_rel1 (#a:Type) (h:double heap) (r:ref a) = rel_map2G sel h (twice r)
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 77, "end_line": 77, "start_col": 0, "start_line": 77 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *) val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a)) let tl_rel #a (R (_::xs) (_::ys)) = R xs ys let cons_rel (R x y) (R xs ys) = R (x::xs) (y::ys) (* Some convenient tuple functions *) let pair_rel (R a b) (R c d) = R (a,c) (b,d) let triple_rel (R a b) (R c d) (R e f) = R (a,c,e) (b,d,f) let fst_rel = rel_map1T fst let snd_rel = rel_map1T snd (* Some convenient boolean functions *) let and_rel = rel_map2T (fun x y -> x && y) let or_rel = rel_map2T (fun x y -> x || y) let eq_rel = rel_map2Teq (fun (x y:bool) -> x = y) (* Some convenient functions combining left and right side (for specification only) *) let and_irel (R a b) = a && b let or_irel (R a b) = a || b let eq_irel (#t:eqtype) (x:(rel t t)) = match x with | R a b -> a = b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
h: FStar.Relational.Relational.double FStar.Monotonic.Heap.heap -> r: FStar.Heap.ref a -> Prims.GTot (FStar.Relational.Relational.double a)
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Relational.Relational.double", "FStar.Monotonic.Heap.heap", "FStar.Heap.ref", "FStar.Relational.Relational.rel_map2G", "FStar.Monotonic.Heap.mref", "FStar.Heap.trivial_preorder", "FStar.Monotonic.Heap.sel", "FStar.Relational.Relational.twice" ]
[]
false
false
false
false
false
let sel_rel1 (#a: Type) (h: double heap) (r: ref a) =
rel_map2G sel h (twice r)
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.loc_of_cloc
val loc_of_cloc (l: MG.loc cloc_cls) : Tot loc
val loc_of_cloc (l: MG.loc cloc_cls) : Tot loc
let loc_of_cloc l = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce loc l
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 14, "end_line": 1664, "start_col": 0, "start_line": 1662 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> () #push-options "--z3rlimit 128 --max_fuel 0 --max_ifuel 1 --initial_ifuel 1 --z3cliopt smt.qi.EAGER_THRESHOLD=4" let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end #pop-options let fill #t #rrel #rel b z len = fill' b z len let abuffer' = ubuffer' let coerce (t2: Type) (#t1: Type) (x1: t1) : Pure t2 (requires (t1 == t2)) (ensures (fun y -> y == x1)) = x1 let cloc_cls = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.cls abuffer) cls let cloc_of_loc l = assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce (MG.loc cloc_cls) l
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: FStar.ModifiesGen.loc LowStar.Monotonic.Buffer.cloc_cls -> LowStar.Monotonic.Buffer.loc
Prims.Tot
[ "total" ]
[]
[ "FStar.ModifiesGen.loc", "LowStar.Monotonic.Buffer.abuffer", "LowStar.Monotonic.Buffer.cloc_cls", "LowStar.Monotonic.Buffer.coerce", "LowStar.Monotonic.Buffer.loc", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "FStar.ModifiesGen.cls", "LowStar.Monotonic.Buffer.ubuffer" ]
[]
false
false
false
true
false
let loc_of_cloc l =
assert_norm (MG.cls abuffer == MG.cls ubuffer); coerce loc l
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.modifies_loc_buffer_from_to_intro'
val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h'))
val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h'))
let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) )
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1188, "start_col": 0, "start_line": 1132 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 16, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> from: FStar.UInt32.t -> to: FStar.UInt32.t -> l: LowStar.Monotonic.Buffer.loc -> h: FStar.Monotonic.HyperStack.mem -> h': FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires (let s = LowStar.Monotonic.Buffer.as_seq h b in let s' = LowStar.Monotonic.Buffer.as_seq h' b in Prims.op_Negation (LowStar.Monotonic.Buffer.g_is_null b) /\ LowStar.Monotonic.Buffer.live h b /\ LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_union l (LowStar.Monotonic.Buffer.loc_buffer b)) h h' /\ FStar.UInt32.v from <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b /\ FStar.Seq.Base.equal (FStar.Seq.Base.slice s 0 (FStar.UInt32.v from)) (FStar.Seq.Base.slice s' 0 (FStar.UInt32.v from)) /\ FStar.Seq.Base.equal (FStar.Seq.Base.slice s (FStar.UInt32.v to) (LowStar.Monotonic.Buffer.length b)) (FStar.Seq.Base.slice s' (FStar.UInt32.v to) (LowStar.Monotonic.Buffer.length b)))) (ensures LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_union l (LowStar.Monotonic.Buffer.loc_buffer_from_to b from to)) h h')
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "LowStar.Monotonic.Buffer.loc", "FStar.Monotonic.HyperStack.mem", "FStar.ModifiesGen.modifies_strengthen", "LowStar.Monotonic.Buffer.ubuffer", "LowStar.Monotonic.Buffer.cls", "LowStar.Monotonic.Buffer.ubuffer_of_buffer_from_to", "FStar.Preorder.preorder", "FStar.Monotonic.HyperStack.mreference", "Prims.unit", "Prims.l_and", "Prims.eq2", "FStar.Monotonic.HyperHeap.rid", "FStar.Monotonic.HyperStack.frameOf", "Prims.int", "Prims.l_or", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "Prims.op_GreaterThan", "FStar.Monotonic.HyperStack.as_addr", "FStar.Monotonic.HyperStack.contains", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "LowStar.Monotonic.Buffer.ubuffer_preserved_intro", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.__proj__Buffer__item__max_length", "LowStar.Monotonic.Buffer.srel_to_lsrel", "LowStar.Monotonic.Buffer.__proj__Buffer__item__content", "Prims.nat", "Prims.bool", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Classical.forall_intro", "Prims.l_imp", "Prims.op_LessThan", "FStar.Seq.Base.index", "FStar.Classical.move_requires", "Prims.op_BarBar", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "LowStar.Monotonic.Buffer.length", "FStar.ModifiesGen.modifies_aloc_elim", "LowStar.Monotonic.Buffer.loc_union", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Monotonic.Buffer.loc_disjoint", "FStar.ModifiesGen.loc_disjoint_aloc_intro", "FStar.Seq.Base.slice", "Prims.op_Subtraction", "LowStar.Monotonic.Buffer.loc_disjoint_includes", "FStar.ModifiesGen.loc_of_aloc", "FStar.ModifiesGen.loc_includes_aloc", "FStar.ModifiesGen.loc", "LowStar.Monotonic.Buffer.as_seq", "FStar.Ghost.hide", "LowStar.Monotonic.Buffer.ubuffer'", "LowStar.Monotonic.Buffer.Mkubuffer_", "FStar.Seq.Base.seq", "LowStar.Monotonic.Buffer.frameOf", "LowStar.Monotonic.Buffer.as_addr", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.ubuffer_of_buffer", "FStar.UInt.uint_t", "LowStar.Monotonic.Buffer.__proj__Buffer__item__idx", "LowStar.Monotonic.Buffer.lemma_equal_instances_implies_equal_types", "FStar.Pervasives.coerce_eq", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_mm", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_preorders", "LowStar.Monotonic.Buffer.modifies_loc_includes", "LowStar.Monotonic.Buffer.loc_addresses", "FStar.Set.singleton" ]
[]
false
false
true
false
false
let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' =
let r0 = frameOf b in let a0 = as_addr b in let bb:ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0:Seq.seq a = as_seq h b in let _s1:Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let { b_max_length = ml ; b_offset = xoff ; b_length = xlen ; b_is_mm = is_mm } = Ghost.reveal x in let { b_max_length = _ ; b_offset = b'off ; b_length = b'len } = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi:ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then (MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h') else if xi < from_ then (assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) ) else (assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_))) in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh')))
false
FStar.Relational.Relational.fst
FStar.Relational.Relational.tl_rel
val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a))
val tl_rel: #a:Type -> l:double (list a){Cons? (R?.l l) /\ Cons? (R?.r l)}-> Tot (double (list a))
let tl_rel #a (R (_::xs) (_::ys)) = R xs ys
{ "file_name": "ulib/legacy/FStar.Relational.Relational.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 43, "end_line": 57, "start_col": 0, "start_line": 57 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module FStar.Relational.Relational open FStar.Heap (* Relational Type constructor (Equivalent to pairs) *) type rel (a:Type) (b:Type) : Type = | R : l:a -> r:b -> rel a b (* Some frequently used abbreviations *) type double (t:Type) = rel t t type eq (t:Type) = p:(double t){R?.l p == R?.r p} let twice x = R x x let tu = twice () (* functions to lift normal functions to Relational functions *) val rel_map1T : ('a -> Tot 'b) -> (double 'a) -> Tot (double 'b) let rel_map1T f (R x1 x2) = R (f x1) (f x2) val rel_map2Teq : #a:eqtype -> #b:eqtype -> #c:Type -> (a -> b -> Tot c) -> (double a) -> (double b) -> Tot (double c) let rel_map2Teq #a #b #c f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2T : ('a -> 'b -> Tot 'c) -> (double 'a) -> (double 'b) -> Tot (double 'c) let rel_map2T f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map2G : ('a -> 'b -> GTot 'c) -> (double 'a) -> (double 'b) -> GTot (double 'c) let rel_map2G f (R x1 x2) (R y1 y2) = R (f x1 y1) (f x2 y2) val rel_map3T : ('a -> 'b -> 'c -> Tot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> Tot (double 'd) let rel_map3T f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) val rel_map3G : ('a -> 'b -> 'c -> GTot 'd) -> (double 'a) -> (double 'b) -> (double 'c) -> GTot (double 'd) let rel_map3G f (R x1 x2) (R y1 y2) (R z1 z2) = R (f x1 y1 z1) (f x2 y2 z2) (* Some convenient arithmetic functions *) let op_Hat_Plus = rel_map2T (fun x y -> x + y) let op_Hat_Minus = rel_map2T (fun x y -> x - y) let op_Hat_Star = rel_map2T (fun x y -> op_Multiply x y) let op_Hat_Slash = rel_map2T (fun x y -> x / y) (* Some convenient list functions *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Heap.fst.checked" ], "interface_file": false, "source_file": "FStar.Relational.Relational.fst" }
[ { "abbrev": false, "full_module": "FStar.Heap", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Relational", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: FStar.Relational.Relational.double (Prims.list a) {Cons? (R?.l l) /\ Cons? (R?.r l)} -> FStar.Relational.Relational.double (Prims.list a)
Prims.Tot
[ "total" ]
[]
[ "FStar.Relational.Relational.double", "Prims.list", "Prims.l_and", "Prims.b2t", "Prims.uu___is_Cons", "FStar.Relational.Relational.__proj__R__item__l", "FStar.Relational.Relational.__proj__R__item__r", "FStar.Relational.Relational.R" ]
[]
false
false
false
false
false
let tl_rel #a (R (_ :: xs) (_ :: ys)) =
R xs ys
false
SteelLoops.fst
SteelLoops.sum_to_n_for_2
val sum_to_n_for_2 (r: ref UInt32.t) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun h -> sel r h == 0ul) (ensures fun h0 _ h1 -> sel r h1 == 10ul)
val sum_to_n_for_2 (r: ref UInt32.t) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun h -> sel r h == 0ul) (ensures fun h0 _ h1 -> sel r h1 == 10ul)
let sum_to_n_for_2 (r:ref UInt32.t) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun h -> sel r h == 0ul) (ensures fun h0 _ h1 -> sel r h1 == 10ul) = for_loop_full 0sz 10sz (fun _ -> vptr r) (fun i v -> v == UInt32.uint_to_t i) (fun _ -> let x = read r in write r (x `FStar.UInt32.add_mod` 1ul))
{ "file_name": "share/steel/tests/krml/SteelLoops.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 71, "end_line": 24, "start_col": 0, "start_line": 15 }
module SteelLoops open Steel.Effect.Atomic open Steel.Effect open Steel.Reference open Steel.Loops let sum_to_n_for (r:ref UInt32.t) : SteelT unit (vptr r) (fun _ -> vptr r) = for_loop 0sz 10sz (fun _ -> vptr r) (fun _ -> let x = read r in write r (x `FStar.UInt32.add_mod` 1ul))
{ "checked_file": "/", "dependencies": [ "Steel.Reference.fsti.checked", "Steel.Loops.fsti.checked", "Steel.Effect.Atomic.fsti.checked", "Steel.Effect.fsti.checked", "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.SizeT.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Int32.fsti.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "SteelLoops.fst" }
[ { "abbrev": false, "full_module": "Steel.Loops", "short_module": null }, { "abbrev": false, "full_module": "Steel.Reference", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Atomic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
r: Steel.Reference.ref FStar.UInt32.t -> Steel.Effect.Steel Prims.unit
Steel.Effect.Steel
[]
[]
[ "Steel.Reference.ref", "FStar.UInt32.t", "Steel.Loops.for_loop_full", "FStar.SizeT.__uint_to_t", "Steel.Loops.nat_at_most", "Steel.Reference.vptr", "Steel.Effect.Common.vprop", "Steel.Effect.Common.t_of", "Prims.eq2", "FStar.UInt32.uint_to_t", "Prims.prop", "Steel.Loops.u32_between", "Steel.Reference.write", "FStar.UInt32.add_mod", "FStar.UInt32.__uint_to_t", "Prims.unit", "Steel.Reference.read", "Steel.Effect.Common.rmem", "Steel.Reference.sel" ]
[]
false
true
false
false
false
let sum_to_n_for_2 (r: ref UInt32.t) : Steel unit (vptr r) (fun _ -> vptr r) (requires fun h -> sel r h == 0ul) (ensures fun h0 _ h1 -> sel r h1 == 10ul) =
for_loop_full 0sz 10sz (fun _ -> vptr r) (fun i v -> v == UInt32.uint_to_t i) (fun _ -> let x = read r in write r (x `FStar.UInt32.add_mod` 1ul))
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.g_upd_seq_as_seq
val g_upd_seq_as_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (s:Seq.lseq a (length b)) (h:HS.mem{live h b}) : Lemma (let h' = g_upd_seq b s h in (Seq.length s > 0 ==> not (g_is_null b)) /\ modifies (loc_buffer b) h h' /\ live h' b /\ HST.equal_domains h h' /\ as_seq h' b == s)
val g_upd_seq_as_seq (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (s:Seq.lseq a (length b)) (h:HS.mem{live h b}) : Lemma (let h' = g_upd_seq b s h in (Seq.length s > 0 ==> not (g_is_null b)) /\ modifies (loc_buffer b) h h' /\ live h' b /\ HST.equal_domains h h' /\ as_seq h' b == s)
let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 1337, "start_col": 0, "start_line": 1327 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 48, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> s: FStar.Seq.Properties.lseq a (LowStar.Monotonic.Buffer.length b) -> h: FStar.Monotonic.HyperStack.mem{LowStar.Monotonic.Buffer.live h b} -> FStar.Pervasives.Lemma (ensures (let h' = LowStar.Monotonic.Buffer.g_upd_seq b s h in (FStar.Seq.Base.length s > 0 ==> Prims.op_Negation (LowStar.Monotonic.Buffer.g_is_null b)) /\ LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_buffer b) h h' /\ LowStar.Monotonic.Buffer.live h' b /\ FStar.HyperStack.ST.equal_domains h h' /\ LowStar.Monotonic.Buffer.as_seq h' b == s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.Seq.Properties.lseq", "LowStar.Monotonic.Buffer.length", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "LowStar.Monotonic.Buffer.g_is_null", "Prims._assert", "FStar.Seq.Base.equal", "FStar.Seq.Base.empty", "Prims.bool", "LowStar.Monotonic.Buffer.modifies_1_modifies", "Prims.unit", "LowStar.Monotonic.Buffer.s_lemma_equal_instances_implies_equal_types", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_mm", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_preorders", "LowStar.Monotonic.Buffer.as_seq", "LowStar.Monotonic.Buffer.g_upd_seq" ]
[]
false
false
true
false
false
let g_upd_seq_as_seq #a #_ #_ b s h =
let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else (assert (Seq.equal (as_seq h' b) s); Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h')
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.g_upd_modifies_strong
val g_upd_modifies_strong (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:nat{i < length b}) (v:a) (h:HS.mem{live h b}) : Lemma (modifies (loc_buffer_from_to b (U32.uint_to_t i) (U32.uint_to_t (i + 1))) h (g_upd b i v h))
val g_upd_modifies_strong (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (i:nat{i < length b}) (v:a) (h:HS.mem{live h b}) : Lemma (modifies (loc_buffer_from_to b (U32.uint_to_t i) (U32.uint_to_t (i + 1))) h (g_upd b i v h))
let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h'
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 80, "end_line": 1345, "start_col": 0, "start_line": 1339 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 48, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer a rrel rel -> i: Prims.nat{i < LowStar.Monotonic.Buffer.length b} -> v: a -> h: FStar.Monotonic.HyperStack.mem{LowStar.Monotonic.Buffer.live h b} -> FStar.Pervasives.Lemma (ensures LowStar.Monotonic.Buffer.modifies (LowStar.Monotonic.Buffer.loc_buffer_from_to b (FStar.UInt32.uint_to_t i) (FStar.UInt32.uint_to_t (i + 1))) h (LowStar.Monotonic.Buffer.g_upd b i v h))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "LowStar.Monotonic.Buffer.length", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.live", "LowStar.Monotonic.Buffer.modifies_1_from_to_modifies", "FStar.UInt32.uint_to_t", "Prims.op_Addition", "Prims.unit", "LowStar.Monotonic.Buffer.s_lemma_equal_instances_implies_equal_types", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_mm", "FStar.Monotonic.Heap.lemma_distinct_addrs_distinct_preorders", "LowStar.Monotonic.Buffer.g_upd" ]
[]
true
false
true
false
false
let g_upd_modifies_strong #_ #_ #_ b i v h =
let h' = g_upd b i v h in Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h'
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.fill'
val fill' (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z: t) (len: U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)))) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ (Seq.slice (as_seq h1 b) 0 (U32.v len)) `Seq.equal` (Seq.create (U32.v len) z) /\ (Seq.slice (as_seq h1 b) (U32.v len) (length b)) `Seq.equal` (Seq.slice (as_seq h0 b) (U32.v len) (length b))))
val fill' (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z: t) (len: U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)))) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ (Seq.slice (as_seq h1 b) 0 (U32.v len)) `Seq.equal` (Seq.create (U32.v len) z) /\ (Seq.slice (as_seq h1 b) (U32.v len) (length b)) `Seq.equal` (Seq.slice (as_seq h0 b) (U32.v len) (length b))))
let fill' (#t:Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z:t) (len:U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)) )) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ Seq.slice (as_seq h1 b) 0 (U32.v len) `Seq.equal` Seq.create (U32.v len) z /\ Seq.slice (as_seq h1 b) (U32.v len) (length b) `Seq.equal` Seq.slice (as_seq h0 b) (U32.v len) (length b) )) = let open HST in if len = 0ul then () else begin let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src)); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h //for modifies clause end
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 1645, "start_col": 0, "start_line": 1611 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len #push-options "--max_fuel 0 --initial_ifuel 1 --max_ifuel 1 --z3rlimit 64" let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [ "smt.qi.EAGER_THRESHOLD=4" ], "z3refresh": false, "z3rlimit": 128, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: LowStar.Monotonic.Buffer.mbuffer t rrel rel -> z: t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "Prims.op_Equality", "FStar.UInt32.__uint_to_t", "Prims.unit", "Prims.bool", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.g_upd_seq_as_seq", "FStar.Seq.Base.slice", "Prims._assert", "Prims.eq2", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.g_upd_seq", "FStar.Seq.Base.equal", "FStar.Seq.Properties.replace_subseq", "FStar.HyperStack.ST.get", "FStar.HyperStack.ST.op_Colon_Equals", "FStar.Seq.Base.seq", "FStar.Seq.Base.create", "FStar.HyperStack.ST.op_Bang", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Monotonic.Buffer.length", "LowStar.Monotonic.Buffer.as_seq", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_buffer" ]
[]
false
true
false
false
false
let fill' (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) (z: t) (len: U32.t) : HST.Stack unit (requires (fun h -> live h b /\ U32.v len <= length b /\ rel (as_seq h b) (Seq.replace_subseq (as_seq h b) 0 (U32.v len) (Seq.create (U32.v len) z)))) (ensures (fun h0 _ h1 -> modifies (loc_buffer b) h0 h1 /\ live h1 b /\ (Seq.slice (as_seq h1 b) 0 (U32.v len)) `Seq.equal` (Seq.create (U32.v len) z) /\ (Seq.slice (as_seq h1 b) (U32.v len) (length b)) `Seq.equal` (Seq.slice (as_seq h0 b) (U32.v len) (length b)))) =
let open HST in if len = 0ul then () else let h = get () in let Buffer max_length content idx length = b in let s_full = !content in let s = Seq.slice s_full (U32.v idx) (U32.v max_length) in let s_src = Seq.create (U32.v len) z in let s' = Seq.replace_subseq s 0 (U32.v len) s_src in let s_full' = Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v len) s_src in assert (s_full' `Seq.equal` (Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.replace_subseq (Seq.slice s_full (U32.v idx) (U32.v idx + U32.v length)) 0 (U32.v len) s_src))); content := s_full'; let h' = HST.get () in assert (s_full' `Seq.equal` (Seq.replace_subseq s_full (U32.v idx) (U32.v idx + U32.v length) (Seq.slice s' 0 (U32.v length)))); assert (h' == g_upd_seq b (Seq.slice s' 0 (U32.v length)) h); g_upd_seq_as_seq b (Seq.slice s' 0 (U32.v length)) h
false
LowStar.Monotonic.Buffer.fst
LowStar.Monotonic.Buffer.blit
val blit (#a:Type0) (#rrel1 #rrel2 #rel1 #rel2:srel a) (src:mbuffer a rrel1 rel1) (idx_src:U32.t) (dst:mbuffer a rrel2 rel2) (idx_dst:U32.t) (len:U32.t) :HST.Stack unit (requires (fun h -> live h src /\ live h dst /\ U32.v idx_src + U32.v len <= length src /\ U32.v idx_dst + U32.v len <= length dst /\ (* TODO: remove the rhs part of this disjunction once patterns on loc_buffer_from_to are introduced *) (loc_disjoint (loc_buffer_from_to src idx_src (idx_src `U32.add` len)) (loc_buffer_from_to dst idx_dst (idx_dst `U32.add` len)) \/ disjoint src dst) /\ rel2 (as_seq h dst) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len))))) (ensures (fun h _ h' -> modifies (loc_buffer dst) h h' /\ live h' dst /\ Seq.slice (as_seq h' dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) == Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) /\ Seq.slice (as_seq h' dst) 0 (U32.v idx_dst) == Seq.slice (as_seq h dst) 0 (U32.v idx_dst) /\ Seq.slice (as_seq h' dst) (U32.v idx_dst + U32.v len) (length dst) == Seq.slice (as_seq h dst) (U32.v idx_dst + U32.v len) (length dst)))
val blit (#a:Type0) (#rrel1 #rrel2 #rel1 #rel2:srel a) (src:mbuffer a rrel1 rel1) (idx_src:U32.t) (dst:mbuffer a rrel2 rel2) (idx_dst:U32.t) (len:U32.t) :HST.Stack unit (requires (fun h -> live h src /\ live h dst /\ U32.v idx_src + U32.v len <= length src /\ U32.v idx_dst + U32.v len <= length dst /\ (* TODO: remove the rhs part of this disjunction once patterns on loc_buffer_from_to are introduced *) (loc_disjoint (loc_buffer_from_to src idx_src (idx_src `U32.add` len)) (loc_buffer_from_to dst idx_dst (idx_dst `U32.add` len)) \/ disjoint src dst) /\ rel2 (as_seq h dst) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len))))) (ensures (fun h _ h' -> modifies (loc_buffer dst) h h' /\ live h' dst /\ Seq.slice (as_seq h' dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) == Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) /\ Seq.slice (as_seq h' dst) 0 (U32.v idx_dst) == Seq.slice (as_seq h dst) 0 (U32.v idx_dst) /\ Seq.slice (as_seq h' dst) (U32.v idx_dst + U32.v len) (length dst) == Seq.slice (as_seq h dst) (U32.v idx_dst + U32.v len) (length dst)))
let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len = let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); // AF: Needed to trigger the preorder relation. A bit verbose because the second sequence // has a ghost computation (U32.v (Ghost.reveal length)) assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len) ) ) ); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h //for modifies clause | _, _ -> ()
{ "file_name": "ulib/LowStar.Monotonic.Buffer.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 14, "end_line": 1608, "start_col": 0, "start_line": 1564 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module LowStar.Monotonic.Buffer module P = FStar.Preorder module G = FStar.Ghost module U32 = FStar.UInt32 module Seq = FStar.Seq module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let srel_to_lsrel (#a:Type0) (len:nat) (pre:srel a) :P.preorder (Seq.lseq a len) = pre (* * Counterpart of compatible_sub from the fsti but using sequences * * The patterns are guarded tightly, the proof of transitivity gets quite flaky otherwise * The cost is that we have to additional asserts as triggers *) let compatible_sub_preorder (#a:Type0) (len:nat) (rel:srel a) (i:nat) (j:nat{i <= j /\ j <= len}) (sub_rel:srel a) = compatible_subseq_preorder len rel i j sub_rel (* * Reflexivity of the compatibility relation *) let lemma_seq_sub_compatilibity_is_reflexive (#a:Type0) (len:nat) (rel:srel a) :Lemma (compatible_sub_preorder len rel 0 len rel) = assert (forall (s1 s2:Seq.seq a). Seq.length s1 == Seq.length s2 ==> Seq.equal (Seq.replace_subseq s1 0 (Seq.length s1) s2) s2) (* * Transitivity of the compatibility relation * * i2 and j2 are relative offsets within [i1, j1) (i.e. assuming i1 = 0) *) let lemma_seq_sub_compatibility_is_transitive (#a:Type0) (len:nat) (rel:srel a) (i1 j1:nat) (rel1:srel a) (i2 j2:nat) (rel2:srel a) :Lemma (requires (i1 <= j1 /\ j1 <= len /\ i2 <= j2 /\ j2 <= j1 - i1 /\ compatible_sub_preorder len rel i1 j1 rel1 /\ compatible_sub_preorder (j1 - i1) rel1 i2 j2 rel2)) (ensures (compatible_sub_preorder len rel (i1 + i2) (i1 + j2) rel2)) = let t1 (s1 s2:Seq.seq a) = Seq.length s1 == len /\ Seq.length s2 == len /\ rel s1 s2 in let t2 (s1 s2:Seq.seq a) = t1 s1 s2 /\ rel2 (Seq.slice s1 (i1 + i2) (i1 + j2)) (Seq.slice s2 (i1 + i2) (i1 + j2)) in let aux0 (s1 s2:Seq.seq a) :Lemma (t1 s1 s2 ==> t2 s1 s2) = Classical.arrow_to_impl #(t1 s1 s2) #(t2 s1 s2) (fun _ -> assert (rel1 (Seq.slice s1 i1 j1) (Seq.slice s2 i1 j1)); assert (rel2 (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice (Seq.slice s2 i1 j1) i2 j2)); assert (Seq.equal (Seq.slice (Seq.slice s1 i1 j1) i2 j2) (Seq.slice s1 (i1 + i2) (i1 + j2))); assert (Seq.equal (Seq.slice (Seq.slice s2 i1 j1) i2 j2) (Seq.slice s2 (i1 + i2) (i1 + j2)))) in let t1 (s s2:Seq.seq a) = Seq.length s == len /\ Seq.length s2 == j2 - i2 /\ rel2 (Seq.slice s (i1 + i2) (i1 + j2)) s2 in let t2 (s s2:Seq.seq a) = t1 s s2 /\ rel s (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2) in let aux1 (s s2:Seq.seq a) :Lemma (t1 s s2 ==> t2 s s2) = Classical.arrow_to_impl #(t1 s s2) #(t2 s s2) (fun _ -> assert (Seq.equal (Seq.slice s (i1 + i2) (i1 + j2)) (Seq.slice (Seq.slice s i1 j1) i2 j2)); assert (rel1 (Seq.slice s i1 j1) (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)); assert (rel s (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2))); assert (Seq.equal (Seq.replace_subseq s i1 j1 (Seq.replace_subseq (Seq.slice s i1 j1) i2 j2 s2)) (Seq.replace_subseq s (i1 + i2) (i1 + j2) s2))) in Classical.forall_intro_2 aux0; Classical.forall_intro_2 aux1 noeq type mbuffer (a:Type0) (rrel:srel a) (rel:srel a) :Type0 = | Null | Buffer: max_length:U32.t -> content:HST.mreference (Seq.lseq a (U32.v max_length)) (srel_to_lsrel (U32.v max_length) rrel) -> idx:U32.t -> length:Ghost.erased U32.t{U32.v idx + U32.v (Ghost.reveal length) <= U32.v max_length} -> mbuffer a rrel rel let g_is_null #_ #_ #_ b = Null? b let mnull #_ #_ #_ = Null let null_unique #_ #_ #_ _ = () let unused_in #_ #_ #_ b h = match b with | Null -> False | Buffer _ content _ _ -> content `HS.unused_in` h let buffer_compatible (#t: Type) (#rrel #rel: srel t) (b: mbuffer t rrel rel) : GTot Type0 = match b with | Null -> True | Buffer max_length content idx length -> compatible_sub_preorder (U32.v max_length) rrel (U32.v idx) (U32.v idx + U32.v length) rel //proof of compatibility let live #_ #rrel #rel h b = match b with | Null -> True | Buffer max_length content idx length -> h `HS.contains` content /\ buffer_compatible b let live_null _ _ _ _ = () let live_not_unused_in #_ #_ #_ _ _ = () let lemma_live_equal_mem_domains #_ #_ #_ _ _ _ = () let frameOf #_ #_ #_ b = if Null? b then HS.root else HS.frameOf (Buffer?.content b) let as_addr #_ #_ #_ b = if g_is_null b then 0 else HS.as_addr (Buffer?.content b) let unused_in_equiv #_ #_ #_ b h = if g_is_null b then Heap.not_addr_unused_in_nullptr (Map.sel (HS.get_hmap h) HS.root) else () let live_region_frameOf #_ #_ #_ _ _ = () let len #_ #_ #_ b = match b with | Null -> 0ul | Buffer _ _ _ len -> len let len_null a _ _ = () let as_seq #_ #_ #_ h b = match b with | Null -> Seq.empty | Buffer max_len content idx len -> Seq.slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len) let length_as_seq #_ #_ #_ _ _ = () let mbuffer_injectivity_in_first_preorder () = () let mgsub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content idx length -> Buffer max_len content (U32.add idx i) (Ghost.hide len) let live_gsub #_ #rrel #rel _ b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> let prf () : Lemma (requires (buffer_compatible b)) (ensures (buffer_compatible (mgsub sub_rel b i len))) = lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel in Classical.move_requires prf () let gsub_is_null #_ #_ #_ _ _ _ _ = () let len_gsub #_ #_ #_ _ _ _ _ = () let frameOf_gsub #_ #_ #_ _ _ _ _ = () let as_addr_gsub #_ #_ #_ _ _ _ _ = () let mgsub_inj #_ #_ #_ _ _ _ _ _ _ _ _ = () #push-options "--z3rlimit 20" let gsub_gsub #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let prf () : Lemma (requires (compatible_sub b i1 len1 sub_rel1 /\ compatible_sub (mgsub sub_rel1 b i1 len1) i2 len2 sub_rel2)) (ensures (compatible_sub b (U32.add i1 i2) len2 sub_rel2)) = lemma_seq_sub_compatibility_is_transitive (length b) rel (U32.v i1) (U32.v i1 + U32.v len1) sub_rel1 (U32.v i2) (U32.v i2 + U32.v len2) sub_rel2 in Classical.move_requires prf () #pop-options /// A buffer ``b`` is equal to its "largest" sub-buffer, at index 0 and /// length ``len b``. let gsub_zero_length #_ #_ #rel b = lemma_seq_sub_compatilibity_is_reflexive (length b) rel let as_seq_gsub #_ #_ #_ h b i len _ = match b with | Null -> () | Buffer _ content idx len0 -> Seq.slice_slice (HS.sel h content) (U32.v idx) (U32.v idx + U32.v len0) (U32.v i) (U32.v i + U32.v len) let lemma_equal_instances_implies_equal_types (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b) : Lemma (requires s1 === s2) (ensures a == b) = Seq.lemma_equal_instances_implies_equal_types () let s_lemma_equal_instances_implies_equal_types (_:unit) : Lemma (forall (a:Type) (b:Type) (s1:Seq.seq a) (s2:Seq.seq b). {:pattern (has_type s1 (Seq.seq a)); (has_type s2 (Seq.seq b)) } s1 === s2 ==> a == b) = Seq.lemma_equal_instances_implies_equal_types() let live_same_addresses_equal_types_and_preorders' (#a1 #a2: Type0) (#rrel1 #rel1: srel a1) (#rrel2 #rel2: srel a2) (b1: mbuffer a1 rrel1 rel1) (b2: mbuffer a2 rrel2 rel2) (h: HS.mem) : Lemma (requires frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ live h b1 /\ live h b2 /\ (~ (g_is_null b1 /\ g_is_null b2))) (ensures a1 == a2 /\ rrel1 == rrel2) = Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); let s1 : Seq.seq a1 = as_seq h b1 in assert (Seq.seq a1 == Seq.seq a2); let s1' : Seq.seq a2 = coerce_eq _ s1 in assert (s1 === s1'); lemma_equal_instances_implies_equal_types a1 a2 s1 s1' let live_same_addresses_equal_types_and_preorders #_ #_ #_ #_ #_ #_ b1 b2 h = Classical.move_requires (live_same_addresses_equal_types_and_preorders' b1 b2) h (* Untyped view of buffers, used only to implement the generic modifies clause. DO NOT USE in client code. *) noeq type ubuffer_ : Type0 = { b_max_length: nat; b_offset: nat; b_length: nat; b_is_mm: bool; } val ubuffer' (region: HS.rid) (addr: nat) : Tot Type0 let ubuffer' region addr = (x: ubuffer_ { x.b_offset + x.b_length <= x.b_max_length } ) let ubuffer (region: HS.rid) (addr: nat) : Tot Type0 = G.erased (ubuffer' region addr) let ubuffer_of_buffer' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) = if Null? b then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else Ghost.hide ({ b_max_length = U32.v (Buffer?.max_length b); b_offset = U32.v (Buffer?.idx b); b_length = U32.v (Buffer?.length b); b_is_mm = HS.is_mm (Buffer?.content b); }) let ubuffer_preserved' (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 = forall (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) . ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) val ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h h' : HS.mem) : GTot Type0 let ubuffer_preserved = ubuffer_preserved' let ubuffer_preserved_intro (#r:HS.rid) (#a:nat) (b:ubuffer r a) (h h' :HS.mem) (f0: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires (frameOf b' == r /\ as_addr b' == a /\ live h b')) (ensures (live h' b')) )) (f: ( (t':Type0) -> (rrel:srel t') -> (rel:srel t') -> (b':mbuffer t' rrel rel) -> Lemma (requires ( frameOf b' == r /\ as_addr b' == a /\ live h b' /\ live h' b' /\ Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len )))) (ensures ( Buffer? b' /\ ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len /\ Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))) )) : Lemma (ubuffer_preserved b h h') = let g' (t':Type0) (rrel rel:srel t') (b':mbuffer t' rrel rel) : Lemma ((frameOf b' == r /\ as_addr b' == a) ==> ( (live h b' ==> live h' b') /\ ( ((live h b' /\ live h' b' /\ Buffer? b') ==> ( let ({ b_max_length = bmax; b_offset = boff; b_length = blen }) = Ghost.reveal b in let Buffer max _ idx len = b' in ( U32.v max == bmax /\ U32.v idx <= boff /\ boff + blen <= U32.v idx + U32.v len ) ==> Seq.equal (Seq.slice (as_seq h b') (boff - U32.v idx) (boff - U32.v idx + blen)) (Seq.slice (as_seq h' b') (boff - U32.v idx) (boff - U32.v idx + blen)) ))))) = Classical.move_requires (f0 t' rrel rel) b'; Classical.move_requires (f t' rrel rel) b' in Classical.forall_intro_4 g' val ubuffer_preserved_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h : HS.mem) : Lemma (ubuffer_preserved b h h) let ubuffer_preserved_refl #r #a b h = () val ubuffer_preserved_trans (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2 h3 : HS.mem) : Lemma (requires (ubuffer_preserved b h1 h2 /\ ubuffer_preserved b h2 h3)) (ensures (ubuffer_preserved b h1 h3)) let ubuffer_preserved_trans #r #a b h1 h2 h3 = () val same_mreference_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) (f: ( (a' : Type) -> (pre: Preorder.preorder a') -> (r': HS.mreference a' pre) -> Lemma (requires (h1 `HS.contains` r' /\ r == HS.frameOf r' /\ a == HS.as_addr r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) )) : Lemma (ubuffer_preserved b h1 h2) let same_mreference_ubuffer_preserved #r #a b h1 h2 f = ubuffer_preserved_intro b h1 h2 (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) (fun t' _ _ b' -> if Null? b' then () else f _ _ (Buffer?.content b') ) val addr_unused_in_ubuffer_preserved (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (HS.live_region h1 r ==> a `Heap.addr_unused_in` (Map.sel (HS.get_hmap h1) r))) (ensures (ubuffer_preserved b h1 h2)) let addr_unused_in_ubuffer_preserved #r #a b h1 h2 = () val ubuffer_of_buffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) :Tot (ubuffer (frameOf b) (as_addr b)) let ubuffer_of_buffer #_ #_ #_ b = ubuffer_of_buffer' b let ubuffer_of_buffer_from_to_none_cond #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot bool = g_is_null b || U32.v to < U32.v from || U32.v from > length b let ubuffer_of_buffer_from_to #a #rrel #rel (b: mbuffer a rrel rel) from to : GTot (ubuffer (frameOf b) (as_addr b)) = if ubuffer_of_buffer_from_to_none_cond b from to then Ghost.hide ({ b_max_length = 0; b_offset = 0; b_length = 0; b_is_mm = false; }) else let to' = if U32.v to > length b then length b else U32.v to in let b1 = ubuffer_of_buffer b in Ghost.hide ({ Ghost.reveal b1 with b_offset = (Ghost.reveal b1).b_offset + U32.v from; b_length = to' - U32.v from }) val ubuffer_preserved_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' /\ live h b)) (ensures (live h' b /\ as_seq h b == as_seq h' b)) let ubuffer_preserved_elim #_ #_ #_ _ _ _ = () val ubuffer_preserved_from_to_elim (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h h' : HS.mem) :Lemma (requires (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) h h' /\ live h b)) (ensures (live h' b /\ ((U32.v from <= U32.v to /\ U32.v to <= length b) ==> Seq.slice (as_seq h b) (U32.v from) (U32.v to) == Seq.slice (as_seq h' b) (U32.v from) (U32.v to)))) let ubuffer_preserved_from_to_elim #_ #_ #_ _ _ _ _ _ = () let unused_in_ubuffer_preserved (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h h':HS.mem) : Lemma (requires (b `unused_in` h)) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h')) = Classical.move_requires (fun b -> live_not_unused_in h b) b; live_null a rrel rel h; null_unique b; unused_in_equiv b h; addr_unused_in_ubuffer_preserved #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) h h' let ubuffer_includes' (larger smaller: ubuffer_) : GTot Type0 = larger.b_is_mm == smaller.b_is_mm /\ larger.b_max_length == smaller.b_max_length /\ larger.b_offset <= smaller.b_offset /\ smaller.b_offset + smaller.b_length <= larger.b_offset + larger.b_length (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_includes0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (larger:ubuffer r1 a1) (smaller:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_includes' (G.reveal larger) (G.reveal smaller) val ubuffer_includes (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) : GTot Type0 let ubuffer_includes #r #a larger smaller = ubuffer_includes0 larger smaller val ubuffer_includes_refl (#r: HS.rid) (#a: nat) (b: ubuffer r a) : Lemma (b `ubuffer_includes` b) let ubuffer_includes_refl #r #a b = () val ubuffer_includes_trans (#r: HS.rid) (#a: nat) (b1 b2 b3: ubuffer r a) : Lemma (requires (b1 `ubuffer_includes` b2 /\ b2 `ubuffer_includes` b3)) (ensures (b1 `ubuffer_includes` b3)) let ubuffer_includes_trans #r #a b1 b2 b3 = () (* * TODO: not sure how to make this lemma work with preorders * it creates a buffer larger' in the proof * we need a compatible preorder for that * may be take that as an argument? *) (*val ubuffer_includes_ubuffer_preserved (#r: HS.rid) (#a: nat) (larger smaller: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (larger `ubuffer_includes` smaller /\ ubuffer_preserved larger h1 h2)) (ensures (ubuffer_preserved smaller h1 h2)) let ubuffer_includes_ubuffer_preserved #r #a larger smaller h1 h2 = ubuffer_preserved_intro smaller h1 h2 (fun t' b' -> if Null? b' then () else let (Buffer max_len content idx' len') = b' in let idx = U32.uint_to_t (G.reveal larger).b_offset in let len = U32.uint_to_t (G.reveal larger).b_length in let larger' = Buffer max_len content idx len in assert (b' == gsub larger' (U32.sub idx' idx) len'); ubuffer_preserved_elim larger' h1 h2 )*) let ubuffer_disjoint' (x1 x2: ubuffer_) : GTot Type0 = if x1.b_length = 0 || x2.b_length = 0 then True else (x1.b_max_length == x2.b_max_length /\ (x1.b_offset + x1.b_length <= x2.b_offset \/ x2.b_offset + x2.b_length <= x1.b_offset)) (* TODO: added this because of #606, now that it is fixed, we may not need it anymore *) let ubuffer_disjoint0 (#r1 #r2:HS.rid) (#a1 #a2:nat) (b1:ubuffer r1 a1) (b2:ubuffer r2 a2) = r1 == r2 /\ a1 == a2 /\ ubuffer_disjoint' (G.reveal b1) (G.reveal b2) val ubuffer_disjoint (#r:HS.rid) (#a:nat) (b1 b2:ubuffer r a) :GTot Type0 let ubuffer_disjoint #r #a b1 b2 = ubuffer_disjoint0 b1 b2 val ubuffer_disjoint_sym (#r:HS.rid) (#a: nat) (b1 b2:ubuffer r a) :Lemma (ubuffer_disjoint b1 b2 <==> ubuffer_disjoint b2 b1) let ubuffer_disjoint_sym #_ #_ b1 b2 = () val ubuffer_disjoint_includes (#r: HS.rid) (#a: nat) (larger1 larger2: ubuffer r a) (smaller1 smaller2: ubuffer r a) : Lemma (requires (ubuffer_disjoint larger1 larger2 /\ larger1 `ubuffer_includes` smaller1 /\ larger2 `ubuffer_includes` smaller2)) (ensures (ubuffer_disjoint smaller1 smaller2)) let ubuffer_disjoint_includes #r #a larger1 larger2 smaller1 smaller2 = () val liveness_preservation_intro (#a:Type0) (#rrel:srel a) (#rel:srel a) (h h':HS.mem) (b:mbuffer a rrel rel) (f: ( (t':Type0) -> (pre: Preorder.preorder t') -> (r: HS.mreference t' pre) -> Lemma (requires (HS.frameOf r == frameOf b /\ HS.as_addr r == as_addr b /\ h `HS.contains` r)) (ensures (h' `HS.contains` r)) )) :Lemma (requires (live h b)) (ensures (live h' b)) let liveness_preservation_intro #_ #_ #_ _ _ b f = if Null? b then () else f _ _ (Buffer?.content b) (* Basic, non-compositional modifies clauses, used only to implement the generic modifies clause. DO NOT USE in client code *) let modifies_0_preserves_mreferences (h1 h2: HS.mem) : GTot Type0 = forall (a: Type) (pre: Preorder.preorder a) (r: HS.mreference a pre) . h1 `HS.contains` r ==> (h2 `HS.contains` r /\ HS.sel h1 r == HS.sel h2 r) let modifies_0_preserves_regions (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) . HS.live_region h1 r ==> HS.live_region h2 r let modifies_0_preserves_not_unused_in (h1 h2: HS.mem) : GTot Type0 = forall (r: HS.rid) (n: nat) . ( HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) ) ==> ( n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r) ) let modifies_0' (h1 h2: HS.mem) : GTot Type0 = modifies_0_preserves_mreferences h1 h2 /\ modifies_0_preserves_regions h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 val modifies_0 (h1 h2: HS.mem) : GTot Type0 let modifies_0 = modifies_0' val modifies_0_live_region (h1 h2: HS.mem) (r: HS.rid) : Lemma (requires (modifies_0 h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_0_live_region h1 h2 r = () val modifies_0_mreference (#a: Type) (#pre: Preorder.preorder a) (h1 h2: HS.mem) (r: HS.mreference a pre) : Lemma (requires (modifies_0 h1 h2 /\ h1 `HS.contains` r)) (ensures (h2 `HS.contains` r /\ h1 `HS.sel` r == h2 `HS.sel` r)) let modifies_0_mreference #a #pre h1 h2 r = () let modifies_0_ubuffer (#r: HS.rid) (#a: nat) (b: ubuffer r a) (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (ubuffer_preserved b h1 h2)) = same_mreference_ubuffer_preserved b h1 h2 (fun a' pre r' -> modifies_0_mreference h1 h2 r') val modifies_0_unused_in (h1 h2: HS.mem) (r: HS.rid) (n: nat) : Lemma (requires ( modifies_0 h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r) )) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_0_unused_in h1 h2 r n = () let modifies_1_preserves_mreferences (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). ((frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r') ==> (h2 `HS.contains` r' /\ HS.sel h1 r' == HS.sel h2 r') let modifies_1_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_from_to_preserves_ubuffers (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = forall (b':ubuffer (frameOf b) (as_addr b)). (ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b') ==> ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2 let modifies_1_preserves_livenesses (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = forall (a':Type) (pre:Preorder.preorder a') (r':HS.mreference a' pre). h1 `HS.contains` r' ==> h2 `HS.contains` r' let modifies_1' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_preserves_ubuffers b h1 h2 val modifies_1 (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_1 = modifies_1' let modifies_1_from_to (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) : GTot Type0 = if ubuffer_of_buffer_from_to_none_cond b from to then modifies_0 h1 h2 else modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_1_preserves_livenesses b h1 h2 /\ modifies_0_preserves_not_unused_in h1 h2 /\ modifies_1_from_to_preserves_ubuffers b from to h1 h2 val modifies_1_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_1_live_region #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) = () val modifies_1_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1 b h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) let modifies_1_liveness #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_liveness (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r')) = () val modifies_1_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1 b h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_1_unused_in #_ #_ #_ _ _ _ _ _ = () let modifies_1_from_to_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (r:HS.rid) (n:nat) :Lemma (requires (modifies_1_from_to b from to h1 h2 /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) = () val modifies_1_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1 b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_1_mreference #_ #_ #_ _ _ _ #_ #_ _ = () let modifies_1_from_to_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r': HS.mreference a' pre) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) = () val modifies_1_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1 b h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) let modifies_1_ubuffer #_ #_ #_ _ _ _ _ = () let modifies_1_from_to_ubuffer (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) (b':ubuffer (frameOf b) (as_addr b)) : Lemma (requires (modifies_1_from_to b from to h1 h2 /\ ubuffer_disjoint #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) b')) (ensures (ubuffer_preserved #(frameOf b) #(as_addr b) b' h1 h2)) = () val modifies_1_null (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) : Lemma (requires (modifies_1 b h1 h2 /\ g_is_null b)) (ensures (modifies_0 h1 h2)) let modifies_1_null #_ #_ #_ _ _ _ = () let modifies_addr_of_preserves_not_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = forall (r: HS.rid) (n: nat) . ((r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r)) ==> (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r)) let modifies_addr_of' (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 = modifies_0_preserves_regions h1 h2 /\ modifies_1_preserves_mreferences b h1 h2 /\ modifies_addr_of_preserves_not_unused_in b h1 h2 val modifies_addr_of (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :GTot Type0 let modifies_addr_of = modifies_addr_of' val modifies_addr_of_live_region (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) :Lemma (requires (modifies_addr_of b h1 h2 /\ HS.live_region h1 r)) (ensures (HS.live_region h2 r)) let modifies_addr_of_live_region #_ #_ #_ _ _ _ _ = () val modifies_addr_of_mreference (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (#a':Type0) (#pre:Preorder.preorder a') (r':HS.mreference a' pre) : Lemma (requires (modifies_addr_of b h1 h2 /\ (frameOf b <> HS.frameOf r' \/ as_addr b <> HS.as_addr r') /\ h1 `HS.contains` r')) (ensures (h2 `HS.contains` r' /\ h1 `HS.sel` r' == h2 `HS.sel` r')) let modifies_addr_of_mreference #_ #_ #_ _ _ _ #_ #_ _ = () val modifies_addr_of_unused_in (#a:Type0) (#rrel:srel a) (#rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) (r:HS.rid) (n:nat) : Lemma (requires (modifies_addr_of b h1 h2 /\ (r <> frameOf b \/ n <> as_addr b) /\ HS.live_region h1 r /\ HS.live_region h2 r /\ n `Heap.addr_unused_in` (HS.get_hmap h2 `Map.sel` r))) (ensures (n `Heap.addr_unused_in` (HS.get_hmap h1 `Map.sel` r))) let modifies_addr_of_unused_in #_ #_ #_ _ _ _ _ _ = () module MG = FStar.ModifiesGen let cls : MG.cls ubuffer = MG.Cls #ubuffer ubuffer_includes (fun #r #a x -> ubuffer_includes_refl x) (fun #r #a x1 x2 x3 -> ubuffer_includes_trans x1 x2 x3) ubuffer_disjoint (fun #r #a x1 x2 -> ubuffer_disjoint_sym x1 x2) (fun #r #a larger1 larger2 smaller1 smaller2 -> ubuffer_disjoint_includes larger1 larger2 smaller1 smaller2) ubuffer_preserved (fun #r #a x h -> ubuffer_preserved_refl x h) (fun #r #a x h1 h2 h3 -> ubuffer_preserved_trans x h1 h2 h3) (fun #r #a b h1 h2 f -> same_mreference_ubuffer_preserved b h1 h2 f) let loc = MG.loc cls let _ = intro_ambient loc let loc_none = MG.loc_none let _ = intro_ambient loc_none let loc_union = MG.loc_union let _ = intro_ambient loc_union let loc_union_idem = MG.loc_union_idem let loc_union_comm = MG.loc_union_comm let loc_union_assoc = MG.loc_union_assoc let loc_union_loc_none_l = MG.loc_union_loc_none_l let loc_union_loc_none_r = MG.loc_union_loc_none_r let loc_buffer_from_to #a #rrel #rel b from to = if ubuffer_of_buffer_from_to_none_cond b from to then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) let loc_buffer #_ #_ #_ b = if g_is_null b then MG.loc_none else MG.loc_of_aloc #_ #_ #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_buffer_eq #_ #_ #_ _ = () let loc_buffer_from_to_high #_ #_ #_ _ _ _ = () let loc_buffer_from_to_none #_ #_ #_ _ _ _ = () let loc_buffer_from_to_mgsub #_ #_ #_ _ _ _ _ _ _ = () let loc_buffer_mgsub_eq #_ #_ #_ _ _ _ _ = () let loc_buffer_null _ _ _ = () let loc_buffer_from_to_eq #_ #_ #_ _ _ _ = () let loc_buffer_mgsub_rel_eq #_ #_ #_ _ _ _ _ _ = () let loc_addresses = MG.loc_addresses let loc_regions = MG.loc_regions let loc_includes = MG.loc_includes let loc_includes_refl = MG.loc_includes_refl let loc_includes_trans = MG.loc_includes_trans let loc_includes_union_r = MG.loc_includes_union_r let loc_includes_union_l = MG.loc_includes_union_l let loc_includes_none = MG.loc_includes_none val loc_includes_buffer (#a:Type0) (#rrel1:srel a) (#rrel2:srel a) (#rel1:srel a) (#rel2:srel a) (b1:mbuffer a rrel1 rel1) (b2:mbuffer a rrel2 rel2) :Lemma (requires (frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2 /\ ubuffer_includes0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_includes (loc_buffer b1) (loc_buffer b2))) let loc_includes_buffer #t #_ #_ #_ #_ b1 b2 = let t1 = ubuffer (frameOf b1) (as_addr b1) in MG.loc_includes_aloc #_ #cls #(frameOf b1) #(as_addr b1) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_includes_gsub_buffer_r l #_ #_ #_ b i len sub_rel = let b' = mgsub sub_rel b i len in loc_includes_buffer b b'; loc_includes_trans l (loc_buffer b) (loc_buffer b') let loc_includes_gsub_buffer_l #_ #_ #rel b i1 len1 sub_rel1 i2 len2 sub_rel2 = let b1 = mgsub sub_rel1 b i1 len1 in let b2 = mgsub sub_rel2 b i2 len2 in loc_includes_buffer b1 b2 let loc_includes_loc_buffer_loc_buffer_from_to #_ #_ #_ b from to = if ubuffer_of_buffer_from_to_none_cond b from to then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) (ubuffer_of_buffer_from_to b from to) let loc_includes_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_includes_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) #push-options "--z3rlimit 20" let loc_includes_as_seq #_ #rrel #_ #_ h1 h2 larger smaller = if Null? smaller then () else if Null? larger then begin MG.loc_includes_none_elim (loc_buffer smaller); MG.loc_of_aloc_not_none #_ #cls #(frameOf smaller) #(as_addr smaller) (ubuffer_of_buffer smaller) end else begin MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller); let ul = Ghost.reveal (ubuffer_of_buffer larger) in let us = Ghost.reveal (ubuffer_of_buffer smaller) in assert (as_seq h1 smaller == Seq.slice (as_seq h1 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)); assert (as_seq h2 smaller == Seq.slice (as_seq h2 larger) (us.b_offset - ul.b_offset) (us.b_offset - ul.b_offset + length smaller)) end #pop-options let loc_includes_addresses_buffer #a #rrel #srel preserve_liveness r s p = MG.loc_includes_addresses_aloc #_ #cls preserve_liveness r s #(as_addr p) (ubuffer_of_buffer p) let loc_includes_region_buffer #_ #_ #_ preserve_liveness s b = MG.loc_includes_region_aloc #_ #cls preserve_liveness s #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let loc_includes_region_addresses = MG.loc_includes_region_addresses #_ #cls let loc_includes_region_region = MG.loc_includes_region_region #_ #cls let loc_includes_region_union_l = MG.loc_includes_region_union_l let loc_includes_addresses_addresses = MG.loc_includes_addresses_addresses cls let loc_disjoint = MG.loc_disjoint let loc_disjoint_sym = MG.loc_disjoint_sym let loc_disjoint_none_r = MG.loc_disjoint_none_r let loc_disjoint_union_r = MG.loc_disjoint_union_r let loc_disjoint_includes = MG.loc_disjoint_includes val loc_disjoint_buffer (#a1 #a2:Type0) (#rrel1 #rel1:srel a1) (#rrel2 #rel2:srel a2) (b1:mbuffer a1 rrel1 rel1) (b2:mbuffer a2 rrel2 rel2) :Lemma (requires ((frameOf b1 == frameOf b2 /\ as_addr b1 == as_addr b2) ==> ubuffer_disjoint0 #(frameOf b1) #(frameOf b2) #(as_addr b1) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2))) (ensures (loc_disjoint (loc_buffer b1) (loc_buffer b2))) let loc_disjoint_buffer #_ #_ #_ #_ #_ #_ b1 b2 = MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) let loc_disjoint_gsub_buffer #_ #_ #_ b i1 len1 sub_rel1 i2 len2 sub_rel2 = loc_disjoint_buffer (mgsub sub_rel1 b i1 len1) (mgsub sub_rel2 b i2 len2) let loc_disjoint_loc_buffer_from_to #_ #_ #_ b from1 to1 from2 to2 = if ubuffer_of_buffer_from_to_none_cond b from1 to1 || ubuffer_of_buffer_from_to_none_cond b from2 to2 then () else MG.loc_disjoint_aloc_intro #_ #cls #(frameOf b) #(as_addr b) #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from1 to1) (ubuffer_of_buffer_from_to b from2 to2) let loc_disjoint_addresses = MG.loc_disjoint_addresses_intro #_ #cls let loc_disjoint_regions = MG.loc_disjoint_regions #_ #cls let modifies = MG.modifies let modifies_live_region = MG.modifies_live_region let modifies_mreference_elim = MG.modifies_mreference_elim let modifies_buffer_elim #_ #_ #_ b p h h' = if g_is_null b then assert (as_seq h b `Seq.equal` as_seq h' b) else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) p h h' ; ubuffer_preserved_elim b h h' end let modifies_buffer_from_to_elim #_ #_ #_ b from to p h h' = if g_is_null b then () else begin MG.modifies_aloc_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) p h h' ; ubuffer_preserved_from_to_elim b from to h h' end let modifies_refl = MG.modifies_refl let modifies_loc_includes = MG.modifies_loc_includes let address_liveness_insensitive_locs = MG.address_liveness_insensitive_locs _ let region_liveness_insensitive_locs = MG.region_liveness_insensitive_locs _ let address_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_address_liveness_insensitive_locs_aloc #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let address_liveness_insensitive_addresses = MG.loc_includes_address_liveness_insensitive_locs_addresses cls let region_liveness_insensitive_buffer #_ #_ #_ b = MG.loc_includes_region_liveness_insensitive_locs_loc_of_aloc #_ cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) let region_liveness_insensitive_addresses = MG.loc_includes_region_liveness_insensitive_locs_loc_addresses cls let region_liveness_insensitive_regions = MG.loc_includes_region_liveness_insensitive_locs_loc_regions cls let region_liveness_insensitive_address_liveness_insensitive = MG.loc_includes_region_liveness_insensitive_locs_address_liveness_insensitive_locs cls let modifies_liveness_insensitive_mreference = MG.modifies_preserves_liveness let modifies_liveness_insensitive_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else liveness_preservation_intro h h' x (fun t' pre r -> MG.modifies_preserves_liveness_strong l1 l2 h h' r (ubuffer_of_buffer x)) let modifies_liveness_insensitive_region = MG.modifies_preserves_region_liveness let modifies_liveness_insensitive_region_mreference = MG.modifies_preserves_region_liveness_reference let modifies_liveness_insensitive_region_buffer l1 l2 h h' #_ #_ #_ x = if g_is_null x then () else MG.modifies_preserves_region_liveness_aloc l1 l2 h h' #(frameOf x) #(as_addr x) (ubuffer_of_buffer x) let modifies_trans = MG.modifies_trans let modifies_only_live_regions = MG.modifies_only_live_regions let no_upd_fresh_region = MG.no_upd_fresh_region let new_region_modifies = MG.new_region_modifies #_ cls let modifies_fresh_frame_popped = MG.modifies_fresh_frame_popped let modifies_loc_regions_intro = MG.modifies_loc_regions_intro #_ #cls let modifies_loc_addresses_intro = MG.modifies_loc_addresses_intro #_ #cls let modifies_ralloc_post = MG.modifies_ralloc_post #_ #cls let modifies_salloc_post = MG.modifies_salloc_post #_ #cls let modifies_free = MG.modifies_free #_ #cls let modifies_none_modifies = MG.modifies_none_modifies #_ #cls let modifies_upd = MG.modifies_upd #_ #cls val modifies_0_modifies (h1 h2: HS.mem) : Lemma (requires (modifies_0 h1 h2)) (ensures (modifies loc_none h1 h2)) let modifies_0_modifies h1 h2 = MG.modifies_none_intro #_ #cls h1 h2 (fun r -> modifies_0_live_region h1 h2 r) (fun t pre b -> modifies_0_mreference #t #pre h1 h2 b) (fun r n -> modifies_0_unused_in h1 h2 r n) val modifies_1_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_1 b h1 h2)) (ensures (modifies (loc_buffer b) h1 h2)) let modifies_1_modifies #t #_ #_ b h1 h2 = if g_is_null b then begin modifies_1_null b h1 h2; modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer b) h1 h2 (fun r -> modifies_1_live_region b h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer b); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer b) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_mreference b h1 h2 p ) (fun t pre p -> modifies_1_liveness b h1 h2 p ) (fun r n -> modifies_1_unused_in b h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer b); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer b) b'; if frameOf b = r' && as_addr b = a' then modifies_1_ubuffer #t b h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_mreference b h1 h2 r_) ) val modifies_1_from_to_modifies (#a:Type0)(#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (h1 h2:HS.mem) :Lemma (requires (modifies_1_from_to b from to h1 h2)) (ensures (modifies (loc_buffer_from_to b from to) h1 h2)) let modifies_1_from_to_modifies #t #_ #_ b from to h1 h2 = if ubuffer_of_buffer_from_to_none_cond b from to then begin modifies_0_modifies h1 h2 end else MG.modifies_intro (loc_buffer_from_to b from to) h1 h2 (fun r -> modifies_1_from_to_live_region b from to h1 h2 r) (fun t pre p -> loc_disjoint_sym (loc_mreference p) (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_addresses_elim #_ #cls #(frameOf b) #(as_addr b) (ubuffer_of_buffer_from_to b from to) true (HS.frameOf p) (Set.singleton (HS.as_addr p)); modifies_1_from_to_mreference b from to h1 h2 p ) (fun t pre p -> modifies_1_from_to_liveness b from to h1 h2 p ) (fun r n -> modifies_1_from_to_unused_in b from to h1 h2 r n ) (fun r' a' b' -> loc_disjoint_sym (MG.loc_of_aloc b') (loc_buffer_from_to b from to); MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b) #(as_addr b) #r' #a' (ubuffer_of_buffer_from_to b from to) b'; if frameOf b = r' && as_addr b = a' then modifies_1_from_to_ubuffer #t b from to h1 h2 b' else same_mreference_ubuffer_preserved #r' #a' b' h1 h2 (fun a_ pre_ r_ -> modifies_1_from_to_mreference b from to h1 h2 r_) ) val modifies_addr_of_modifies (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (h1 h2:HS.mem) :Lemma (requires (modifies_addr_of b h1 h2)) (ensures (modifies (loc_addr_of_buffer b) h1 h2)) let modifies_addr_of_modifies #t #_ #_ b h1 h2 = MG.modifies_address_intro #_ #cls (frameOf b) (as_addr b) h1 h2 (fun r -> modifies_addr_of_live_region b h1 h2 r) (fun t pre p -> modifies_addr_of_mreference b h1 h2 p ) (fun r n -> modifies_addr_of_unused_in b h1 h2 r n ) val modifies_loc_buffer_from_to_intro' (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (from to: U32.t) (l: loc) (h h' : HS.mem) : Lemma (requires ( let s = as_seq h b in let s' = as_seq h' b in not (g_is_null b) /\ live h b /\ modifies (loc_union l (loc_buffer b)) h h' /\ U32.v from <= U32.v to /\ U32.v to <= length b /\ Seq.slice s 0 (U32.v from) `Seq.equal` Seq.slice s' 0 (U32.v from) /\ Seq.slice s (U32.v to) (length b) `Seq.equal` Seq.slice s' (U32.v to) (length b) )) (ensures (modifies (loc_union l (loc_buffer_from_to b from to)) h h')) #push-options "--z3rlimit 16" let modifies_loc_buffer_from_to_intro' #a #rrel #rel b from to l h h' = let r0 = frameOf b in let a0 = as_addr b in let bb : ubuffer r0 a0 = ubuffer_of_buffer b in modifies_loc_includes (loc_union l (loc_addresses true r0 (Set.singleton a0))) h h' (loc_union l (loc_buffer b)); MG.modifies_strengthen l #r0 #a0 (ubuffer_of_buffer_from_to b from to) h h' (fun f (x: ubuffer r0 a0) -> ubuffer_preserved_intro x h h' (fun t' rrel' rel' b' -> f _ _ (Buffer?.content b')) (fun t' rrel' rel' b' -> // prove that the types, rrels, rels are equal Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); assert (Seq.seq t' == Seq.seq a); let _s0 : Seq.seq a = as_seq h b in let _s1 : Seq.seq t' = coerce_eq _ _s0 in lemma_equal_instances_implies_equal_types a t' _s0 _s1; let boff = U32.v (Buffer?.idx b) in let from_ = boff + U32.v from in let to_ = boff + U32.v to in let ({ b_max_length = ml; b_offset = xoff; b_length = xlen; b_is_mm = is_mm }) = Ghost.reveal x in let ({ b_max_length = _; b_offset = b'off; b_length = b'len }) = Ghost.reveal (ubuffer_of_buffer b') in let bh = as_seq h b in let bh' = as_seq h' b in let xh = Seq.slice (as_seq h b') (xoff - b'off) (xoff - b'off + xlen) in let xh' = Seq.slice (as_seq h' b') (xoff - b'off) (xoff - b'off + xlen) in let prf (i: nat) : Lemma (requires (i < xlen)) (ensures (i < xlen /\ Seq.index xh i == Seq.index xh' i)) = let xi = xoff + i in let bi : ubuffer r0 a0 = Ghost.hide ({ b_max_length = ml; b_offset = xi; b_length = 1; b_is_mm = is_mm; }) in assert (Seq.index xh i == Seq.index (Seq.slice (as_seq h b') (xi - b'off) (xi - b'off + 1)) 0); assert (Seq.index xh' i == Seq.index (Seq.slice (as_seq h' b') (xi - b'off) (xi - b'off + 1)) 0); let li = MG.loc_of_aloc bi in MG.loc_includes_aloc #_ #cls x bi; loc_disjoint_includes l (MG.loc_of_aloc x) l li; if xi < boff || boff + length b <= xi then begin MG.loc_disjoint_aloc_intro #_ #cls bb bi; assert (loc_disjoint (loc_union l (loc_buffer b)) li); MG.modifies_aloc_elim bi (loc_union l (loc_buffer b)) h h' end else if xi < from_ then begin assert (Seq.index xh i == Seq.index (Seq.slice bh 0 (U32.v from)) (xi - boff)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' 0 (U32.v from)) (xi - boff)) end else begin assert (to_ <= xi); assert (Seq.index xh i == Seq.index (Seq.slice bh (U32.v to) (length b)) (xi - to_)); assert (Seq.index xh' i == Seq.index (Seq.slice bh' (U32.v to) (length b)) (xi - to_)) end in Classical.forall_intro (Classical.move_requires prf); assert (xh `Seq.equal` xh') ) ) #pop-options let modifies_loc_buffer_from_to_intro #a #rrel #rel b from to l h h' = if g_is_null b then () else modifies_loc_buffer_from_to_intro' b from to l h h' let does_not_contain_addr = MG.does_not_contain_addr let not_live_region_does_not_contain_addr = MG.not_live_region_does_not_contain_addr let unused_in_does_not_contain_addr = MG.unused_in_does_not_contain_addr let addr_unused_in_does_not_contain_addr = MG.addr_unused_in_does_not_contain_addr let free_does_not_contain_addr = MG.free_does_not_contain_addr let does_not_contain_addr_elim = MG.does_not_contain_addr_elim let modifies_only_live_addresses = MG.modifies_only_live_addresses let loc_not_unused_in = MG.loc_not_unused_in _ let loc_unused_in = MG.loc_unused_in _ let loc_regions_unused_in = MG.loc_regions_unused_in cls let loc_unused_in_not_unused_in_disjoint = MG.loc_unused_in_not_unused_in_disjoint cls let not_live_region_loc_not_unused_in_disjoint = MG.not_live_region_loc_not_unused_in_disjoint cls let live_loc_not_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.does_not_contain_addr_addr_unused_in h) (frameOf b, as_addr b); MG.loc_addresses_not_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let unused_in_loc_unused_in #_ #_ #_ b h = unused_in_equiv b h; Classical.move_requires (MG.addr_unused_in_does_not_contain_addr h) (frameOf b, as_addr b); MG.loc_addresses_unused_in cls (frameOf b) (Set.singleton (as_addr b)) h; () let modifies_address_liveness_insensitive_unused_in = MG.modifies_address_liveness_insensitive_unused_in cls let modifies_only_not_unused_in = MG.modifies_only_not_unused_in let mreference_live_loc_not_unused_in = MG.mreference_live_loc_not_unused_in cls let mreference_unused_in_loc_unused_in = MG.mreference_unused_in_loc_unused_in cls let modifies_loc_unused_in l h1 h2 l' = modifies_loc_includes address_liveness_insensitive_locs h1 h2 l; modifies_address_liveness_insensitive_unused_in h1 h2; loc_includes_trans (loc_unused_in h1) (loc_unused_in h2) l' let fresh_frame_modifies h0 h1 = MG.fresh_frame_modifies #_ cls h0 h1 let popped_modifies = MG.popped_modifies #_ cls let modifies_remove_new_locs l_fresh l_aux l_goal h1 h2 h3 = modifies_only_not_unused_in l_goal h1 h3 let disjoint_neq #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () let empty_disjoint #t1 #t2 #rrel1 #rel1 #rrel2 #rel2 b1 b2 = let r = frameOf b1 in let a = as_addr b1 in if r = frameOf b2 && a = as_addr b2 then MG.loc_disjoint_aloc_intro #_ #cls #r #a #r #a (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) else () (* let includes_live #a #rrel #rel1 #rel2 h larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let includes_frameOf_as_addr #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) let pointer_distinct_sel_disjoint #a #_ #_ #_ #_ b1 b2 h = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then begin HS.mreference_distinct_sel_disjoint h (Buffer?.content b1) (Buffer?.content b2); loc_disjoint_buffer b1 b2 end else loc_disjoint_buffer b1 b2 let is_null #_ #_ #_ b = Null? b let msub #a #rrel #rel sub_rel b i len = match b with | Null -> Null | Buffer max_len content i0 len0 -> Buffer max_len content (U32.add i0 i) len let moffset #a #rrel #rel sub_rel b i = match b with | Null -> Null | Buffer max_len content i0 len -> Buffer max_len content (U32.add i0 i) (Ghost.hide ((U32.sub (Ghost.reveal len) i))) let index #_ #_ #_ b i = let open HST in let s = ! (Buffer?.content b) in Seq.index s (U32.v (Buffer?.idx b) + U32.v i) let g_upd_seq #_ #_ #_ b s h = if Seq.length s = 0 then h else let s0 = HS.sel h (Buffer?.content b) in let Buffer _ content idx length = b in HS.upd h (Buffer?.content b) (Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v length) s) let lemma_g_upd_with_same_seq #_ #_ #_ b h = if Null? b then () else let open FStar.UInt32 in let Buffer _ content idx length = b in let s = HS.sel h content in assert (Seq.equal (Seq.replace_subseq s (v idx) (v idx + v length) (Seq.slice s (v idx) (v idx + v length))) s); HS.lemma_heap_equality_upd_with_sel h (Buffer?.content b) #push-options "--z3rlimit 48" let g_upd_seq_as_seq #a #_ #_ b s h = let h' = g_upd_seq b s h in if g_is_null b then assert (Seq.equal s Seq.empty) else begin assert (Seq.equal (as_seq h' b) s); // prove modifies_1_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_modifies b h h' end let g_upd_modifies_strong #_ #_ #_ b i v h = let h' = g_upd b i v h in // prove modifies_1_from_to_preserves_ubuffers Heap.lemma_distinct_addrs_distinct_preorders (); Heap.lemma_distinct_addrs_distinct_mm (); s_lemma_equal_instances_implies_equal_types (); modifies_1_from_to_modifies b (U32.uint_to_t i) (U32.uint_to_t (i + 1)) h h' #pop-options let upd' #_ #_ #_ b i v = let open HST in let h = get() in let Buffer max_length content idx len = b in let s0 = !content in let sb0 = Seq.slice s0 (U32.v idx) (U32.v max_length) in let s_upd = Seq.upd sb0 (U32.v i) v in let sf = Seq.replace_subseq s0 (U32.v idx) (U32.v max_length) s_upd in assert (sf `Seq.equal` Seq.replace_subseq s0 (U32.v idx) (U32.v idx + U32.v len) (Seq.upd (as_seq h b) (U32.v i) v)); content := sf let recallable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) :GTot Type0 = (not (g_is_null b)) ==> ( HST.is_eternal_region (frameOf b) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_buf #_ #_ #_ b = (not (g_is_null b)) ==> ( HS.is_heap_color (HS.color (frameOf b)) /\ not (HS.is_mm (Buffer?.content b)) /\ buffer_compatible b ) let region_lifetime_sub #a #rrel #rel #subrel b0 b1 = match b1 with | Null -> () | Buffer max_len content idx length -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j subrel) let recallable_null #_ #_ #_ = () let recallable_mgsub #_ #rrel #rel b i len sub_rel = match b with | Null -> () | Buffer max_len content idx length -> lemma_seq_sub_compatibility_is_transitive (U32.v max_len) rrel (U32.v idx) (U32.v idx + U32.v length) rel (U32.v i) (U32.v i + U32.v len) sub_rel (* let recallable_includes #_ #_ #_ #_ #_ #_ larger smaller = if Null? larger || Null? smaller then () else MG.loc_includes_aloc_elim #_ #cls #(frameOf larger) #(frameOf smaller) #(as_addr larger) #(as_addr smaller) (ubuffer_of_buffer larger) (ubuffer_of_buffer smaller) *) let recall #_ #_ #_ b = if Null? b then () else HST.recall (Buffer?.content b) private let spred_as_mempred (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :HST.mem_predicate = fun h -> buffer_compatible b ==> p (as_seq h b) let witnessed #_ #rrel #rel b p = match b with | Null -> p Seq.empty | Buffer max_length content idx length -> HST.token_p content (spred_as_mempred b p) private let lemma_stable_on_rel_is_stable_on_rrel (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (p:spred a) :Lemma (requires (Buffer? b /\ stable_on p rel)) (ensures (HST.stable_on (spred_as_mempred b p) (Buffer?.content b))) = let Buffer max_length content idx length = b in let mp = spred_as_mempred b p in let aux (h0 h1:HS.mem) :Lemma ((mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content)) ==> mp h1) = Classical.arrow_to_impl #(mp h0 /\ rrel (HS.sel h0 content) (HS.sel h1 content) /\ buffer_compatible b) #(mp h1) (fun _ -> assert (rel (as_seq h0 b) (as_seq h1 b))) in Classical.forall_intro_2 aux let witness_p #a #rrel #rel b p = match b with | Null -> () | Buffer _ content _ _ -> lemma_stable_on_rel_is_stable_on_rrel b p; //AR: TODO: the proof doesn't go through without this assertion, which should follow directly from the lemma call assert (HST.stable_on #(Seq.lseq a (U32.v (Buffer?.max_length b))) #(srel_to_lsrel (U32.v (Buffer?.max_length b)) rrel) (spred_as_mempred b p) (Buffer?.content b)); HST.witness_p content (spred_as_mempred b p) let recall_p #_ #_ #_ b p = match b with | Null -> () | Buffer _ content _ _ -> HST.recall_p content (spred_as_mempred b p) let witnessed_functorial #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> assert (as_seq HS.empty_mem b1 == Seq.empty) | Buffer _ content _ _, _ -> assert (forall (len:nat) (i:nat) (j:nat{i <= j /\ j <= len}). compatible_sub_preorder len rrel i j rel1); HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let witnessed_functorial_st #a #rrel #rel1 #rel2 b1 b2 i len s1 s2 = match b1, b2 with | Null, Null -> () | Buffer _ content _ _, _ -> HST.token_functoriality content (spred_as_mempred b1 s1) (spred_as_mempred b2 s2) let freeable (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) = (not (g_is_null b)) /\ HS.is_mm (Buffer?.content b) /\ HS.is_heap_color (HS.color (frameOf b)) /\ U32.v (Buffer?.max_length b) > 0 /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b let free #_ #_ #_ b = HST.rfree (Buffer?.content b) let freeable_length #_ #_ #_ b = () let freeable_disjoint #_ #_ #_ #_ #_ #_ b1 b2 = if frameOf b1 = frameOf b2 && as_addr b1 = as_addr b2 then MG.loc_disjoint_aloc_elim #_ #cls #(frameOf b1) #(as_addr b1) #(frameOf b2) #(as_addr b2) (ubuffer_of_buffer b1) (ubuffer_of_buffer b2) private let alloc_heap_common (#a:Type0) (#rrel:srel a) (r:HST.erid) (len:U32.t{U32.v len > 0}) (s:Seq.seq a{Seq.length s == U32.v len}) (mm:bool) :HST.ST (lmbuffer a rrel rrel (U32.v len)) (requires (fun _ -> True)) (ensures (fun h0 b h1 -> alloc_post_mem_common b h0 h1 s /\ frameOf b == r /\ HS.is_mm (Buffer?.content b) == mm /\ Buffer?.idx b == 0ul /\ Ghost.reveal (Buffer?.length b) == Buffer?.max_length b)) = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = if mm then HST.ralloc_mm r s else HST.ralloc r s in let b = Buffer len content 0ul (Ghost.hide len) in b let mgcmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) false private let read_sub_buffer (#a:Type0) (#rrel #rel:srel a) (b:mbuffer a rrel rel) (idx len:U32.t) : HST.ST (Seq.seq a) (requires fun h0 -> live h0 b /\ U32.v len > 0 /\ U32.v idx + U32.v len <= length b) (ensures fun h0 s h1 -> h0 == h1 /\ s == Seq.slice (as_seq h0 b) (U32.v idx) (U32.v idx + U32.v len)) = let open HST in let s = ! (Buffer?.content b) in //the whole allocation unit let s = Seq.slice s (U32.v (Buffer?.idx b)) (U32.v (Buffer?.max_length b)) in //b buffer Seq.slice s (U32.v idx) (U32.v idx + U32.v len) //slice of b let mgcmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) false let mmalloc #_ #_ r init len = alloc_heap_common r len (Seq.create (U32.v len) init) true let mmalloc_and_blit #_ #_ r #_ #_ src id_src len = alloc_heap_common r len (read_sub_buffer src id_src len) true let malloca #a #rrel init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (Seq.create (U32.v len) init) in Buffer len content 0ul (Ghost.hide len) let malloca_and_blit #a #rrel #_ #_ src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc (read_sub_buffer src id_src len) in Buffer len content 0ul (Ghost.hide len) let malloca_of_list #a #rrel init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.salloc s in Buffer len content 0ul (Ghost.hide len) let mgcmalloc_of_list #a #rrel r init = let len = U32.uint_to_t (FStar.List.Tot.length init) in let s = Seq.seq_of_list init in lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc r s in Buffer len content 0ul (Ghost.hide len) let mmalloc_drgn #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_mm #a #rrel d init len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content : HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn_mm d (Seq.create (U32.v len) init) in Buffer len content 0ul len let mmalloc_drgn_and_blit #a #rrel #_ #_ d src id_src len = lemma_seq_sub_compatilibity_is_reflexive (U32.v len) rrel; let content: HST.mreference (Seq.lseq a (U32.v len)) (srel_to_lsrel (U32.v len) rrel) = HST.ralloc_drgn d (read_sub_buffer src id_src len) in Buffer len content 0ul len
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Set.fsti.checked", "FStar.Seq.fst.checked", "FStar.Preorder.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.ModifiesGen.fsti.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Heap.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "LowStar.Monotonic.Buffer.fst" }
[ { "abbrev": true, "full_module": "FStar.ModifiesGen", "short_module": "MG" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "Seq" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.Ghost", "short_module": "G" }, { "abbrev": true, "full_module": "FStar.Preorder", "short_module": "P" }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Monotonic", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 64, "z3rlimit_factor": 4, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
src: LowStar.Monotonic.Buffer.mbuffer a rrel1 rel1 -> idx_src: FStar.UInt32.t -> dst: LowStar.Monotonic.Buffer.mbuffer a rrel2 rel2 -> idx_dst: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Monotonic.Buffer.srel", "LowStar.Monotonic.Buffer.mbuffer", "FStar.UInt32.t", "FStar.Pervasives.Native.Mktuple2", "FStar.HyperStack.ST.mreference", "FStar.Seq.Properties.lseq", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.srel_to_lsrel", "FStar.Ghost.erased", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.Ghost.reveal", "Prims.op_Equality", "FStar.UInt32.__uint_to_t", "Prims.unit", "Prims.bool", "LowStar.Monotonic.Buffer.g_upd_seq_as_seq", "FStar.Seq.Base.slice", "Prims._assert", "Prims.eq2", "FStar.Monotonic.HyperStack.mem", "LowStar.Monotonic.Buffer.g_upd_seq", "FStar.Seq.Base.equal", "FStar.Seq.Properties.replace_subseq", "FStar.HyperStack.ST.get", "FStar.HyperStack.ST.op_Colon_Equals", "LowStar.Monotonic.Buffer.as_seq", "LowStar.Monotonic.Buffer.length", "FStar.Seq.Base.seq", "FStar.HyperStack.ST.op_Bang" ]
[]
false
true
false
false
false
let blit #a #rrel1 #rrel2 #rel1 #rel2 src idx_src dst idx_dst len =
let open HST in match src, dst with | Buffer _ _ _ _, Buffer _ _ _ _ -> if len = 0ul then () else let h = get () in let Buffer max_length1 content1 idx1 length1 = src in let Buffer max_length2 content2 idx2 length2 = dst in let s_full1 = !content1 in let s_full2 = !content2 in let s1 = Seq.slice s_full1 (U32.v idx1) (U32.v max_length1) in let s2 = Seq.slice s_full2 (U32.v idx2) (U32.v max_length2) in let s_sub_src = Seq.slice s1 (U32.v idx_src) (U32.v idx_src + U32.v len) in let s2' = Seq.replace_subseq s2 (U32.v idx_dst) (U32.v idx_dst + U32.v len) s_sub_src in let s_full2' = Seq.replace_subseq s_full2 (U32.v idx2) (U32.v max_length2) s2' in assert (Seq.equal (Seq.slice s2' (U32.v idx_dst) (U32.v idx_dst + U32.v len)) s_sub_src); assert (Seq.equal (Seq.slice s2' 0 (U32.v idx_dst)) (Seq.slice s2 0 (U32.v idx_dst))); assert (Seq.equal (Seq.slice s2' (U32.v idx_dst + U32.v len) (length dst)) (Seq.slice s2 (U32.v idx_dst + U32.v len) (length dst))); assert (s_full2' `Seq.equal` (Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.replace_subseq (as_seq h dst) (U32.v idx_dst) (U32.v idx_dst + U32.v len) (Seq.slice (as_seq h src) (U32.v idx_src) (U32.v idx_src + U32.v len))))); content2 := s_full2'; let h1 = get () in assert (s_full2' `Seq.equal` (Seq.replace_subseq s_full2 (U32.v idx2) (U32.v idx2 + U32.v length2) (Seq.slice s2' 0 (U32.v length2)))); assert (h1 == g_upd_seq dst (Seq.slice s2' 0 (U32.v length2)) h); g_upd_seq_as_seq dst (Seq.slice s2' 0 (U32.v length2)) h | _, _ -> ()
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.is_keccak
val is_keccak : _: Spec.Hash.Definitions.hash_alg -> Prims.bool
let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 60, "start_col": 0, "start_line": 58 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.hash_alg -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.bool" ]
[]
false
false
false
true
false
let is_keccak =
function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.is_blake
val is_blake : _: Spec.Hash.Definitions.hash_alg -> Prims.bool
let is_blake = function | Blake2S | Blake2B -> true | _ -> false
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 70, "start_col": 0, "start_line": 68 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.hash_alg -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.bool" ]
[]
false
false
false
true
false
let is_blake =
function | Blake2S | Blake2B -> true | _ -> false
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.is_sha2
val is_sha2 : _: Spec.Hash.Definitions.hash_alg -> Prims.bool
let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 55, "start_col": 0, "start_line": 53 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.hash_alg -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.bool" ]
[]
false
false
false
true
false
let is_sha2 =
function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.keccak_alg
val keccak_alg : Type0
let keccak_alg = a:hash_alg { is_keccak a }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 43, "end_line": 78, "start_col": 0, "start_line": 78 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Spec.Hash.Definitions.is_keccak" ]
[]
false
false
false
true
true
let keccak_alg =
a: hash_alg{is_keccak a}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.is_md
val is_md : _: Spec.Hash.Definitions.hash_alg -> Prims.bool
let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 75, "start_col": 0, "start_line": 73 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.hash_alg -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.bool" ]
[]
false
false
false
true
false
let is_md =
function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.is_shake
val is_shake : _: Spec.Hash.Definitions.hash_alg -> Prims.bool
let is_shake = function | Shake128 | Shake256 -> true | _ -> false
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 14, "end_line": 65, "start_col": 0, "start_line": 63 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.hash_alg -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.bool" ]
[]
false
false
false
true
false
let is_shake =
function | Shake128 | Shake256 -> true | _ -> false
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.sha2_alg
val sha2_alg : Type0
let sha2_alg = a:hash_alg { is_sha2 a }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 39, "end_line": 77, "start_col": 0, "start_line": 77 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Spec.Hash.Definitions.is_sha2" ]
[]
false
false
false
true
true
let sha2_alg =
a: hash_alg{is_sha2 a}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.blake_alg
val blake_alg : Type0
let blake_alg = a:hash_alg { is_blake a }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 41, "end_line": 79, "start_col": 0, "start_line": 79 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a }
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Spec.Hash.Definitions.is_blake" ]
[]
false
false
false
true
true
let blake_alg =
a: hash_alg{is_blake a}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.md_alg
val md_alg : Type0
let md_alg = a:hash_alg { is_md a }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 80, "start_col": 0, "start_line": 80 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a }
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Spec.Hash.Definitions.is_md" ]
[]
false
false
false
true
true
let md_alg =
a: hash_alg{is_md a}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.maxed_hash_alg
val maxed_hash_alg : Type0
let maxed_hash_alg = a:hash_alg { Some? (max_input_length a) }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 62, "end_line": 107, "start_col": 0, "start_line": 107 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a } let blake_alg = a:hash_alg { is_blake a } let md_alg = a:hash_alg { is_md a } let fixed_len_alg = a:hash_alg { not (is_shake a) } inline_for_extraction let to_blake_alg (a:blake_alg) = match a with | Blake2S -> Spec.Blake2.Blake2S | Blake2B -> Spec.Blake2.Blake2B inline_for_extraction let to_hash_alg (a : Spec.Blake2.alg) = match a with | Spec.Blake2.Blake2S -> Blake2S | Spec.Blake2.Blake2B -> Blake2B (** Maximum input data length. *) (* In bytes. *) inline_for_extraction let max_input_length: hash_alg -> option pos = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> Some (pow2 61 - 1) | SHA2_384 | SHA2_512 -> Some (pow2 125 - 1) | Blake2S -> Some (pow2 64 - 1) | Blake2B -> Some (pow2 128 - 1) | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> None
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "FStar.Pervasives.Native.uu___is_Some", "Prims.pos", "Spec.Hash.Definitions.max_input_length" ]
[]
false
false
false
true
true
let maxed_hash_alg =
a: hash_alg{Some? (max_input_length a)}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.fixed_len_alg
val fixed_len_alg : Type0
let fixed_len_alg = a:hash_alg { not (is_shake a) }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 81, "start_col": 0, "start_line": 81 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a } let blake_alg = a:hash_alg { is_blake a }
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Prims.op_Negation", "Spec.Hash.Definitions.is_shake" ]
[]
false
false
false
true
true
let fixed_len_alg =
a: hash_alg{not (is_shake a)}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.bytes_blocks
val bytes_blocks : a: Spec.Hash.Definitions.hash_alg -> Type0
let bytes_blocks a = l:bytes { Seq.length l % block_length a = 0 }
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 308, "start_col": 0, "start_line": 307 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a } let blake_alg = a:hash_alg { is_blake a } let md_alg = a:hash_alg { is_md a } let fixed_len_alg = a:hash_alg { not (is_shake a) } inline_for_extraction let to_blake_alg (a:blake_alg) = match a with | Blake2S -> Spec.Blake2.Blake2S | Blake2B -> Spec.Blake2.Blake2B inline_for_extraction let to_hash_alg (a : Spec.Blake2.alg) = match a with | Spec.Blake2.Blake2S -> Blake2S | Spec.Blake2.Blake2B -> Blake2B (** Maximum input data length. *) (* In bytes. *) inline_for_extraction let max_input_length: hash_alg -> option pos = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> Some (pow2 61 - 1) | SHA2_384 | SHA2_512 -> Some (pow2 125 - 1) | Blake2S -> Some (pow2 64 - 1) | Blake2B -> Some (pow2 128 - 1) | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> None let maxed_hash_alg = a:hash_alg { Some? (max_input_length a) } let sha2_alg_is_maxed (a: hash_alg): Lemma (requires (is_sha2 a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_sha2 a) ] = () let md_alg_is_maxed (a: hash_alg): Lemma (requires (is_md a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_md a) ] = () let blake_alg_is_maxed (a: hash_alg): Lemma (requires (is_blake a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_blake a) ] = () // TODO: many of these definitions are only used by the MD padding scheme, // meaning they should be defined over md_alg! (* A type that can hold a maximum length, in bits. *) inline_for_extraction let len_int_type: maxed_hash_alg -> inttype = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> U64 | SHA2_384 | SHA2_512 -> U128 | Blake2S -> U64 | Blake2B -> U128 inline_for_extraction let nat_to_len (a:maxed_hash_alg) (n:nat{n <= maxint (len_int_type a)}) = mk_int #(len_int_type a ) #PUB n (* A type that can hold a maximum length, in bits. *) inline_for_extraction let len_t: hash_alg -> Type = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> pub_uint64 | SHA2_384 | SHA2_512 -> pub_uint128 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> unit | Blake2S -> pub_uint64 | Blake2B -> pub_uint128 val len_v: a:maxed_hash_alg -> len_t a -> nat let len_v = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> uint_v #U64 #PUB | SHA2_384 | SHA2_512 -> uint_v #U128 #PUB | Blake2S -> uint_v #U64 #PUB | Blake2B -> uint_v #U128 #PUB (* Number of bytes occupied by a len_t, i.e. the size of the encoded length in the padding. *) let len_length: maxed_hash_alg -> Tot nat = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> 8 | SHA2_384 | SHA2_512 -> 16 | Blake2S -> 8 | Blake2B -> 16 (* Same thing, as a machine integer *) inline_for_extraction let len_len: a:maxed_hash_alg -> Tot (n:size_t{v n = len_length a}) = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> 8ul | SHA2_384 | SHA2_512 -> 16ul | Blake2S -> 8ul | Blake2B -> 16ul (** Working state of the algorithms. *) (* Internally, hash functions operate on a series of machine words. *) inline_for_extraction let word_t: hash_alg -> Tot inttype = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> U32 | SHA2_384 | SHA2_512 -> U64 | Blake2S -> U32 | Blake2B -> U64 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> U64 inline_for_extraction let row (a:blake_alg) = lseq (uint_t (word_t a) SEC) 4 inline_for_extraction let word (a: hash_alg) = match a with | Blake2S | Blake2B -> row a | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> uint_t (word_t a) SEC (* In bytes. Should be: bytes_of_int (word_t a) *) let word_length: hash_alg -> Tot nat = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> 4 | SHA2_384 | SHA2_512 -> 8 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> 8 | Blake2S -> 4 | Blake2B -> 8 let rate (a: keccak_alg): (rate: size_nat{0 < rate / 8 /\ rate / 8 <= 200}) = match a with | SHA3_224 -> 1152 | SHA3_256 -> 1088 | SHA3_384 -> 832 | SHA3_512 -> 576 | Shake128 -> 1344 | Shake256 -> 1088 (* Number of words for a block size *) let block_word_length (a: hash_alg) = match a with | SHA3_224 -> normalize_term (rate SHA3_224 / 8 / 8) | SHA3_256 -> normalize_term (rate SHA3_256 / 8 / 8) | SHA3_384 -> normalize_term (rate SHA3_384 / 8 / 8) | SHA3_512 -> normalize_term (rate SHA3_512 / 8 / 8) | Shake128 -> normalize_term (rate Shake128 / 8 / 8) | Shake256 -> normalize_term (rate Shake256 / 8 / 8) | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | Blake2S | Blake2B -> 16 (* Define the size block in bytes *) let block_length a: nonzero = let open FStar.Mul in word_length a * block_word_length a (* Number of words for intermediate hash, i.e. the working state. *) inline_for_extraction let state_word_length a = match a with | MD5 -> 4 | SHA1 -> 5 | Blake2S | Blake2B -> 4 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> 25 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> 8 inline_for_extraction let extra_state a = match a with | Blake2S | Blake2B -> n:nat { n % block_length a = 0 } | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> unit (* The working state *) inline_for_extraction let words_state a = lseq (word a) (state_word_length a) (* Number of words for final hash *) inline_for_extraction let hash_word_length: a:md_alg -> Tot nat = function | MD5 -> 4 | SHA1 -> 5 | SHA2_224 -> 7 | SHA2_256 -> 8 | SHA2_384 -> 6 | SHA2_512 -> 8 (* Define the final hash length in bytes *) let hash_length (a: hash_alg { not (is_shake a) }): Lib.IntTypes.(n:size_nat { n > 0 }) = let open FStar.Mul in if is_md a then word_length a * hash_word_length a else match a with | SHA3_224 -> 28 | SHA3_256 -> 32 | SHA3_384 -> 48 | SHA3_512 -> 64 | Blake2S -> 4 * 8 | Blake2B -> 8 * 8 (** Padding *) (* Number of zeroes that should go into the padding *) let pad0_length (a:md_alg) (len:nat): Tot (n:nat{(len + 1 + n + len_length a) % block_length a = 0}) = (block_length a - (len + len_length a + 1)) % block_length a (* Total length for the padding, a.k.a. the suffix length. *) let pad_length (a: md_alg) (len: nat): Tot (n:nat { (len + n) % block_length a = 0 }) = pad0_length a len + 1 + len_length a (** Endian-ness *) (* Define word based operators *) let bytes_of_words: a:hash_alg{is_md a} -> Tot (#len:size_nat{FStar.Mul.(len * word_length a) <= max_size_t} -> s:lseq (word a) len -> Tot (lbytes FStar.Mul.(word_length a * len))) = function | MD5 -> Lib.ByteSequence.uints_to_bytes_le #U32 #SEC | SHA1 | SHA2_224 | SHA2_256 -> Lib.ByteSequence.uints_to_bytes_be #U32 #SEC | SHA2_384 | SHA2_512 -> Lib.ByteSequence.uints_to_bytes_be #U64 #SEC let words_of_bytes: a:hash_alg{is_md a} -> Tot (#len:size_nat{FStar.Mul.(len * word_length a) <= max_size_t} -> b:lbytes FStar.Mul.(word_length a * len) -> Tot (lseq (word a) len)) = function | MD5 -> Lib.ByteSequence.uints_from_bytes_le #U32 #SEC | SHA1 | SHA2_224 | SHA2_256 -> Lib.ByteSequence.uints_from_bytes_be #U32 #SEC | SHA2_384 | SHA2_512 -> Lib.ByteSequence.uints_from_bytes_be #U64 #SEC (** The data format taken and returned by the hash specifications. *) (* Input data. *) type bytes = Seq.seq uint8 (* Input data, multiple of a block length. *) let bytes_block a = l:bytes { Seq.length l = block_length a }
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Spec.Hash.Definitions.hash_alg -> Type0
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.hash_alg", "Spec.Hash.Definitions.bytes", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Modulus", "FStar.Seq.Base.length", "Lib.IntTypes.uint8", "Spec.Hash.Definitions.block_length" ]
[]
false
false
false
true
true
let bytes_blocks a =
l: bytes{Seq.length l % block_length a = 0}
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.to_hash_alg
val to_hash_alg : a: Spec.Blake2.Definitions.alg -> Spec.Hash.Definitions.hash_alg
let to_hash_alg (a : Spec.Blake2.alg) = match a with | Spec.Blake2.Blake2S -> Blake2S | Spec.Blake2.Blake2B -> Blake2B
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 92, "start_col": 0, "start_line": 89 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a } let blake_alg = a:hash_alg { is_blake a } let md_alg = a:hash_alg { is_md a } let fixed_len_alg = a:hash_alg { not (is_shake a) } inline_for_extraction let to_blake_alg (a:blake_alg) = match a with | Blake2S -> Spec.Blake2.Blake2S | Blake2B -> Spec.Blake2.Blake2B
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Spec.Blake2.Definitions.alg -> Spec.Hash.Definitions.hash_alg
Prims.Tot
[ "total" ]
[]
[ "Spec.Blake2.Definitions.alg", "Spec.Hash.Definitions.Blake2S", "Spec.Hash.Definitions.Blake2B", "Spec.Hash.Definitions.hash_alg" ]
[]
false
false
false
true
false
let to_hash_alg (a: Spec.Blake2.alg) =
match a with | Spec.Blake2.Blake2S -> Blake2S | Spec.Blake2.Blake2B -> Blake2B
false
Spec.Hash.Definitions.fst
Spec.Hash.Definitions.len_int_type
val len_int_type: maxed_hash_alg -> inttype
val len_int_type: maxed_hash_alg -> inttype
let len_int_type: maxed_hash_alg -> inttype = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> U64 | SHA2_384 | SHA2_512 -> U128 | Blake2S -> U64 | Blake2B -> U128
{ "file_name": "specs/Spec.Hash.Definitions.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 19, "end_line": 123, "start_col": 0, "start_line": 118 }
module Spec.Hash.Definitions open Lib.IntTypes open Lib.Sequence open Lib.ByteSequence (* This module contains shared definitions across all hash algorithms. It * defines a common, shared `hash_alg` type, along with word sizes, type of the * working state, and other helpers. It also defines the type of the core functions * init / update / pad / finish that any algorithm must implement in order to be * turned into a complete hash construction. * * Some notes about terminology: * - this module uses the HACL* naming convention, namely init/update/finish * (Cédric uses init/compress/extract in EverCrypt.HMAC and above) * - this module defines maximum lengths to be *bounds* (i.e. max_value + 1), a * somewhat dubious convention that persists for historical reasons, but that * is abandoned in miTLS via an extra indirection * * The following naming conventions are (tentatively) enforced. * - underscores (no camelCase) * - suffixes: _length for nat, _len for machine integer * - by default, lengths are in bytes; we use _word_length or _bit_len as suffixes * - for names that might conflict with their stateful counterparts, we prefix * with the type, e.g. words_state or bytes_block *) (** Supported hash algorithms. NOTE: this inductive is *append-only* because the constructors generate macro definitions that various bindings (OCaml, Rust) have taken a dependency on. So, in order to keep the numbering stable, we only append new cases, which is why SHA3_224 comes after SHA3_256 (the former was added *after* the latter). *) type hash_alg = | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 | SHA1 | MD5 | Blake2S | Blake2B | SHA3_256 | SHA3_224 | SHA3_384 | SHA3_512 | Shake128 | Shake256 inline_for_extraction noextract let is_sha2 = function | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false inline_for_extraction noextract let is_keccak = function | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_shake = function | Shake128 | Shake256 -> true | _ -> false inline_for_extraction noextract let is_blake = function | Blake2S | Blake2B -> true | _ -> false inline_for_extraction noextract let is_md = function | MD5 | SHA1 | SHA2_224 | SHA2_256 | SHA2_384 | SHA2_512 -> true | _ -> false let sha2_alg = a:hash_alg { is_sha2 a } let keccak_alg = a:hash_alg { is_keccak a } let blake_alg = a:hash_alg { is_blake a } let md_alg = a:hash_alg { is_md a } let fixed_len_alg = a:hash_alg { not (is_shake a) } inline_for_extraction let to_blake_alg (a:blake_alg) = match a with | Blake2S -> Spec.Blake2.Blake2S | Blake2B -> Spec.Blake2.Blake2B inline_for_extraction let to_hash_alg (a : Spec.Blake2.alg) = match a with | Spec.Blake2.Blake2S -> Blake2S | Spec.Blake2.Blake2B -> Blake2B (** Maximum input data length. *) (* In bytes. *) inline_for_extraction let max_input_length: hash_alg -> option pos = function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> Some (pow2 61 - 1) | SHA2_384 | SHA2_512 -> Some (pow2 125 - 1) | Blake2S -> Some (pow2 64 - 1) | Blake2B -> Some (pow2 128 - 1) | SHA3_224 | SHA3_256 | SHA3_384 | SHA3_512 | Shake128 | Shake256 -> None let maxed_hash_alg = a:hash_alg { Some? (max_input_length a) } let sha2_alg_is_maxed (a: hash_alg): Lemma (requires (is_sha2 a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_sha2 a) ] = () let md_alg_is_maxed (a: hash_alg): Lemma (requires (is_md a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_md a) ] = () let blake_alg_is_maxed (a: hash_alg): Lemma (requires (is_blake a)) (ensures (Some? (max_input_length a))) [ SMTPat (is_blake a) ] = () // TODO: many of these definitions are only used by the MD padding scheme, // meaning they should be defined over md_alg! (* A type that can hold a maximum length, in bits. *)
{ "checked_file": "/", "dependencies": [ "Spec.Blake2.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.Definitions.fst" }
[ { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.Sequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Spec.Hash.Definitions.maxed_hash_alg -> Lib.IntTypes.inttype
Prims.Tot
[ "total" ]
[]
[ "Spec.Hash.Definitions.maxed_hash_alg", "Lib.IntTypes.U64", "Lib.IntTypes.U128", "Lib.IntTypes.inttype" ]
[]
false
false
false
true
false
let len_int_type: maxed_hash_alg -> inttype =
function | MD5 | SHA1 | SHA2_224 | SHA2_256 -> U64 | SHA2_384 | SHA2_512 -> U128 | Blake2S -> U64 | Blake2B -> U128
false